我正在做一个串口项目..
我需要从组合框中选择可用的 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;
}
cbCommPorts 是我为组合框取的名称
私人无效 cbCommPorts_SelectedIndexChanged(对象发送者,EventArgs e){ int COM1 = cbCommPorts.SelectedIndex; int COM2 = cbCommPorts.SelectedIndex; int COM3 = cbCommPorts.SelectedIndex;对象 selectedItem = serialPort1.PortName;
MessageBox.Show("COM PORT: " + selectedItem.ToString() + " Selected"); }
我的代码有问题吗?谢谢..