//Catch event from detector if COM is connect or dis connect from pc private void PortsChanged(object sender, PortsChangedArgs e) { if (e.EventType == EventType.Insertion) { if (serialPortMessage == null) { ScanPorts(); } } else { serialPortMessage.EndRead(); var foundFlag = false; foreach (var port in e.SerialPorts) { if (port == serialPortMessage.name) { foundFlag = true; break; } } if (!foundFlag) { serialPortMessage.Dispose(); serialPortMessage = null; } else { serialPortMessage.StartRead(); } } }
//Use COM name to init SerialPortMessageObject and Start Listen private void SetSerialPortMessage(string port) { if (serialPortMessage != null) { serialPortMessage.Dispose(); } serialPortMessage = new SerialPortMessage(new System.IO.Ports.SerialPort(port)); serialPortMessage.SerialPortReceived += messageScheduler.ScheduleFuction; serialPortMessage.StartRead(); }