检测是否有可以的端口可用

阅读 19

2024-11-07

private void cmb_port_Click(object sender, EventArgs e)
    {
        try
        {
            this.Invoke(new Action(() =>
            {
                ports = SerialPort.GetPortNames();//获取可用串口
                if (ports.Length > 0)//ports.Length > 0说明有串口可用
                {
                    cmb_port.DataSource = ports;
                }
                else
                {
                    MessageBox.Show("无可用串口,请检查端口是否存在!");
                }
            }));

        }
        catch (Exception ex)
        {
            Log($"获取可用串口失败:{ex.Message}");//日志
        }

    }

精彩评论(0)

0 0 举报