当前位置:编程学习 > C#/ASP.NET >>

获取串口数据 (C#)

2. 效果图 如下

 

3. 关键代码
[csharp] 
private void buttonOpenClose_Click(object sender, EventArgs e) 
      { 
          try 
          { 
              labmsg.Text = ""; 
              //根据当前串口对象,来判断操作 
              if (comm.IsOpen) 
              { 
                  //打开时点击,则关闭串口 
                  comm.Close(); 
              } 
              else 
              { 
                  //关闭时点击,则设置好端口,波特率后打开 
                  comm.PortName = comboPortName.Text; 
                  comm.BaudRate = int.Parse(comboBaudrate.Text); 
                  try 
                  { 
                      comm.Open(); 
                  } 
                  catch (Exception ex) 
                  { 
                      //捕获到异常信息,创建一个新的comm对象,之前的不能用了。 
                      comm = new SerialPort(); 
                      //现实异常信息给客户。 
                      MessageBox.Show(ex.Message); 
                  } 
              } 
              //设置按钮的状态 
              buttonOpenClose.Text = comm.IsOpen ? "关闭串口" : "打开串口"; 
              buttonSend.Enabled = comm.IsOpen; 
          } 
          catch (Exception ex) 
          { 
 
              labmsg.Text = "ERR"; 
          } 
      } 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,