對於串口設備常常遇到從新拔插串口設備時候,程序又須要從新選擇串口打開。對此不多麻煩的要死。string
如今咱們能夠根據該設備的id去遍歷串口設備,一旦符合就打開此串口便可。it
public void init()
{foreach
string portName = "";
List<USBDeviceInfo> usbList = GetUSBDevices();
foreach (var usb in usbList)
{
if (usb.PnpDeviceID.IndexOf(@"USB\VID_10C4&PID_EA60") >= 0) //設備idList
{
portName = usb.DeviceID;
break;
}
}
if (portName != "")
{
serialPort = new SerialPort(portName);遍歷
serialPort.BaudRate = 19200;
serialPort.Parity = System.IO.Ports.Parity.None;
carCls = new CarCls(1, serialPort);
carCls.intcom();
}
}程序