private void comport_box_SC_SelectedIndexChanged(object sender, EventArgs e) { String serialport_name = (String)comport_box_SC.SelectedItem; // if selected item is same as currently connected, do nothing and return if (comport_SC != null && comport_SC.IsOpen && comport_SC.PortName == serialport_name) { return; } add_text_message_SC("trying to connect " + serialport_name + "...\n"); boost_type boost_type_temp = test_serial_port(serialport_name); if (boost_type_temp == boost_type.POCKET_DETECTOR) { } else if (boost_type_temp == boost_type.SMART_CUE) { try { comport_SC = new SerialPort(serialport_name, BAUDRATE, PARITY, DATABITS, STOPBITS); comport_SC.ReadTimeout = SERIAL_TIMEOUT_MS; comport_SC.Open(); add_text_message_SC("succeeded to connect " + serialport_name + "\n"); smartcue_worker.RunWorkerAsync(); return; } catch { } } else if (boost_type_temp == boost_type.NOT_RECOGNIZED) { add_text_message_SC("unrecognizable device: " + serialport_name + "\n"); } else { add_text_message_SC("unable to connect " + serialport_name + "\n"); } }
// comport_scan_worker continuously update serialport and make boost workers work // private void comport_scan_worker_DoWork(object sender, DoWorkEventArgs e) { while (true) { Thread.Sleep(1000); Console.Write("comscanner called\n"); if (comport_scan_worker.CancellationPending) { Console.Write("cancellation requested\n"); e.Cancel = true; return; } // always update comports box for all the devices // update the list of serial ports keeping selected item if already selected String[] serialport_names = SerialPort.GetPortNames(); // if updated, update comport list Invoke(new update_comport_list_delegate(update_comport_list), (Object)serialport_names); // scan all the comports and open a port if a valid port is detected and no port is already opend for the device for (int i = 0; i < serialport_names.Length; i++) { boost_type boost_type_temp = test_serial_port(serialport_names[i]); // if port is valid pocket detector and pocket detector is not yet opened, // select the index to make selectedindexchanged() run, in which the port is opened if (boost_type_temp == boost_type.POCKET_DETECTOR && (comport_PD == null || !comport_PD.IsOpen)) { Invoke(new select_index_delegate(select_index), comport_box_PD, i); } else if (boost_type_temp == boost_type.SMART_CUE && (comport_SC == null || !comport_SC.IsOpen)) { Invoke(new select_index_delegate(select_index), comport_box_SC, i); } } String status_text = ""; if (comport_PD != null && comport_PD.IsOpen && pocketdetector_worker.IsBusy) { status_text += "Pocket detector: running "; } else { status_text += "Pocket detector: not running "; } if (comport_SC != null && comport_SC.IsOpen && pocketdetector_worker.IsBusy) { status_text += "Smart cue: running "; } else { status_text += "Smart cue: not running "; } Invoke(new status_set_text_delegate(set_text_status_all), status_text); } }