基于C# Winform的串口数据接收
来源:互联网 发布:小黄鸭淘客软件合法吗 编辑:程序博客网 时间:2024/05/20 00:48
很多初学者在接触到串口编程时,不知道如何入手,找了网上一些文章,也是鱼龙混杂,不好学习。
今天,我分享一篇文章,讲述 基于C# Winform的串口数据接收编程(适合大概明白WinForm编程的同学,没有接触过WinForm的同学下载源码学习)
首先添加串口(自行拖拽)
SerialPort serialPort1 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One); //初始化串口设置
创建一个数据接收方法
void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e) { Byte[] InputBuf = new Byte[128]; try { serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead); //读取缓冲区的数据直到“}”即0x7D为结束符 //InputBuf = UnicodeEncoding.Default.GetBytes(strRD); //将得到的数据转换成byte的格式 System.Threading.Thread.Sleep(50); this.Invoke(disp_delegate, InputBuf); } catch (TimeoutException ex) //超时处理 { MessageBox.Show(ex.ToString()); } }
建立一个委托事件:(关于C#委托事件,下文会有参考文章)
public void DispUI(byte[] InputBuf) { //textBox1.Text = Convert.ToString(InputBuf); ASCIIEncoding encoding = new ASCIIEncoding(); richTextBox1.Text = encoding.GetString(InputBuf); }
完整的程序代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO.Ports;namespace WindowsFormsApplication3{ public partial class Form1 : Form { SerialPort serialPort1 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One); //初始化串口设置 public delegate void Displaydelegate(byte[] InputBuf); Byte[] OutputBuf = new Byte[128]; public Displaydelegate disp_delegate; public Form1() { disp_delegate = new Displaydelegate(DispUI); serialPort1.DataReceived += new SerialDataReceivedEventHandler(Comm_DataReceived); InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { if (button1.Text == "打开") { serialPort1.Open(); button1.Text = "关闭"; } else { serialPort1.Close(); button1.Text = "打开"; } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e) { Byte[] InputBuf = new Byte[128]; try { serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead); //读取缓冲区的数据直到“}”即0x7D为结束符 //InputBuf = UnicodeEncoding.Default.GetBytes(strRD); //将得到的数据转换成byte的格式 System.Threading.Thread.Sleep(50); this.Invoke(disp_delegate, InputBuf); } catch (TimeoutException ex) //超时处理 { MessageBox.Show(ex.ToString()); } } public void DispUI(byte[] InputBuf) { //textBox1.Text = Convert.ToString(InputBuf); ASCIIEncoding encoding = new ASCIIEncoding(); richTextBox1.Text = encoding.GetString(InputBuf); } }}
程序运行
最后分享一个虚拟串口工具:vspd(在没有底层硬件的时候,这个工具就派上用场了,结合串口小助手还有自己开发的程序使用)
程序源码:http://download.csdn.net/detail/kevin_iot/9724392
委托事件:http://www.cnblogs.com/hyddd/archive/2009/07/26/1531538.html
0 0
- 基于C# Winform的串口数据接收
- 基于C# Winform的串口数据接收
- 串口接收数据c#
- 串口接收数据c#
- 串口接收数据c#
- C#对串口数据接收的处理
- 串口数据的接收
- C# UDP、串口的数据接收与发送
- 有关c#通过串口接收机器数据的分析
- 基于c#的winform
- C#异步数据接收串口操作类
- C#异步数据接收串口操作类
- C#异步数据接收串口操作类
- C#异步数据接收串口操作类
- C#串口通讯 接收数据问题
- C# 解决串口接收数据不完整
- C#中串口接收数据触发事件
- c#串口数据接收处理大纲
- Oracle 的汉字排序问题
- xcode关于ARC的疑惑
- 后台使用国际化
- 使用 GROW 模型
- LayaAir引擎学习日志13----LayaAir怎么在mac环境下打包成ios的app以及发布的所有流程
- 基于C# Winform的串口数据接收
- Android设计模式——单例模式
- python 实现递归删除文件和文件夹
- Oracle sql loader 导数据时添加序号的三种方法
- 4 种方法识别Linux系统 USB 设备
- Xcode6 SizeClass疑问
- 几个属性修饰的问题retain 和 copy
- 调试STM32 time遇到的问题小结
- Hrbust 1638 新年快乐【对称博弈】