C# 串口组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13794376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:48:37  来源:igfitidea点击:

Combo box for serial port

c#combobox

提问by user1670247

I'm doing a project for serial port..

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

I need to select the available com port from the combo box.

我需要从组合框中选择可用的 com 端口。

but i cant get it.. And i needed some help~

但我无法得到它..我需要一些帮助~

Here's my code.. its in C#:

这是我的代码..它在 C# 中:

btn_Open is a button to activate the serial port

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 is the name i got for the Combo Box

    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 PORT: " + selectedItem.ToString() + " Selected");
    }
    
  1. cbCommPorts 是我为 Combo Box 取的名字

    private void cbCommPorts_SelectedIndexChanged(object sender, EventArgs e) { int COM1 = cbCommPorts.SelectedIndex; int COM2 = cbCommPorts.SelectedIndex; int COM3 = cbCommPorts.SelectedIndex; 对象 selectedItem = serialPort1.PortName;

        MessageBox.Show("COM PORT: " + selectedItem.ToString() + " Selected");
    }
    

Is there any problem to my codes? Thanks..

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

采纳答案by TimothyP

Here's one way you might use it with Windows Forms

这是您可以将它与 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....
        }
    }
}

That being said, unless you are maintaining legacy software, it might be a good idea to take a look at WPF. Learning how to use WPF instead of WinForms will ready you for development on Windows 8, Windows Phone etc... And the databinding features make what you're trying to do really easy.

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

回答by ysjia

WPF Code style

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);

回答by Marc Ohoo

private void comboBox_DropDownOpened(object sender, EventArgs e)
{
    string[] ports = SerialPort.GetPortNames();
    comboBox.Items.Clear();
    foreach (string comport in ports)
    {
        comboBox.Items.Add(comport);
    }
}

Addition to ysjiapost.

除了ysjia帖子。

If you not add .Clear(); everytime you click on it it will expand further. You will have duplicates.

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