private void RefreshDisplay() { richTextDTC.Text = ""; richTextPending.Text = ""; richTextPermanent.Text = ""; string text; Font fontBigBold = new Font("Courier New", 10f, FontStyle.Bold); Font fontSmallBold = new Font("Courier New", 8f, FontStyle.Bold); Font fontSmallItalic = new Font("Courier New", 8f, FontStyle.Italic | FontStyle.Bold); try { if (m_ListDTC.Count > 0) { for (int i = 1; i <= m_ListDTC.Count; i++) { DTC dtc = m_ListDTC[i - 1]; richTextDTC.SelectionFont = fontBigBold; richTextDTC.SelectionColor = Color.Red; richTextDTC.AppendText(string.Format("{0}. {1}\r\n", i, dtc.Name)); if (string.IsNullOrEmpty(dtc.Description)) { richTextDTC.SelectionFont = fontSmallItalic; richTextDTC.SelectionColor = Color.Black; text = " 未发现定义\r\n\r\n"; } else { richTextDTC.SelectionFont = fontSmallBold; richTextDTC.SelectionColor = Color.Black; text = string.Format(" {0}: {1}\r\n\r\n", dtc.Category, dtc.Description); } richTextDTC.AppendText(text); } } else { richTextDTC.SelectionFont = fontBigBold; richTextDTC.SelectionColor = Color.Green; richTextDTC.AppendText("无存储的故障码"); } if (m_ListPending.Count > 0) { for (int i = 1; i <= m_ListPending.Count; i++) { DTC dtc = m_ListPending[i - 1]; richTextPending.SelectionFont = fontBigBold; richTextPending.SelectionColor = Color.Red; richTextPending.AppendText(string.Format("{0}. {1}\r\n", i, dtc.Name)); if (string.IsNullOrEmpty(dtc.Description)) { richTextPending.SelectionFont = fontSmallItalic; richTextPending.SelectionColor = Color.Black; text = " 未发现定义\r\n\r\n"; } else { richTextPending.SelectionFont = fontSmallBold; richTextPending.SelectionColor = Color.Black; text = string.Format(" {0}: {1}\r\n\r\n", dtc.Category, dtc.Description); } richTextPending.AppendText(text); } } else { richTextPending.SelectionFont = fontBigBold; richTextPending.SelectionColor = Color.Green; richTextPending.AppendText("无未决故障码"); } if (m_ListPermanent.Count > 0) { for (int i = 1; i <= m_ListPermanent.Count; i++) { DTC dtc = (DTC)m_ListPermanent[i - 1]; richTextPermanent.SelectionFont = fontBigBold; richTextPermanent.SelectionColor = Color.Red; richTextPermanent.AppendText(string.Format("{0}. {1}\r\n", i, dtc.Name)); if (string.IsNullOrEmpty(dtc.Description)) { richTextPermanent.SelectionFont = fontSmallItalic; richTextPermanent.SelectionColor = Color.Black; text = " 未发现定义\r\n\r\n"; } else { richTextPermanent.SelectionFont = fontSmallBold; richTextPermanent.SelectionColor = Color.Black; text = string.Format(" {0}: {1}\r\n\r\n", dtc.Category, dtc.Description); } richTextPermanent.AppendText(text); } } else { richTextPermanent.SelectionFont = fontBigBold; richTextPermanent.SelectionColor = Color.Green; richTextPermanent.AppendText("无永久故障码"); } } finally { fontBigBold.Dispose(); fontSmallBold.Dispose(); fontSmallItalic.Dispose(); } }
private void CollectData() { DisplayStatusMessage("请求MIL状态和故障码数量"); DisplayRequest("0101"); OBDParameterValue value5 = m_obdInterface.GetValue("SAE.MIL"); this.BeginInvoke((EventHandler) delegate { progressBar.Value += 1; }); if (!value5.ErrorDetected) { if (value5.BoolValue) { DisplayDetailMessage("MIL状态: On"); m_bReportForm.ReportPage1.MilStatus = true; } else { DisplayDetailMessage("MIL状态: Off"); m_bReportForm.ReportPage1.MilStatus = false; } } OBDParameterValue value3 = m_obdInterface.GetValue("SAE.DTC_COUNT"); this.BeginInvoke((EventHandler) delegate { progressBar.Value += 1; }); if (!value3.ErrorDetected) { m_bReportForm.ReportPage1.TotalCodes = (int)value3.DoubleValue; DisplayDetailMessage("存储的故障码数量: " + value3.DoubleValue.ToString()); } DisplayStatusMessage("请求存储的故障码列表"); OBDParameterValue value4 = m_obdInterface.GetValue("SAE.STORED_DTCS"); this.BeginInvoke((EventHandler) delegate { progressBar.Value += 1; }); if (!value4.ErrorDetected) { m_bReportForm.ReportPage1.DTCList.Clear(); foreach (string strVal in value4.ListStringValue) { m_bReportForm.ReportPage1.DTCList.Add(strVal); DisplayDetailMessage("存储的故障码: " + strVal); DTC dtc2 = m_obdInterface.GetDTC(strVal); if (dtc2 != null) { m_bReportForm.ReportPage1.DTCDefinitionList.Add(dtc2.Description); } } } DisplayStatusMessage("请求未决故障码列表"); OBDParameterValue value2 = m_obdInterface.GetValue("SAE.PENDING_DTCS"); this.BeginInvoke((EventHandler) delegate { progressBar.Value += 1; }); if (!value2.ErrorDetected) { m_bReportForm.ReportPage1.PendingList.Clear(); foreach (string strVal in value2.ListStringValue) { m_bReportForm.ReportPage1.PendingList.Add(strVal); DisplayDetailMessage("未决故障码: " + strVal); DTC dtc = m_obdInterface.GetDTC(strVal); if (dtc != null) { m_bReportForm.ReportPage1.PendingDefinitionList.Add(dtc.Description); } } } DisplayStatusMessage("检查冻结帧数据"); OBDParameter parameter = m_obdInterface.LookupParameter("SAE.FF_DTC"); if (parameter != null) { OBDParameter freezeFrameCopy = parameter.GetFreezeFrameCopy(0); value2 = m_obdInterface.GetValue(freezeFrameCopy); this.BeginInvoke((EventHandler) delegate { progressBar.Value += 1; }); if (!value2.ErrorDetected) { m_bReportForm.ReportPage1.FreezeFrameDTC = value2.StringValue; DisplayDetailMessage("找到冻结帧数据 " + value2.StringValue); CollectFreezeFrameData(); } else { m_bReportForm.ReportPage1.FreezeFrameDTC = "P0000"; DisplayDetailMessage("未找到冻结帧数据"); } CollectMonitoringTestData(); this.BeginInvoke((EventHandler) delegate { progressBar.Value = progressBar.Maximum; btnGenerate.Enabled = true; // ReportForm窗体类里需要调用SaveFileDialog.ShowDialog() // 该方法需要调用COM对象(系统中的打开文件通用对话框),故需要调用者线程具有STA模式(单线程单元模式) // 而Task无法显式设置线程模式为STA,故只能在主UI线程里调用ReportForm窗体类 m_bReportForm.ShowDialog(); }); } }