C#中使用SerialPort类实现串口通信
在.NET Framework 2.0中提供了SerialPort类,该类主要实现串口数据通信等。本文章将本人在学习过程中从网络上搜集到的相关信息写出来供大家参考。
下面主要介绍该类的主要属性(表1)和方法(表.2)。
如果需要了解更多的信息请登录http://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport(VS.80).aspx查看。
表1 SerialPort类的常用属性
名 称 | 说 明 |
BaseStream | 获取 SerialPort 对象的基础 Stream 对象 |
BaudRate | 获取或设置串行波特率 |
BreakState | 获取或设置中断信号状态 |
BytesToRead | 获取接收缓冲区中数据的字节数 |
BytesToWrite | 获取发送缓冲区中数据的字节数 |
CDHolding | 获取端口的载波检测行的状态 |
CtsHolding | 获取“可以发送”行的状态 |
DataBits | 获取或设置每个字节的标准数据位长度 |
DiscardNull | 获取或设置一个值,该值指示 Null 字节在端口和接收缓冲区之间传输时是否被忽略 |
DsrHolding | 获取数据设置就绪 (DSR) 信号的状态 |
DtrEnable | 获取或设置一个值,该值在串行通信过程中启用数据终端就绪 (DTR) 信号 |
Encoding | 获取或设置传输前后文本转换的字节编码 |
Handshake | 获取或设置串行端口数据传输的握手协议 |
IsOpen | 获取一个值,该值指示 SerialPort 对象的打开或关闭状态 |
NewLine | 获取或设置用于解释 ReadLine( )和WriteLine( )方法调用结束的值 |
Parity | 获取或设置奇偶校验检查协议 |
名 称 | 说 明 |
ParityReplace | 获取或设置一个字节,该字节在发生奇偶校验错误时替换数据流中的无效字节 |
PortName | 获取或设置通信端口,包括但不限于所有可用的 COM 端口 |
ReadBufferSize | 获取或设置 SerialPort 输入缓冲区的大小 |
ReadTimeout | 获取或设置读取操作未完成时发生超时之前的毫秒数 |
ReceivedBytesThreshold | 获取或设置 DataReceived 事件发生前内部输入缓冲区中的字节数 |
RtsEnable | 获取或设置一个值,该值指示在串行通信中是否启用请求发送 (RTS) 信号 |
StopBits | 获取或设置每个字节的标准停止位数 |
WriteBufferSize | 获取或设置串行端口输出缓冲区的大小 |
WriteTimeout | 获取或设置写入操作未完成时发生超时之前的毫秒数 |
表2 SerialPort类的常用方法
方 法 名 称 | 说 明 |
Close | 关闭端口连接,将 IsOpen 属性设置为False,并释放内部 Stream 对象 |
Open | 打开一个新的串行端口连接 |
Read | 从 SerialPort 输入缓冲区中读取 |
ReadByte | 从 SerialPort 输入缓冲区中同步读取一个字节 |
ReadChar | 从 SerialPort 输入缓冲区中同步读取一个字符 |
ReadLine | 一直读取到输入缓冲区中的 NewLine 值 |
ReadTo | 一直读取到输入缓冲区中指定 value 的字符串 |
Write | 已重载。将数据写入串行端口输出缓冲区 |
WriteLine | 将指定的字符串和 NewLine 值写入输出缓冲区 |
使用SerialPort类的方法:
方法一:
首先要添加
using System.IO;
using System.IO.Ports;
1...在类的内部定义SerialPort com;
2...打开串口
com = new SerialPort();
com.BaudRate = 115200;
com.PortName = "COM1";
com.DataBits = 8;
com.Open();//打开串口
3...发送数据
Byte[] TxData ={1,2,3,4,5,6,7,8 };
com.Write(TxData, 0, 8);
4...接收数据
4.1使用事件接收
this.com.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.OnDataReceived);
private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
4.2使用线程接收
接收数据启动一个线程,使其接收。
在类的内部定义
Thread _readThread;
bool _keepReading;
打开串口后启动线程
_keepReading = true;
_readThread = new Thread(ReadPort);
_readThread.Start();
private void ReadPort()
{
while (_keepReading)
{
if (com.IsOpen)
{
byte[] readBuffer = new byte[com.ReadBufferSize + 1];
try
{
// If there are bytes available on the serial port,
// Read returns up to "count" bytes, but will not block (wait)
// for the remaining bytes. If there are no bytes available
// on the serial port, Read will block until at least one byte
// is available on the port, up until the ReadTimeout milliseconds
// have elapsed, at which time a TimeoutException will be thrown.
int count = com.Read(readBuffer, 0, com.ReadBufferSize);
String SerialIn = System.Text.Encoding.ASCII.GetString(readBuffer, 0, count);
if (count != 0)
//byteToHexStr(readBuffer);
ThreadFunction(byteToHexStr(readBuffer,count));
}
catch (TimeoutException) { }
}
else
{
TimeSpan waitTime = new TimeSpan(0, 0, 0, 0, 50);
Thread.Sleep(waitTime);
}
}
}
方法二:使用C#自带的SerialPor控件。
1...在“工具箱”的“组件”中选择SerialPor控件添加。
2...设置串口并打开
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.Open();
3...写入数据可以使用Write或者下面的函数
serialPort1.WriteLine(str);
4...添加数据接收的事件
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
namespace Ex13_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSent_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.Parity = Parity.Odd;
serialPort1.StopBits = StopBits.Two;
serialPort1.ReadTimeout = 1000;
if (serialPort1.IsOpen == false)
serialPort1.Open();
byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
// serialPort1.Close();
MessageBox.Show("数据发送成功!", "系统提示");
}
private void btnRev_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == false)
serialPort1.Open();
byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
textBox2.Text = Encoding.Unicode.GetString(data);
serialPort1.Close();
MessageBox.Show("数据接收成功!", "系统提示");
}
}
}
运行结果:
本文来源:http://blog.csdn.net/goodshot/article/details/7764081