当前位置: 首页 > news >正文

C# 中的 SerialPort

 简介

C# 中的 SerialPort 类提供了对串行端口(如 COM 端口)进行通信的功能。通过 SerialPort 类,你可以打开、关闭端口,读取和写入数据以及设置通信参数等。下面是对 SerialPort 类的一些详细解释: 

 创建 SerialPort 对象

SerialPort serialPort = new SerialPort("COM1", 9600);

创建了一个名为 serialPort 的串口对象,并指定了端口名称 "COM1" 和波特率 9600

设置串口参数

serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;

除了在构造函数中设置外,你还可以通过上述属性来设置串口的波特率、奇偶校验位、数据位和停止位等参数。

打开和关闭串口

serialPort.Open();
// 在完成通信后记得关闭串口
serialPort.Close();

使用 Open 方法打开串口,使用 Close 方法关闭串口。

读取数据、写入数据 

 byte[] buffer= new byte[1024]
int bytesRead = serialPort.Read(buffer, 0, buffer.Length);

使用 Read 方法从串口读取数据到指定的缓冲区中。

byte[] data = { 0x01, 0x02, 0x03 };
serialPort.Write(data, 0, data.Length);

使用 Write 方法将数据写入串口发送出去。

using System;
using System.IO.Ports;class SerialCommunication
{static void Main(){// 创建串口对象,并指定串口号和波特率SerialPort serialPort = new SerialPort("COM1", 9600);try{// 打开串口serialPort.Open();// 设置要发送的数据byte iWay = 0x01; // 数据1byte tempLight = 100; // 数据2byte tempEnd = ((byte)(0x24 ^ iWay ^ tempLight)); // 计算校验位byte[] sendtemp = new byte[] { 0x24, iWay, tempLight, tempEnd }; // 组装要发送的数据// 向串口写入数据serialPort.Write(sendtemp, 0, sendtemp.Length);Console.WriteLine("数据发送成功!");}catch (UnauthorizedAccessException e){Console.WriteLine(e.Message);}catch (IOException e){Console.WriteLine(e.Message);}finally{// 关闭串口serialPort.Close();}}
}

事件处理

 

serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{SerialPort sp = (SerialPort)sender;string indata = sp.ReadExisting();Console.WriteLine("Data Received:");Console.WriteLine(indata);
}

你可以注册 DataReceived 事件来处理从串口接收到的数据。

using System;
using System.IO.Ports;class SerialCommunication
{static void Main(){// 创建串口对象,并指定串口号和波特率SerialPort serialPort = new SerialPort("COM1", 9600);try{// 打开串口serialPort.Open();// 设置数据接收事件处理程序serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);Console.WriteLine("开始接收数据,按任意键退出...");Console.ReadKey();}catch (UnauthorizedAccessException e){Console.WriteLine(e.Message);}catch (IOException e){Console.WriteLine(e.Message);}finally{// 关闭串口serialPort.Close();}}private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e){SerialPort sp = (SerialPort)sender;string indata = sp.ReadExisting();Console.WriteLine("接收到的数据: " + indata);}
}

在接收数据的示例中,我们通过 DataReceived 事件来异步接收数据,并在 DataReceivedHandler 事件处理程序中处理接收到的数据。

http://www.lryc.cn/news/228441.html

相关文章:

  • 2022年06月 Python(五级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • YOLO图像识别
  • 2023NewStarCTF
  • 计算机网络的发展及应用
  • K-means(K-均值)算法
  • 网络安全自学
  • 加速mvn下载seatunnel相关jar包
  • 【函数讲解】botorch中的函数 is_non_dominated():用于计算非支配(non-dominated)前沿
  • LeetCode题94,44,145,二叉树的前中后序遍历,非递归
  • Python 框架学习 Django篇 (九) 产品发布、服务部署
  • Git 服务器上的 LFS 下载
  • Canvas和SVG:你应该选择哪一个?
  • openGauss学习笔记-122 openGauss 数据库管理-设置密态等值查询-密态支持函数/存储过程
  • BEVFormer 论文阅读
  • Centos批量删除系统重复进程
  • VUE组件的生命周期
  • 【Git系列】Github指令搜索
  • 【OpenCV】用数组给Mat图像赋值,单/双/三通道 Mat赋值
  • Doris:读取Doris数据的N种方法
  • ceph-deploy bclinux aarch64 ceph 14.2.10
  • 爬虫项目(13):使用lxml抓取相亲信息
  • mysql-数据库三大范式是什么、mysql有哪些索引类型,分别有什么作用 、 事务的特性和隔离级别
  • 微信小程序案例3-2 计算器
  • QT QSplitter
  • 银行支付凭证截图生成器在线,工商邮政农业招商建设,画板+透明标签+图片框
  • 微服务概述
  • LabVIEW中NIPackageManager功能介绍
  • 【C语言】sem_getvalue
  • Linux的shell的$# | fi | 说明
  • C //例 7.12 用选择法对数组中10个整数按由小到大排序。