c#中如何刷新串口

本文关键字:刷新 串口 何刷新 | 更新日期: 2023-09-27 18:09:01

我想让我的应用程序可以在c#中刷新串行端口。当端口列表(在ComboBox)是空的,我点击按钮refresh,它的工作完美,并显示活动端口列表。但是如果我断开了Serial Port,我点击refresh按钮,实际上它必须使端口列表(在组合框中)为空,因为串行端口断开了连接。那么如何使当我点击refresh按钮和条件断开时,我们使所有端口列表(在组合框中)为空?

这是我在刷新按钮中的代码:

private void button2_Click_2(object sender, EventArgs e)
{
    if(String.IsNullOrEmpty(cboPort.Text))
    {
        comm.SetPortNameValues(cboPort);
        for (int i = 0; i < cboPort.Items.Count; i++)
        {
            string value = cboPort.GetItemText(cboPort.Items[i]);
            if (String.IsNullOrEmpty(value))
            {
                string a = cboPort.SelectedIndex.ToString();
                return;
            }
            else
            {
                cboPort.SelectedIndex = 0;
            }
        }
     }
     else if ((cboPort.Text) != " " && cboPort.SelectedIndex == -1)
     {
           cboPort.Text = " ";
           return;
     }
}

这是我在setportnamvalues:

中的代码
    public void SetPortNameValues(object obj)
    {
        foreach (string str in SerialPort.GetPortNames())
        {
             ((ComboBox)obj).Items.Add(str);   
        }
    }

我的期望是:

1. i connect serial port 2. i run my app 3. i disconnect serial port 4. i hit refresh 5. final result is port list empty in combobox

谢谢你的帮助和回复,我还是c#的新手。问候!

c#中如何刷新串口

我终于得到了答案。

这是setportnamvalues的修改:

public void SetPortNameValues(object obj)
    {
        string[] ports = SerialPort.GetPortNames(); // load all name of com ports to string
        ((ComboBox)obj).Items.Clear(); //delete previous names in combobox items
        foreach (string port in ports) //add this names to comboboxPort items
        {
            ((ComboBox)obj).Items.Add(port); //if there are some com ports ,select first
        }
        if (((ComboBox)obj).Items.Count > 0)
        {
            ((ComboBox)obj).SelectedIndex = 0;
        }
        else
        {
            ((ComboBox)obj).Text = " "; //if there are no com ports ,write Empty
        }
    }

在这里修改按钮动作:

    private void button2_Click_2(object sender, EventArgs e)
    {
        comm.SetPortNameValues(cboPort);
    }

是的,我终于得到了我想要的。

我的解决方案如下

  1. 初始化COM列表

  2. 为combobox添加serialPort_OnClick事件,以便每当用户单击combobox时,COM项将被重新加载

    private void InitializePortSetting()
    {
        mDateTime = DateTime.Now;
        //1. Setting port list
        // Get a list of serial port names.
        portList = SerialPort.GetPortNames();
        Console.WriteLine("The following serial ports were found:");
        // Display each port name to the console.
        foreach (string port in portList)
        {
            port_name.Items.Add(port);
        }
    }
    private void serialPort_OnClick(object sender, EventArgs e)
    {
        port_name.Items.Clear();
        port_name.Text = "";
        //port_name.Dispose();
        // Get a list of serial port names.
        portList = SerialPort.GetPortNames();
        Console.WriteLine("The following serial ports were found:");
        // Display each port name to the console.
        foreach (string port in portList)
        {
            port_name.Items.Add(port);
        }
    }