C#实时判断串口连接状态

写在前面

C#中SerialPort类中没有直接可以判断串口连接状态的方法,在程序运行过程中需要能够实时检测串口的连接状态防止意外断开引起的异常。

以下的方法通过创建多线程进行轮询,通过SerialPort.GetPortNames();这个静态方法不停地获取设备串口号并与创建连接时记录的串口号进行比较。

后台代码

Thread thCheckSerialState= new Thread(new ThreadStart(delegate
{
while (true)
{
    button_Port.Dispatcher.Invoke(new Action(delegate
    {

            string[] gCOM = System.IO.Ports.SerialPort.GetPortNames(); // 获取设备的所有可用串口
            int j = gCOM.Length; // 得到所有可用串口数目
            for (int i = 0; i < j; i++)
            {
                if (gCOM[i] == portName)
                {
                    button_Port.Content = "关闭串口";
                    return;
                }
            }

            button_Port.Content = "打开串口";
  
    }), null);
    Thread.CurrentThread.Join(500);//设定时间500ms
    }
}));
thCheckSerialState.Start();//启动线程

前台代码


<ComboBox x:Name="comboBox_PortNumber" HorizontalAlignment="Left" Margin="76,3,0,0" VerticalAlignment="Top" Width="76" DropDownOpened="comboBox_PortNumber_DropDownOpened" Height="24" FontSize="12" />

<Button x:Name="button_Port" Content="打开串口" HorizontalAlignment="Left" Margin="0,119,-0.4,0" VerticalAlignment="Top" Width="162" Height="27" Click="button_Port_Click"/>



下载

基于上述开发的串口程序
下载链接:https://pan.baidu.com/s/1GjMK5oIuUbuCrIBfkX0noQ
提取码:qw7f

或访问
https://download.csdn.net/download/qq_44348665/12254541

  • 0
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort port1 = new SerialPort(); string InputData = String.Empty; delegate void SetTextCallback(string text); private void Port_Select() {//获取机器中的串口地址 string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { comboBox1.Items.Add(port); } } private void Form1_Load_1(object sender, EventArgs e) { Port_Select(); this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "关闭串口") //当要关闭串口的时候 { port1.DiscardOutBuffer(); port1.DiscardInBuffer(); port1.Close(); button1.Text = "打开串口"; label3.Text = "串口当前状况:未打开"; comboBox1.Enabled = true; comboBox2.Enabled = true; } else if (button1.Text == "打开串口") //当要打开串口的时候 { try { port1.PortName = comboBox1.SelectedItem.ToString(); port1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem); port1.DataBits = 8; port1.RtsEnable = true; port1.Open(); port1.DiscardOutBuffer(); port1.DiscardInBuffer(); button1.Text = "关闭串口"; comboBox1.Enabled = false; comboBox2.Enabled = false; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + " 波特率:" + comboBox2.SelectedItem.ToString() + " 数据位:8 "; } catch { button1.Text = "打开串口"; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + "打开失败"; MessageBox.Show("该串口无法打开"); } } } 资源中部分代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值