C#SerialPort如何读取串口数据并显示在TextBox上
2016-10-14 17:59
543 查看
SerialPort中串口数据的读取与写入有较大的不同。由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取。一、线程实时读串口;二、事件触发方式实现。
由于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据多少个时才触发DataReceived事件,默认为1。
另外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。如下:
SerialPort spSend;//spSend,spReceive用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据
SerialPort spReceive; //spReceive接受数据
TextBox txtSend; //发送区
TextBox txtReceive; //接受区
Button btnSend; //数据发送按钮
delegate void UpdateTextEventHandler(string text);//委托,此为重点
UpdateTextEventHandler updateText;
public void InitClient() //窗体控件已经初始化
{
updateText = new UpdateTextEventHandler(UpdateTextBox);//实例化委托对象
spSend.Open(); //SerialPort对象在程序结束前必须关闭,在此说明
spReceive.DataReceived += newPorts.SerialDataReceivedEventHandler(spReceive_DataReceived);
spReceive.Open();
}
public void btnSend_Click(object sender,EventArgse)
{
spSend.WriteLine(txtSend.Text);
}
public void spReceive_DataReceived(objectsender,Ports.SerialDataReceivedEventArgs e)
{
//byte[] readBuffer = newbyte[spReceive.ReadBufferSize];
//spReceive.Read(readBuffer, 0,readBuffer.Length);
//this.Invoke(updateText, new string[] {Encoding.Unicode.GetString(readBuffer) });
string readString = spReceive.ReadExisting();
this.Invoke(updateText,new string[]{readString});
}
private void UpdateTextBox(string text)
{
txtReceive.Text = text;
}
转自:http://www.cnblogs.com/zhb3373/articles/5116674.html
由于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据多少个时才触发DataReceived事件,默认为1。
另外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。如下:
SerialPort spSend;//spSend,spReceive用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据
SerialPort spReceive; //spReceive接受数据
TextBox txtSend; //发送区
TextBox txtReceive; //接受区
Button btnSend; //数据发送按钮
delegate void UpdateTextEventHandler(string text);//委托,此为重点
UpdateTextEventHandler updateText;
public void InitClient() //窗体控件已经初始化
{
updateText = new UpdateTextEventHandler(UpdateTextBox);//实例化委托对象
spSend.Open(); //SerialPort对象在程序结束前必须关闭,在此说明
spReceive.DataReceived += newPorts.SerialDataReceivedEventHandler(spReceive_DataReceived);
spReceive.Open();
}
public void btnSend_Click(object sender,EventArgse)
{
spSend.WriteLine(txtSend.Text);
}
public void spReceive_DataReceived(objectsender,Ports.SerialDataReceivedEventArgs e)
{
//byte[] readBuffer = newbyte[spReceive.ReadBufferSize];
//spReceive.Read(readBuffer, 0,readBuffer.Length);
//this.Invoke(updateText, new string[] {Encoding.Unicode.GetString(readBuffer) });
string readString = spReceive.ReadExisting();
this.Invoke(updateText,new string[]{readString});
}
private void UpdateTextBox(string text)
{
txtReceive.Text = text;
}
转自:http://www.cnblogs.com/zhb3373/articles/5116674.html
相关文章推荐
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
- C#2.0中,SerialPort如何读取串口数据并显示在TextBox上
- C#2.0中,SerialPort如何读取串口数据并显示在TextBox上
- c# serialport读取不限数量的16进制数据
- 使用QSerialport读取串口数据并处理
- C# serialPort 串口 获取扫描抢数据
- c# serialport读取不限数量的16进制数据
- [ASP.NET教程] C#中如何实现数据拖动?(拖动图片,到TextBox,并显示)
- C#中如何实现数据拖动?(拖动图片,到TextBox,并显示)
- C# 串口接收数据中serialPort.close()死锁的实例
- c#中接收16进制串口数据(com), 在textbox显示
- c#读取SqlServer数据显示到textbox中
- C#串口SerialPort常用属性方法
- C#如何从文本文件读取信息并使用ArrayList类向用户显示该信息
- C# 如何在textbox里显示txt文件的内容
- 关于Qt5 SerialPort串口传输丢失数据问题。
- 如何解决从mysql中读取数据时显示为乱吗的问题.
- SerialPort同步和异步数据读取