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

C#SerialPort如何读取串口数据并显示在TextBox上的更多相关文章

  1. SerialPort如何读取串口数据并显示在TextBox上,多线程委托

    namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...

  2. Javascript实例技巧精选(6)—滚动鼠标中键读取Json数据分页显示网页内容

    >>点击这里下载完整html源码<< 截图如下: 滚动鼠标中键读取Json数据分页显示网页内容,关键的Javascript如下: <script type="t ...

  3. python3 读取串口数据

    python3 读取串口数据 demo import serial import time ser = serial.Serial("COM3",115200,timeout = ...

  4. Qt监控Arduino开关状态(读取串口数据)

    setup.ini配置文件内容 [General] #游戏所在主机IP GameIp1=192.168.1.151 GameIp2=192.168.1.152 GameIp3=192.168.1.15 ...

  5. Django读取Mysql数据并显示在前端

    一.首先按添加网页的步骤添加网页,我的网页名为table.html, app名为web table.html放到相应目录下, froms文件提前写好 修改views.py ? 1 2 3 4 5 6 ...

  6. (5)air202读取串口数据并上传到阿里云显示

    一.首先进行云端设置 根据串口助手显示的信息,以及模块文档说明我们可以知道 其中red和ir是红光LED的原始数据, HR表示心率值, HRvalid是心率是否有效标识, SP02是血氧数值,,SPO ...

  7. VB.net 利用SerialPort进行读取串口操作

    Imports SystemImports System.IO.Ports Public Class Form1 Private Sub Form1_Load(ByVal sender As Syst ...

  8. android 读取串口数据的服务

    2016-09-1813:10:03 继承Service,定义抽象方法onDataReceived,子类通过实现抽象方法获取接收到数据的回调. package com.zrsoft.liftad.se ...

  9. 一种非常巧妙的读取串口数据的方法--C#

    读取不完就一直等待,读完了就立刻走,之前都是设置一个溢出时间,不管是不是早就读取完了都要在这等着,有一定的时间浪费. 注意,用之前要设置好SerialPort类的TimeOut属性:

随机推荐

  1. oracle 学习笔记(三)

    1. SQL(基础查询) 1.1. 基本查询语句 1.1.1. FROM子句 SQL查询语句的语法如下:   SELECT <*, column [alias], -> FROM tabl ...

  2. java 连接接数据库 中的代码 放到配置文件中

    1.DButil.java package com.jobproject.util; import java.sql.Connection;import java.sql.DriverManager; ...

  3. 面向对象Part3

    继承: Extends在英文中的翻译是拓展. 但是java允许多重继承. 一条线.单线. ------------------------------------------------------- ...

  4. Java对象的深拷贝和浅拷贝、集合的交集并集

    http://blog.csdn.net/lian_1988/article/details/45970927 http://www.cnblogs.com/yxnchinahlj/archive/2 ...

  5. 【openStack】Libcloud 如何支持 keystone V3?

    Examples This section includes some examples which show how to use the newly available functionality ...

  6. 高性能MySQL(三):服务器性能剖析

    select * from c LEFT JOIN c100w on c.id=c100w.id; -- 联合查询 show PROFILES; -- 查看查询耗时 select * from c; ...

  7. Coding List

    决定还是用回.net吧,一个人瞎搞比较快,在这里把进展做个简单的记录.

  8. selenium webdriver 建行软键盘输入密码

    driver.get("https://ibsbjstar.ccb.com.cn/app/V5/CN/STY1/login.jsp"); driver.manage().timeo ...

  9. ABAP 订单-交货单-发货过账自动完成 案例

    *&---------------------------------------------------------------------* *& Report  ZSDR006 ...

  10. monkey之三:monkey测试测略(摘抄)

    一.分类 Monkey测试针对不同的对象,不同的目的,采用不同的测略方案. 测试类型分为: 应用程序的稳定性测试和压力测试 测试对象分为: 单个APK和多个APK集合 测试目的分为: 解决问题的测试( ...