C# 使用SerialPort类设计串口通讯程序
来源:互联网 发布:淘宝店铺哪里购买 编辑:程序博客网 时间:2024/04/30 02:42
使用SerialPort类设计串口通讯程序
http://zonghe.17xie.com/book/10536548/27796.html
C#:
1) 添加引用
using System.IO.Ports;
2) 定义SerialPort类实例
private SerialPort SpCom2 = new SpCom("COM2", 9600,Parity.None, 8,StopBits.One);
3) 设置通讯端口号及波特率、数据位、停止位和校验位。
SpCom.PortName="COM1";
SpCom.BaudRate= 9600;
SpCom.Parity= IO.Ports.Parity.None;
SpCom.DataBits= 8;
SpCom.StopBits= IO.Ports.StopBits.One;
或是定义时直接初始化
private SerialPort SpCom2 =newSpCom("COM2", 9600,Parity.None, 8,StopBits.One);
4) 发送数据
SpCom.Write(TextSendData.Text);
5) 添加接受事件
a) 在运行时将事件与事件处理程序相关联(通过委托实现)
SpCom.DataReceived+=newSerialDataReceivedEventHandler(SpCom2_DataReceived);
说明:
SerialDataReceivedEventHandler委托表示将处理SerialPort对象的DataReceived事件的方法
b) 添加事件处理程序(签名一定要一致)
privatevoidSpCom_DataReceived(object sender,SerialDataReceivedEventArgse)
6) 读取数据
string data =SpCom .ReadExisting();
- C# 使用SerialPort类设计串口通讯程序
- 使用SerialPort类设计串口通讯程序
- 使用SerialPort类设计串口通讯程序
- 使用SerialPort类设计串口通讯程序
- 使用SerialPort 类设计串口通讯程序
- 使用SerialPort类设计串口通讯程序
- 使用SerialPort类设计串口通讯程序
- 使用SerialPort类设计串口通讯程序
- 使用SerialPort类设计串口通讯程序
- 使用SerialPort类设计串口通讯程序 (VS2005)[转载]
- 使用SerialPort类设计串口通讯程序 (VS2005)
- 使用SerialPort类设计串口通讯程
- C# 串口通讯:serialport类
- C#串口通信程序SerialPort类
- 传感器(c#2.0)serialPort串口通讯
- C#用SerialPort实现串口通讯
- 使用Java进行串口SerialPort通讯
- serialport 控件 串口通讯
- [Java]读取文件方法大全
- 45+ Free Ebook Download Web Sites of Information Overload
- AMR音频编码器概述及文件格式分析
- vc和matlab混合调试mex文件
- Linux字符设备驱动(二)
- C# 使用SerialPort类设计串口通讯程序
- C#判断数据库中取出的字段值是否为空(NULL)
- 【WebGUI】——IE,Firefox下通用的js触发click事件方法
- 伪随机数生成算法及性能检验
- SQLSEVER中固定排序
- [iphone]Code Sign error: Provisioning profile XXXX can"t be found
- hdu 1857 Word Puzzle
- Mysql数据库查询重复数据
- 樱唯舞的伤感日志推荐:我最喜欢的是你的笑容