public bool TCPClientInit(bool bLog = true) { try { m_client = new TcpClient(m_cfg.Setting.Data.TCPServerIP, m_cfg.Setting.Data.TCPServerPort); m_clientStream = m_client.GetStream(); if (bLog || !m_bLastTestStatus) { m_log.TraceInfo("TcpClient init success"); } m_bInited = true; } catch (Exception ex) { if (m_clientStream != null) { m_clientStream.Close(); } if (m_client != null) { m_client.Close(); } if (bLog || m_bLastTestStatus) { m_log.TraceError("TcpClient init error: " + ex.Message); } m_bInited = false; } m_bLastTestStatus = m_bInited; return(m_bInited); }
public Main() { InitializeComponent(); m_bTesting = false; m_lastHeight = this.Height; m_serialRecvBuf = ""; m_bLastStatus = false; m_bBlink = false; m_lockObj = new object(); m_counterFailed = 0; m_bLoop = false; m_OBDInited = false; m_inFinishing = 0; this.Text = Properties.Resources.MainTitle + " Ver: " + MainFileVersion.AssemblyVersion; m_log = new BaseLib.Logger(".\\log\\Temper", BaseLib.EnumLogLevel.LogLevelAll, true, 100); m_log.TraceInfo("=================================================================="); m_log.TraceInfo("===================== START Ver: " + MainFileVersion.AssemblyVersion + " ====================="); m_cfg = new Config(m_log); try { m_cfg.LoadConfigAll(); } catch (ApplicationException ex) { MessageBox.Show(ex.Message.Split(':')[0] + "配置文件读取出错,将会使用默认配置", "初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); } m_db = new BaseLib.Model("TemperSensor", m_cfg, m_log); m_tester = new Temper(m_log, m_cfg, m_db); m_tester.TCPClientInit(); if (!m_tester.GetInitStatus()) { MessageBox.Show("无法连接WiFi串口服务器", "初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (m_cfg.Setting.Data.ScannerPort.Length > 0) { m_sp = new BaseLib.SerialPortClass( m_cfg.Setting.Data.ScannerPort, m_cfg.Setting.Data.ScannerBaud, Parity.None, 8, StopBits.One ); try { m_sp.OpenPort(); m_sp.DataReceived += new BaseLib.SerialPortClass.SerialPortDataReceiveEventArgs(SerialDataReceived); } catch (Exception ex) { m_log.TraceError("Open serial port error: " + ex.Message); MessageBox.Show("打开串口扫码枪出错", "初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } m_obdDll = new OBDDll(m_log); m_timerInterval = new System.Timers.Timer(m_cfg.Setting.Data.Interval); m_timerInterval.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeInterval); m_timerInterval.AutoReset = true; m_timerInterval.Enabled = false; m_timerTick = new System.Timers.Timer(m_cfg.Setting.Data.Interval); // 心跳间隔同采样间隔一致 m_timerTick.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeTick); m_timerTick.AutoReset = true; m_timerTick.Enabled = true; m_timerBlink = new System.Timers.Timer(m_cfg.Setting.Data.Interval); // 闪烁间隔同采样间隔一致 m_timerBlink.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeBlink); m_timerBlink.AutoReset = true; m_timerBlink.Enabled = true; }
private void FinishTest() { m_timerInterval.Enabled = false; m_timerTick.Enabled = true; bool bResult = false; double dAverage1 = 0; double dAverage2 = 0; double dCount1 = 0; double dCount2 = 0; double dLastTemper1 = 0; double dLastTemper2 = 0; if (!m_bLoop) { switch (m_cfg.Setting.Data.Rule) { case 0: bResult = m_tester.GetResult0(m_dSetup, out dLastTemper1, out dLastTemper2); m_log.TraceInfo(string.Format("Get the test result: {0}, last: {1}℃/{2}℃", bResult, dLastTemper1.ToString("F2"), dLastTemper2.ToString("F2"))); break; case 1: bResult = m_tester.GetResult1(m_dSetup, out dAverage1, out dAverage2); m_log.TraceInfo(string.Format("Get the test result: {0}, average: {1}℃/{2}℃", bResult, dAverage1.ToString("F2"), dAverage2.ToString("F2"))); break; case 2: bResult = m_tester.GetResult2(m_dSetup, out dCount1, out dCount2); if (m_cfg.Setting.Data.SuccessiveValue > 1) { m_log.TraceInfo(string.Format("Get the test result: {0}, count: {1}/{2}", bResult, dCount1.ToString("F0"), dCount2.ToString("F0"))); } else { m_log.TraceInfo(string.Format("Get the test result: {0}, percentage: {1}%/{2}%", bResult, dCount1.ToString("F2"), dCount2.ToString("F2"))); } break; } } string strTimeStamp = m_tester.GetTimeStamp(); try { m_tester.ExportResultFile(bResult, strTimeStamp); m_bBlink = !bResult; } catch (Exception ex) { m_log.TraceError("ExportResultFile() ERROR: " + ex.Message); if (!m_bLoop) { MessageBox.Show(ex.Message, "ExportResultFile() ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } } if (!m_bLoop) { try { m_tester.WriteDB(bResult, strTimeStamp); } catch (ApplicationException ex) { MessageBox.Show(ex.Message, "WriteDB() ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { m_log.TraceError("WriteDB() ERROR: " + ex.Message); MessageBox.Show(ex.Message, "WriteDB() ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (bResult) { m_counterFailed = 0; } else { ++m_counterFailed; } this.Invoke((EventHandler) delegate { if (bResult) { this.lblInfo.ForeColor = Color.Green; this.lblInfo.Text = "合格"; } else { this.lblInfo.ForeColor = Color.Red; this.lblInfo.Text = "不合格"; } switch (m_cfg.Setting.Data.Rule) { case 0: this.lblInfo.Text += ", 当前温度: "; switch (m_cfg.Setting.Data.Temper) { case 0: this.lblInfo.Text += dLastTemper1.ToString("F2") + "℃/" + dLastTemper2.ToString("F2") + "℃"; break; case 1: this.lblInfo.Text += dLastTemper1.ToString("F2") + "℃"; break; case 2: this.lblInfo.Text += dLastTemper2.ToString("F2") + "℃"; break; } break; case 1: this.lblInfo.Text += ", 平均温度: "; switch (m_cfg.Setting.Data.Temper) { case 0: this.lblInfo.Text += dAverage1.ToString("F2") + "℃/" + dAverage2.ToString("F2") + "℃"; break; case 1: this.lblInfo.Text += dAverage1.ToString("F2") + "℃"; break; case 2: this.lblInfo.Text += dAverage2.ToString("F2") + "℃"; break; } break; case 2: this.lblInfo.Text += ", 连续合格温度点"; if (m_cfg.Setting.Data.SuccessiveValue > 1) { this.lblInfo.Text += "(" + m_cfg.Setting.Data.SuccessiveValue.ToString("F0") + ")"; switch (m_cfg.Setting.Data.Temper) { case 0: this.lblInfo.Text += "个数: " + dCount1.ToString("F0") + "个/" + dCount2.ToString("F0") + "个"; break; case 1: this.lblInfo.Text += dCount1.ToString("F0") + "个"; break; case 2: this.lblInfo.Text += dCount2.ToString("F0") + "个"; break; } } else { this.lblInfo.Text += "(" + (m_cfg.Setting.Data.SuccessiveValue * 100).ToString("F2") + "%)"; switch (m_cfg.Setting.Data.Temper) { case 0: this.lblInfo.Text += "占比: " + (dCount1 * 100).ToString("F2") + "%/" + (dCount2 * 100).ToString("F2") + "%"; break; case 1: this.lblInfo.Text += (dCount1 * 100).ToString("F2") + "%"; break; case 2: this.lblInfo.Text += (dCount2 * 100).ToString("F2") + "%"; break; } } break; } if (m_counterFailed >= m_cfg.Setting.Data.TestFailedQty) { m_log.TraceWarning(string.Format("There are {0} NG vehicles", m_counterFailed)); this.lblInfo.Text += ", 已连续" + m_counterFailed.ToString() + "辆车不合格"; this.lblInfo.BackColor = Color.Red; this.lblInfo.ForeColor = Color.White; } m_strInfo = this.lblInfo.Text; }); } else { StopLoop(false); Task.Factory.StartNew(StartTest); } m_bTesting = false; }