private SerialCOMManager() { SerialCOMListRepository serialCOMListRepository = new SerialCOMListRepository(); List <SerialCOMList> serialCOMList = serialCOMListRepository.GetList(); TurnTableListRepository turnTableListRepository = new TurnTableListRepository(); List <Guid?> turnTableSerialComIDs = turnTableListRepository.GetListWithCondition(new { CommunicationType = 1 }).Select(_ => _.CommunicationID).ToList(); UPSListRepository UPSListRepository = new UPSListRepository(); List <Guid?> UPSSerialComIDs = UPSListRepository.GetListWithCondition(new { CommunicationType = 1 }).Select(_ => _.CommunicationID).ToList(); SolarEnergyListRepository solarEnergyListRepository = new SolarEnergyListRepository(); List <Guid?> solarEnergySerialComIDs = solarEnergyListRepository.GetListWithCondition(new { CommunicationType = 1 }).Select(_ => _.CommunicationID).ToList(); string falseMsg = string.Empty; foreach (var item in serialCOMList) { try { SerialPort serialPort = new SerialPort(); serialPort.PortName = item.COMNo; //COM口名 serialPort.BaudRate = item.Baud; //波特率 serialPort.DataBits = item.DataBit; //每个字节的标准数据位长度 serialPort.Parity = (Parity)item.CheckBit; serialPort.StopBits = StopBits.One; if (turnTableSerialComIDs.Contains(item.DeviceID)) { serialPort.DataReceived += TurntableSerialPortDataReceived; } else if (UPSSerialComIDs.Contains(item.DeviceID)) { serialPort.DataReceived += UPSSerialPortDataReceived; } else if (solarEnergySerialComIDs.Contains(item.DeviceID)) { serialPort.DataReceived += SolarEnergySerialPortDataReceived; } SerialPortDic[item.DeviceID] = serialPort; if (!OpenSerialCOM(item.DeviceID, out falseMsg)) { throw new Exception(falseMsg); } } catch (Exception ex) { Console.WriteLine(falseMsg); LogServerManager.AddErrLog(ErrLogType.DBErr, ex); continue; } } }