=波波日志 > Asp.Net/C#/WCF > C#2.0中,SerialPort如何读取串口数据并显示在TextBox上=
[转]C#2.0中,SerialPort如何读取串口数据并显示在TextBox上
SerialPort中串口数据的读取与写入有较大的不同。由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取。一、线程实时读串口;二、事件触发方式实现。
由于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据多少个时才触发DataReceived事件,默认为1。
另外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。如下:
http://www.cnblogs.com/showlie/articles/751737.html
由于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据多少个时才触发DataReceived事件,默认为1。
另外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。如下:
+展开
-C#
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 += new Ports.SerialDataReceivedEventHandler(spReceive_DataReceived);
spReceive.Open();
}
public void btnSend_Click(object sender,EventArgs e)
{
spSend.WriteLine(txtSend.Text);
}
public void spReceive_DataReceived(object sender,Ports.SerialDataReceivedEventArgs e)
{
//byte[] readBuffer = new byte[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;
}
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 += new Ports.SerialDataReceivedEventHandler(spReceive_DataReceived);
spReceive.Open();
}
public void btnSend_Click(object sender,EventArgs e)
{
spSend.WriteLine(txtSend.Text);
}
public void spReceive_DataReceived(object sender,Ports.SerialDataReceivedEventArgs e)
{
//byte[] readBuffer = new byte[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/showlie/articles/751737.html
类别:Asp.Net/C#/WCF 作者:转载 日期:2010-05-31 【评论:1】
- ivan
日期:2011-10-10 18:22:53 IP:218.13.*.*
我读取数据出来是乱码的!管理员回复(2011-10-13 11:19:23)
你试试使用字节流来获取字符串,可能是你手机编码和.net使用默认编码不一致导致乱码了。+展开-C#public void spReceive_DataReceived(object sender,Ports.SerialDataReceivedEventArgs e)
{
byte[] readBuffer = new byte[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});
}
发表留言
百度赞助
同类热门博文
- ·IE里Cookie跨域不能..
- ·去掉隐藏asp.net编译..
- ·解决asp.net验证视图..
- ·找不到System.Web.S..
- ·web服务因URL意外地..
- ·用C#编写ActiveX控件..
- ·用C#编写ActiveX控件..
- ·asp.net 串口操作
博格Tag
- flash/flex/fcs/AIR(752)
- Asp.Net/C#/WCF(598)
- 操作系统及应用软件(376)
- JavaScript/Ajax(331)
- SQL及数据库(134)
- 黑客技术(115)
- Asp/VBScript(111)
- HTML/WML/CSS兼容/XML(102)
- PHP/apache/Perl(96)
- 网站排名及优化(96)
- 其他(75)
- showbo日志(66)
- lucene.net/分词技术(33)
- 计算机网络(26)
- 机械重工(26)
- C#设计模式(25)
- Google Maps开发(17)
- 日语学习(15)
- Canvas/VML/SVG(13)
- linux(11)
- 游戏开发(8)
- 正则表达式(5)
- Jsp/Java(4)
最新博文
- ·详解SqlConnection连..
- ·C#实现的html内容截..
- ·asp.net web.config..
- ·asp.net<%--注释--%..
- ·ASP.NET环境配置常见..
- ·asp.net防止图片盗链..
- ·Session.Abandon的使..
- ·asp中缓存cache技术..
随机博文
