/// <summary> /// this function starts the serial port detection, waits for the results to return, and /// reports back the detect serial port name /// </summary> /// <returns></returns> public string DeterminePort(int baud) { Initialize(); string comport = "invalid"; bool done = false; SerialAutodetectConfig config = new SerialAutodetectConfig(); config.m_baud = baud; //open and test all serial ports at once foreach (String s in DeviceDriver.GetPortNames()) { if (s.Equals("COM113")) { // create a new tester ConnectionTester tester = new ConnectionTester(s); // specify the name of the port we're trying to detect //set the baud tester.m_baud = config.m_baud; //set up to listen to events tester.ConnectionTesterStatusEvent += new ConnectionTester.ConnectionTesterStatus(ConnectionTesterStatusDel); //start it off tester.Start(); } } m_starttime = Environment.TickCount; while (!done) { try { //check for timeout if (Environment.TickCount >= (m_starttime + TIMEOUTTIME)) { done = true; } Thread.Sleep(0); // yield lock (m_lock) { // check the m_lstresults foreach (ConnectionTester con in m_lstresults) { if (con.m_result == ConnectionTester.eConnTestStatus.eDeviceResponded) { comport = con.m_portname; done = true; Initialize(); break; } } } } catch (Exception ex) { DebugLogger.Instance().LogError(ex); } } return(comport); }
private void SetData() { ConnectionConfig cc = m_config; cmbPorts.Items.Clear(); //set all available port names //foreach (String s in SerialPort.GetPortNames()) if (m_addauto) { cmbPorts.Items.Add("AutoDetect"); } foreach (String s in DeviceDriver.GetPortNames()) { cmbPorts.Items.Add(s); } cmbPorts.SelectedItem = cc.comname; cmbSpeed.SelectedItem = cc.speed.ToString(); txtDataBits.Text = cc.databits.ToString(); }