/// <summary> /// 检测串口列表并处理 /// </summary> private void DetectSerialPortListProcess() { //获取当前串口列表 nowSerialPortList.Clear(); foreach (string item in SerialPort.GetPortNames()) { nowSerialPortList.Add(item.ToString()); } //串口列表比对 if (CompareArray(nowSerialPortList.ToArray(), bakSerialPortList.ToArray()) == false) { //获取串口信息 GetSerialPortInfo(DicSerialPortInfo); //更新备份列表 bakSerialPortList.Clear(); foreach (string item in nowSerialPortList) { int index = StrSerialPortDefaultInfo.ToList().IndexOf(DicSerialPortInfo[item]); if (DicSerialPortInfo.ContainsKey(item) && (index >= 0)) { bakSerialPortList.Insert(0, item); } else { bakSerialPortList.Add(item); } } //触发事件 EventSerialPortList(bakSerialPortList.ToArray()); } }
/// <summary> /// 判断是否默认端口 /// </summary> /// <param name="item"></param> private bool IsDefaultSerialPort(string portName) { return(DicSerialPortInfo.ContainsKey(portName) && (StrSerialPortDefaultInfo.ToList().IndexOf(DicSerialPortInfo[portName]) > 0)); }