【问题标题】:Adding Serial Ports to comboBox only if they are not in use! C#仅在未使用时将串行端口添加到组合框! C#
【发布时间】:2023-03-14 04:25:02
【问题描述】:

我在显示可用串行端口的不同 TabPages 上有一些组合框。我可以在组合框中选择一个端口并连接到它以获取数据。现在我希望组合框隐藏已经在使用的端口。最好的方法是什么?

这是组合框下拉菜单上发生的情况:

    string[] portNames = SerialPort.GetPortNames();
    comboBox9.Items.Clear();
    foreach (var portName in portNames)
    {
        //checks if combox already contains same Item.
        if (!comboBox9.Items.Contains(portNames))
        {
            comboBox9.Items.Add(portName);
        }
    }




    private void button1_Click(object sender, EventArgs e)
    {
        //<-- This block ensures that no exceptions happen
        if (_serialPort1 != null && _serialPort1.IsOpen)
            _serialPort1.Close();
        if (_serialPort1 != null)
           _serialPort1.Dispose();
        //<-- End of Block

        if (comboBox2.Text != "")
        {
            _serialPort1 = new SerialPort(comboBox2.Text, BaudRate, Parity.Even, 7, StopBits.Two);       //<-- Creates new SerialPort using the name selected in the combobox
            _serialPort1.DataReceived += SerialPortOnDataReceived;       //<-- this event happens everytime when new data is received by the ComPort

            _serialPort1.Open();     //<-- make the comport listen

            button1.Enabled = false;
            button2.Enabled = true;

【问题讨论】:

  • 您正在 for 循环之前创建组合框的所有项目。这会将所有端口添加到组合框。 SerialPort.GetPortNames() 返回所有端口(空闲端口和正在使用的端口)?
  • 是的,它会返回免费的和正在使用的。
  • 那么你如何决定哪一个是免费的,哪一个是正在使用的?
  • 私有串行端口_serialPort1; _serialPort.IsOpen
  • 好吧..这就是我的理解。您已经在组合框中填充了所有端口。现在,当您再次调用SerialPort.GetPortName() 时,您想检查哪些端口正在使用中,并且您想从组合框中删除这些端口。你能分享一下你首先在组合框中添加所有端口的代码吗?您编写了哪些代码来从组合框中删除正在使用的端口?

标签: c# combobox serial-port


【解决方案1】:

如下改变button_click事件。

private void button1_Click(object sender, EventArgs e)
{
    //<-- This block ensures that no exceptions happen
    if (_serialPort1 != null && _serialPort1.IsOpen)
        _serialPort1.Close();
    if (_serialPort1 != null)
       _serialPort1.Dispose();
    //<-- End of Block
    // Adding port back to the comboBox as it is not open now.
    comboBox.Items.Add(_serialPort1.PortName);

    if (comboBox2.Text != "")
    {
        _serialPort1 = new SerialPort(comboBox2.Text, BaudRate, Parity.Even, 7, StopBits.Two);       //<-- Creates new SerialPort using the name selected in the combobox
        _serialPort1.DataReceived += SerialPortOnDataReceived;       //<-- this event happens everytime when new data is received by the ComPort

        _serialPort1.Open();     //<-- make the comport listen

        //removing port from the comboBox as it is now open and in-use.
        comboBox2.Items.Remove(comboBox2.Text);
        button1.Enabled = false;
        button2.Enabled = true;
 }

这应该可以解决您的问题。

【讨论】:

  • 谢谢!这对我有帮助!我需要这个comboBox.Items.Add(_serialPort1.PortName)
  • 但它只有在我删除组合框下拉事件时才有效......所以组合框中显示的串行端口不是最新的。如何检查一个端口是否打开并且该端口不需要添加到组合框?或者我需要在下拉事件中更改什么?我已经尝试过:` if (_serialPort2.PortName != portName & _serialPort3.PortName != portName & _serialPort4.PortName != portName & _serialPort5.PortName != portName & !comboBox9.Items.Contains(portName)) { comboBox9.Items.添加(端口名称); }`
  • 你能把你的代码放在问题中吗?还分享一些关于代码的更多细节,比如它什么时候被执行,Form_Load,button_Click?如果您也可以在代码和业务用例周围放置一些上下文,那就更好了。分享更多可以解释整个场景的代码也会很有帮助。
  • 好的,我会的!
猜你喜欢
  • 2017-06-13
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
相关资源
最近更新 更多