我正在为串口做一个项目..
我需要从组合框中选择可用的com端口.
但我无法得到它..我需要一些帮助〜
这是我的代码..它在C#中:
btn_Open是一个激活串口的按钮
private void btnOpen_Click(object sender, EventArgs e)
{
string [] myPort;
int COM1 = cbCommPorts.SelectedIndex;
int COM2 = cbCommPorts.SelectedIndex;
int COM3 = cbCommPorts.SelectedIndex;
Object selectedItem = serialPort1.PortName;
myPort = System.IO.Ports.SerialPort.GetPortNames();
cbCommPorts.Items.AddRange(myPort);
serialPort1.PortName = cbCommPorts.SelectedItem.ToString();
serialPort1.BaudRate = 115200;
if (serialPort1.IsOpen) {
serialPort1.PortName = cbCommPorts.SelectedItem.ToString();
serialPort1.Open();
btnTransmit.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
}
Run Code Online (Sandbox Code Playgroud)
cbCommPorts是我为组合框得到的名字
private void cbCommPorts_SelectedIndexChanged(object sender,EventArgs e){int COM1 = cbCommPorts.SelectedIndex; int COM2 = cbCommPorts.SelectedIndex; int COM3 = cbCommPorts.SelectedIndex; Object selectedItem = serialPort1.PortName;
MessageBox.Show("COM …Run Code Online (Sandbox Code Playgroud)