3

我正在做一个串口项目..

我需要从组合框中选择可用的 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;
        }
  1. 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");
    }
    

我的代码有问题吗?谢谢..

4

3 回答 3

6

这是您可以将其与 Windows 窗体一起使用的一种方式

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
    }

    void Form1_Load(object sender, EventArgs e)
    {
        var ports = SerialPort.GetPortNames();
        cmbSerialPorts.DataSource = ports;
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        if (cmbSerialPorts.SelectedIndex > -1)
        {
            MessageBox.Show(String.Format("You selected port '{0}'", cmbSerialPorts.SelectedItem));
            Connect(cmbSerialPorts.SelectedItem.ToString());
        }
        else
        {
            MessageBox.Show("Please select a port first");
        }
    }

    private void Connect(string portName)
    {
        var port = new SerialPort(portName);
        if (!port.IsOpen)
        {
            port.BaudRate = 19200;
            port.Open();
            //Continue here....
        }
    }
}

话虽如此,除非您正在维护旧软件,否则看看 WPF 可能是个好主意。学习如何使用 WPF 而不是 WinForms 将为您在 Windows 8、Windows Phone 等上进行开发做好准备……而且数据绑定功能使您尝试做的事情变得非常容易。

于 2012-12-10T03:14:29.347 回答
0

WPF 代码风格

public void comboBox_DropDownOpened(object sender, EventArgs e)
    {   
        string[] ports = SerialPort.GetPortNames();          
        foreach ( string comport in ports)
            {
                comboBox.Items.Add(comport);
            }
    }
.... /*Two control item combobox&button, comboxbox's item is COM port and   It's first argument of Function 「System.IO.Ports.SerialPort 」. Using (comboBox.text) */
private void button1_Click(object sender, RoutedEventArgs e)
    {

        System.IO.Ports.SerialPort Port = new SerialPort
            ((comboBox.Text), 115200, Parity.None, 8, StopBits.One);
        try
        {
            Port.Open();
            Port.Write(cmdByteArray, 0, cmdByteArray.Length );
        }
        catch { Exception ex; }
        Port.Read(readbyte, 0, readbyte.Length);
于 2016-06-16T09:40:32.320 回答
0
private void comboBox_DropDownOpened(object sender, EventArgs e)
{
    string[] ports = SerialPort.GetPortNames();
    comboBox.Items.Clear();
    foreach (string comport in ports)
    {
        comboBox.Items.Add(comport);
    }
}

除了ysjia帖子。

如果不添加 .Clear(); 每次点击它都会进一步展开。你会有重复的。

于 2016-12-07T11:22:42.267 回答