void GetSerialPorts() { Ports = Model_SerialPort.SerialPortMain.GetPortNames(); if (Ports.Length != 0) { this.comboBox_COM.Items.Clear(); this.comboBox_COM.Items.AddRange(Ports); Model_SerialPort.SerialPortMain[] temp_ports = new Model_SerialPort.SerialPortMain[Ports.Length]; for (int i = 0; i < temp_ports.Length; i++) { temp_ports[i] = new Model_SerialPort.SerialPortMain(); temp_ports[i].PortName = Ports[i]; temp_ports[i].Event_Show += this.textBox1.Show;//装载事件 this.SerialPorts.Add(temp_ports[i]); } this.comboBox_COM.SelectedIndex = 0; } }
private void comboBox_COM_SelectedIndexChanged(object sender, EventArgs e) { foreach (Model_SerialPort.SerialPortMain i in SerialPorts) { if (i.PortName == this.comboBox_COM.SelectedItem.ToString()) { CurrentPort = i; if (i.IsOpen) { this.button_Open.Text = "Close"; this.toolStripStatusLabel1.Text = string.Format("端口:{0} 状态:打开 波特率:{1}", CurrentPort.PortName, CurrentPort.BaudRate.ToString()); } else { this.button_Open.Text = "Open"; this.toolStripStatusLabel1.Text = string.Format("端口:{0} 状态:关闭", CurrentPort.PortName); } } } }