C#SerialPort如何读取串口数据并显示在TextBox上
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上的更多相关文章
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...
- Javascript实例技巧精选(6)—滚动鼠标中键读取Json数据分页显示网页内容
>>点击这里下载完整html源码<< 截图如下: 滚动鼠标中键读取Json数据分页显示网页内容,关键的Javascript如下: <script type="t ...
- python3 读取串口数据
python3 读取串口数据 demo import serial import time ser = serial.Serial("COM3",115200,timeout = ...
- Qt监控Arduino开关状态(读取串口数据)
setup.ini配置文件内容 [General] #游戏所在主机IP GameIp1=192.168.1.151 GameIp2=192.168.1.152 GameIp3=192.168.1.15 ...
- Django读取Mysql数据并显示在前端
一.首先按添加网页的步骤添加网页,我的网页名为table.html, app名为web table.html放到相应目录下, froms文件提前写好 修改views.py ? 1 2 3 4 5 6 ...
- (5)air202读取串口数据并上传到阿里云显示
一.首先进行云端设置 根据串口助手显示的信息,以及模块文档说明我们可以知道 其中red和ir是红光LED的原始数据, HR表示心率值, HRvalid是心率是否有效标识, SP02是血氧数值,,SPO ...
- VB.net 利用SerialPort进行读取串口操作
Imports SystemImports System.IO.Ports Public Class Form1 Private Sub Form1_Load(ByVal sender As Syst ...
- android 读取串口数据的服务
2016-09-1813:10:03 继承Service,定义抽象方法onDataReceived,子类通过实现抽象方法获取接收到数据的回调. package com.zrsoft.liftad.se ...
- 一种非常巧妙的读取串口数据的方法--C#
读取不完就一直等待,读完了就立刻走,之前都是设置一个溢出时间,不管是不是早就读取完了都要在这等着,有一定的时间浪费. 注意,用之前要设置好SerialPort类的TimeOut属性:
随机推荐
- html显示缩略小图 无失真图片
<html> <head> <title>我的图片处理</title> <style type="text/css"> ...
- mysql 5.5 修改字符编码
修改/etc/mysql/my.cnf 配置文件: 最后重启mysql 服务,再查看: 编码已经改好了,可以支持中文字符编码了.
- npm提速
解决办法:npm --> cnpm https://npm.taobao.org
- Apache 服务器搭建 总结
安装素材准备:<1>下载jdk <2>下载apache2.0.55 <3>下载tomcat5.5 <4>下载jk(mod_jk-apache-2.0.5 ...
- Python类中super()和__init__()的关系
Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...
- Reverse Core 第一部分 代码逆向技术基础
@date: 2016/10/14 <逆向工程核心原理>笔记 记录书中较重要的知识,方便回顾 ps. 因有一些逆向基础,所以我本来就比较熟悉的知识并未详细记录 第一章 关于逆向工程 目标, ...
- JSP复习整理(二)基本语法
最基础的整理.. 一.语句声明 <%@ page language="java" contentType="text/html; charset=UTF-8&quo ...
- [Project Name] was compiled with optimization - stepping may behave oddly; variables may not be available.
控制台输出的时候显示一串这样的信息:[Project Name] was compiled with optimization - stepping may behave oddly; variabl ...
- adb 的使用
打出log:adb logcat -s fliterName ps:(fliterName就是你的tag)
- ***IT程序员自我修养和情商提升文章
低情商的13个表现 --------------------------------------------------------------------- — THE END —