public OBDIfEx() { StrLoadConfigResult = ""; OBDDll = new SH_OBD_Dll(".\\log"); OBDIf = OBDDll.GetOBDInterface(); Log = OBDDll.GetLogger(); _configResult = (int)LoadConfigResult.Success; if (!OBDIf.DllSettingsResult) { _configResult |= (int)LoadConfigResult.DllSettings; } MainSettings = LoadSettings <MainSettings>(_mainSettings_xml); DBandMES = LoadSettings <DBandMES>(_dbandMES_xml); OBDResultSetting = LoadSettings <OBDResultSetting>(_obdResultSetting_xml); if (_configResult != (int)LoadConfigResult.Success) { if ((_configResult & (int)LoadConfigResult.DllSettings) == (int)LoadConfigResult.DllSettings) { StrLoadConfigResult += "Dll设置读取错误\n"; } else if ((_configResult & (int)LoadConfigResult.DBandMES) == (int)LoadConfigResult.DBandMES) { StrLoadConfigResult += "数据库和MES设置读取错误\n"; } else if ((_configResult & (int)LoadConfigResult.OBDResultSetting) == (int)LoadConfigResult.OBDResultSetting) { StrLoadConfigResult += "OBD检测结果设置读取错误\n"; } else if ((_configResult & (int)LoadConfigResult.MainSettings) == (int)LoadConfigResult.MainSettings) { StrLoadConfigResult += "主程序设置读取错误\n"; } } ScannerPortOpened = false; if (MainSettings.UseSerialScanner) { ScannerSP = new SerialPortClass( MainSettings.ScannerPortName, MainSettings.ScannerBaudRate, Parity.None, 8, StopBits.One ); try { ScannerSP.OpenPort(); ScannerPortOpened = true; } catch (Exception ex) { Log.TraceError("打开扫码枪串口出错: " + ex.Message); } } }
public DllMain(string basePath) { _log = new Logger(basePath + "\\log", EnumLogLevel.LogLevelAll, true, 100); _log.TraceInfo("==========================================================================="); _log.TraceInfo("===================== START DllVersion: " + DllVersion <DllMain> .AssemblyVersion + " ====================="); _cfg = new Config(basePath, _log); _cfg.LoadConfigAll(); _addr = 0x83; _sync = 0x55; _k1 = 0x01; _k2 = 0x8A; _k3 = 0x75; _loopInterval = 20; _retryTimes = 2; _sp = new SerialPortClass(_cfg.Setting.Data.SerialPort, _cfg.Setting.Data.SerialBaud, Parity.None, 8, StopBits.One); try { _sp.OpenPort(); _sp.DataReceived += new SerialPortClass.SerialPortDataReceiveEventArgs(SerialDataReceived); _frames = new List <Frame>(); } catch (Exception ex) { _log.TraceError("Open serial port error: " + ex.Message); throw; } }
static void Main(string[] args) { _sp = new SerialPortClass("COM6", 9600, Parity.None, 8, StopBits.One); try { _sp.OpenPort(); _sp.DataReceived += new SerialPortClass.SerialPortDataReceiveEventArgs(SerialDataReceived); _frames = new List <Frame>(); Console.WriteLine("Waiting for command"); } catch (Exception ex) { Console.WriteLine("Open serial port error: " + ex.Message); throw; } try { while (true) { if (_frames.Count > 0) { if (_frames.First().Baud == 0x05 && _frames.First().Data == 0x83) { _sp.SendData(new byte[] { 0x96, 0x55 }, 0, 2); _sp.SendData(new byte[] { 0x96, 0x01 }, 0, 2); _sp.SendData(new byte[] { 0x96, 0x8A }, 0, 2); } else if (_frames.First().Baud == 0x96 && _frames.First().Data == 0x75) { ++_0300Times; SendData(0xF6, "ABSMadeByAPG"); } else if (_frames.First().Baud == 0x96 && _frames.First().Data != null) { List <byte> cmd = new List <byte>(); RecvCMD(cmd); switch (cmd[0]) { case 0x03: switch (cmd[2]) { case 0x00: switch (++_0300Times) { case 1: SendData(0xF6, "ABSMadeByAPG"); break; case 2: SendData(0xF6, "APG3550700E3"); break; case 3: SendData(0xF6, "SWV ZHU V500"); break; case 4: SendData(0xF6, new byte[] { 0x00, 0x07, 0xFA, 0x00, 0x01 }); _0300Times = 0; break; } break; case 0x05: // 不处理 _0300Times = 0; SendData(0x09, Array.Empty <byte>()); break; case 0x06: // 系统复位退出 _0300Times = 0; _0404Times = 0; _syncNum = 0; break; case 0x07: SendData(0xFC, new byte[] { 0x01, 0x1B, 0x05, 0x01, 0x1D, 0x05, 0x01, 0x22, 0x05, 0x01, 0x1F, 0x05, 0x01, 0x2D, 0x05, 0x01, 0x2E, 0x05 }); _0300Times = 0; break; case 0x09: // 不处理 _0300Times = 0; break; default: _0300Times = 0; break; } break; case 0x04: switch (cmd[2]) { case 0x04: switch (++_0404Times) { case 1: SendData(0xF5, new byte[] { 0x04, 0xFC }); break; case 2: SendData(0xF5, new byte[] { 0x02, 0xA1 }); break; case 3: SendData(0xF5, new byte[] { 0x02, 0xA3 }); break; case 4: SendData(0xF5, new byte[] { 0x02, 0xA4 }); break; case 5: SendData(0xF5, new byte[] { 0x02, 0xA5 }); break; case 6: SendData(0xF5, new byte[] { 0x02, 0xA6 }); break; case 7: SendData(0xF5, new byte[] { 0x02, 0xA3 }); break; case 8: SendData(0xF5, new byte[] { 0x02, 0xA2 }); break; case 9: SendData(0xF5, new byte[] { 0x02, 0xA1 }); break; case 10: SendData(0xF5, new byte[] { 0x02, 0xA8 }); break; case 11: SendData(0xF5, new byte[] { 0x02, 0xA9 }); break; case 12: SendData(0xF5, new byte[] { 0x02, 0xAA }); break; case 13: SendData(0xF5, new byte[] { 0x02, 0xAB }); break; case 14: SendData(0xF5, new byte[] { 0x02, 0xA8 }); break; case 15: SendData(0xF5, new byte[] { 0x02, 0xA2 }); break; case 16: SendData(0xF5, new byte[] { 0x02, 0xA1 }); break; case 17: SendData(0xF5, new byte[] { 0x02, 0xAD }); break; case 18: SendData(0xF5, new byte[] { 0x02, 0xAE }); break; case 19: SendData(0xF5, new byte[] { 0x02, 0xAF }); break; case 20: SendData(0xF5, new byte[] { 0x02, 0xB0 }); break; case 21: SendData(0xF5, new byte[] { 0x02, 0xAD }); break; case 22: SendData(0xF5, new byte[] { 0x02, 0xA2 }); break; case 23: SendData(0xF5, new byte[] { 0x02, 0xA1 }); break; case 24: SendData(0xF5, new byte[] { 0x02, 0xB2 }); break; case 25: SendData(0xF5, new byte[] { 0x02, 0xB3 }); break; case 26: SendData(0xF5, new byte[] { 0x02, 0xB4 }); break; case 27: SendData(0xF5, new byte[] { 0x02, 0xB5 }); break; case 28: SendData(0xF5, new byte[] { 0x02, 0xB2 }); break; case 29: SendData(0x0A, new byte[] { 0xAF }); _0404Times = 0; break; } break; case 0x29: SendData(0xE7, new byte[] { 0x01, 0x01, 0x01, 0x01 }); _0404Times = 0; break; default: _0404Times = 0; break; } break; } } _frames.Clear(); } } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); throw; } }