using EGFramework; /// /// 检测串口对应的空轨驱动器,或是颜色传感器,检测后记录到串口列表中 /// public class ModelSerialTest : EGModule,IEGFramework { public const int BAUD_RATE_ENGINE = 9600; public const int BAUD_RATE_COLOR = 38400; public const string PORT_LINUX_0 = "/dev/ttyUSB0"; public const string PORT_LINUX_1 = "/dev/ttyUSB1"; public const string PORT_WIN_0 = "COM2"; public const string PORT_WIN_1 = "COM5"; public bool IsLinux = true; public string TestPort0 { set; get; } = PORT_LINUX_0; public string TestPort1 { set; get; } = PORT_LINUX_1; public string ColorSerialPort { set; get; } = ""; public string EngineSerialPort { set; get; } = ""; public IUnRegister ColorSensorInitEvent { set; get; } public IUnRegister EngineInitEvent{ set; get; } public override void Init() { ColorSensorInitEvent = this.EGRegisterMessageEvent(OnColorSensorInit); this.EGOnMessage(); this.EGOnMessage(); if (!IsLinux) { TestPort0 = PORT_WIN_0; TestPort1 = PORT_WIN_1; } InitSerialPort(); // throw new NotImplementedException(); } public async void InitSerialPort() { this.EGSerialPort().Open(TestPort0, 38400); this.EGSendMessage(new RequestColorSingleRead(), TestPort0, ProtocolType.SerialPort); await Task.Delay(2000); if(ColorSerialPort == "") { this.EGSerialPort().Close(TestPort0); this.EGSerialPort().Open(TestPort1, 38400); this.EGSendMessage(new RequestColorSingleRead(), TestPort1, ProtocolType.SerialPort); await Task.Delay(2000); if(ColorSerialPort == "") { this.EGSerialPort().Close(TestPort1); EG.Print("Color sensor init failed,Time out."); } else { EG.Print("_"+this.EGSerialPort().SerialPortDevices.ContainsKey(TestPort0)); this.EGSerialPort().Open(TestPort0, 9600); this.EGSendMessage(new RequestTrackReadEnabled(), TestPort0, ProtocolType.SerialPort); EngineInitEvent = this.EGRegisterMessageEvent(OnEngineInit); await Task.Delay(2000); if(EngineSerialPort == "") { this.EGSerialPort().Close(TestPort0); EG.Print("Engine init failed USB0,Time out."); } } } else { this.EGSerialPort().Open(TestPort1, 9600); this.EGSendMessage(new RequestTrackReadEnabled(), TestPort1, ProtocolType.SerialPort); EngineInitEvent = this.EGRegisterMessageEvent(OnEngineInit); await Task.Delay(2000); if(EngineSerialPort == "") { this.EGSerialPort().Close(TestPort1); EG.Print("Engine init failed USB1,Time out."); } } } public void OnColorSensorInit(ResponseColorSensor res, string sender, ProtocolType type) { if (type == ProtocolType.SerialPort) { ColorSensorInitEvent.UnRegister(); this.ColorSerialPort = sender; EG.Print("Color sensor init success!"); } } public void OnEngineInit(ResponseTrack res,string sender,ProtocolType type) { if (type == ProtocolType.SerialPort) { EngineInitEvent.UnRegister(); this.EngineSerialPort = sender; EG.Print("Engine sensor init success!"); this.GetModule().ResetTrack(); } } }