public SerialPortView() { InitializeComponent(); try { serialPort = new SerialPortEx(); serialPort.BaudRate = 115200; serialPort.ReadTimeout = 2000; serialPort.WriteTimeout = 1000; serialPort.ReadBufferSize = 8192; serialPort.DtrEnable = true; serialPort.Disposed += new EventHandler(serialPort_Disposed); serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); cBoxSerialPort.Items.Clear(); foreach (object newItem in SerialPortEx.GetPortNamesEx()) { cBoxSerialPort.Items.Add(newItem); } cBoxSerialPort.SelectionChanged += new SelectionChangedEventHandler(cBoxSerialPort_SelectionChanged); if (cBoxSerialPort.Items.Count <= 0) { SerialToggle.IsEnabled = false; } cBoxSerialPort.SelectedIndex = Convert.ToInt32(Settings.Default.SerialPortIndex); } catch (Exception ex) { Console.WriteLine("serial port : {0}", (object)ex); } }
private void cBoxSerialPort_DropDownOpened(object sender, EventArgs e) { object selectedItem = cBoxSerialPort.SelectedItem; int selectedIndex = cBoxSerialPort.SelectedIndex; cBoxSerialPort.SelectionChanged -= new SelectionChangedEventHandler(cBoxSerialPort_SelectionChanged); cBoxSerialPort.Items.Clear(); foreach (object newItem in SerialPortEx.GetPortNamesEx()) { cBoxSerialPort.Items.Add(newItem); } cBoxSerialPort.SelectionChanged += new SelectionChangedEventHandler(cBoxSerialPort_SelectionChanged); cBoxSerialPort.SelectedItem = selectedItem; if (cBoxSerialPort.Items.Count <= 0) { SerialToggle.IsEnabled = false; } else { SerialToggle.IsEnabled = true; } }