基于C# Winform的串口数据接收

63 篇文章 2 订阅

https://blog.csdn.net/woolnil/article/details/78048272
今天,我分享一篇文章,讲述 基于C# Winform的串口数据接收编程(适合大概明白WinForm编程的同学,没有接触过WinForm的同学下载源码学习)


首先添加串口(自行拖拽)

[csharp]  view plain  copy
  1. SerialPort serialPort1 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);      //初始化串口设置  

创建一个数据接收方法

[csharp]  view plain  copy
  1. void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e)  
  2.         {  
  3.   
  4.             Byte[] InputBuf = new Byte[128];  
  5.   
  6.             try  
  7.             {  
  8.                 serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);                                //读取缓冲区的数据直到“}”即0x7D为结束符  
  9.                 //InputBuf = UnicodeEncoding.Default.GetBytes(strRD);             //将得到的数据转换成byte的格式  
  10.                 System.Threading.Thread.Sleep(50);  
  11.                 this.Invoke(disp_delegate, InputBuf);  
  12.   
  13.             }  
  14.             catch (TimeoutException ex)         //超时处理  
  15.             {  
  16.                 MessageBox.Show(ex.ToString());  
  17.             }  
  18.         }  

建立一个委托事件:(关于C#委托事件,下文会有参考文章)

[csharp]  view plain  copy
  1. public void DispUI(byte[] InputBuf)  
  2. {  
  3.     //textBox1.Text = Convert.ToString(InputBuf);  
  4.   
  5.     ASCIIEncoding encoding = new ASCIIEncoding();  
  6.     richTextBox1.Text = encoding.GetString(InputBuf);  
  7. }  

完整的程序代码:

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO.Ports;  
  10.   
  11. namespace WindowsFormsApplication3  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         SerialPort serialPort1 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);      //初始化串口设置  
  16.         public delegate void Displaydelegate(byte[] InputBuf);  
  17.         Byte[] OutputBuf = new Byte[128];  
  18.         public Displaydelegate disp_delegate;  
  19.   
  20.         public Form1()  
  21.         {  
  22.             disp_delegate = new Displaydelegate(DispUI);  
  23.             serialPort1.DataReceived += new SerialDataReceivedEventHandler(Comm_DataReceived);  
  24.   
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         private void button1_Click(object sender, EventArgs e)  
  29.         {  
  30.             try  
  31.             {  
  32.                 if (button1.Text == "打开")  
  33.                 {  
  34.                     serialPort1.Open();  
  35.                     button1.Text = "关闭";  
  36.                 }  
  37.                 else  
  38.                 {  
  39.                     serialPort1.Close();  
  40.                     button1.Text = "打开";  
  41.                 }  
  42.   
  43.             }  
  44.             catch (Exception ex)  
  45.             {  
  46.                 MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  47.                 return;  
  48.             }  
  49.         }  
  50.   
  51.         void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e)  
  52.         {  
  53.   
  54.             Byte[] InputBuf = new Byte[128];  
  55.   
  56.             try  
  57.             {  
  58.                 serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);                                //读取缓冲区的数据直到“}”即0x7D为结束符  
  59.                 //InputBuf = UnicodeEncoding.Default.GetBytes(strRD);             //将得到的数据转换成byte的格式  
  60.                 System.Threading.Thread.Sleep(50);  
  61.                 this.Invoke(disp_delegate, InputBuf);  
  62.   
  63.             }  
  64.             catch (TimeoutException ex)         //超时处理  
  65.             {  
  66.                 MessageBox.Show(ex.ToString());  
  67.             }  
  68.         }  
  69.   
  70.         public void DispUI(byte[] InputBuf)  
  71.         {  
  72.             //textBox1.Text = Convert.ToString(InputBuf);  
  73.   
  74.             ASCIIEncoding encoding = new ASCIIEncoding();  
  75.             richTextBox1.Text = encoding.GetString(InputBuf);  
  76.         }  
  77.     }  
  78. }  
  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值