public static int CreateTestedPortsList(ref LogWindow lw, string message, ref System.Windows.Forms.ProgressBar pb, bool breakWhenFound) { DevicesLists.Clear(); int[] portBaudrates = { 115200, 57600, 38400, 19200, 9600, 4800, 2400 }; MainWindow.SetupProgressBar(ref pb, PortsList.Count * portBaudrates.Length); CommunicationResult result = CommunicationResult.Failed; Communication.CardID = 255; foreach (int baudrate in portBaudrates) { for (int i = 0; i < PortsList.Count; i++) { MainWindow.WriteProgress(ref pb); //AvailablePort port = PortsList[i]; //CurrentSerialPort = PortsList[i]; CurrentSerialPort = new AvailablePort(PortsList[i].PortName, baudrate); MainWindow.Log(ref lw, String.Format(message, i + 1, PortsList.Count, CurrentSerialPort.PortName, baudrate)); //Communication.Type = CommunicationType.Serial; result = Communication.TestController(); if (result == CommunicationResult.Success) { CurrentSerialPort = new AvailablePort(CurrentSerialPort.PortName, baudrate); DevicesLists.Add(CurrentSerialPort); if (breakWhenFound == true) { pb.Value = pb.Maximum; Baudrate = baudrate; break; } } } if ((result == CommunicationResult.Success) && (breakWhenFound == true)) { break; } } MainWindow.SetupProgressBar(ref pb, 0); CurrentSerialPort = new AvailablePort("test", 0); return(DevicesLists.Count); }
public static List <AvailablePort> CreatePortsList(int maxNumber) { PortsList.Clear(); DevicesLists.Clear(); using (SerialPort testSerialPort = new SerialPort()) { testSerialPort.ReadTimeout = 1000; testSerialPort.WriteTimeout = 1000; for (int i = 1; i <= maxNumber; i++) { string portName = "COM" + i.ToString(); testSerialPort.PortName = portName; testSerialPort.BaudRate = Baudrate; try { testSerialPort.Open(); if (testSerialPort.IsOpen) { //AvailablePort tmpPort = new AvailablePort(); CurrentSerialPort = new AvailablePort(portName, baudrate); //CurrentSerialPort.PortName = portName; //CurrentSerialPort.Tested = false; //CurrentSerialPort.Baudrate = baudrate; PortsList.Add(CurrentSerialPort); testSerialPort.Close(); } } catch { if (testSerialPort.IsOpen) { testSerialPort.Close(); } } } } return(PortsList); }