自定义串口通讯类的实现
前面写串口通讯是有界面的,后面的项目感觉串口通讯只是辅助的作用,应该专门写一个不可视的类来做,这样的好处是通讯模块是独立的,要用的时候直接引用就行了。
陵水黎族ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
namespace YBDW
{
class SerialPortCommunication
{
//声明变量
public SerialPort sp = new SerialPort();
public bool isOpen = false;//串口是否被打开
public bool SetSetPropertyOK = false;//属性是否设置好
public bool isDataFormatHex = false;//数据格式是否是16进制
List
string sDataChar;//转换成的字符串
//声明事件
public delegate void OnReceiveMsgHandler(string s1);
public event OnReceiveMsgHandler OnReceiveMsg;//接收数据事件
public struct isCommPortPropertys
{
public string sPort;//串口号
public string sBaudRate;//波特率
public string sDataBit;//数据位
public string sParity;//校验位
public string sStopBit;//停止位
public string sDataFormat;//数据格式
public string sReadTimeout;//超时读取时间
}
public isCommPortPropertys CommPortProperty = new isCommPortPropertys();
//构造函数
public SerialPortCommunication()
{
//
//sp.sp_DataReceived += OnReceiveMsg;
}
public void SetCommPortProperty()
{
//设置串口属性
sp.PortName = CommPortProperty.sPort;
sp.BaudRate = Convert.ToInt32(CommPortProperty.sBaudRate);
sp.DataBits = Convert.ToInt16(CommPortProperty.sDataBit);
switch (CommPortProperty.sParity)
{
case "无":
sp.Parity = Parity.None;
break;
case "奇校验":
sp.Parity = Parity.Odd;
break;
case "偶校验":
sp.Parity = Parity.Even;
break;
default:
sp.Parity = Parity.None;
break;
}
switch (CommPortProperty.sDataFormat)
{
case "ASCII":
isDataFormatHex = false;
break;
case "HEX":
isDataFormatHex = true;
break;
default:
isDataFormatHex = false;
break;
}
sp.ReadTimeout = Convert.ToInt32(CommPortProperty.sReadTimeout);
sp.RtsEnable = true;
//挂接DataReceived数据接收事件,当串口收到数据后触发该事件
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
SetSetPropertyOK = true;
}
public void Close()
{
sp.DataReceived -= sp_DataReceived;
sp.Close();
isOpen = false;
}
public void Start()
{
if (SetSetPropertyOK)
{
try
{
sp.Open();
isOpen = true;
}
catch (Exception e)
{
sp.Close();
isOpen = false;
MessageBox.Show("属性没有设置好!"+e.ToString());
}
}
}
private void sp_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
Thread.Sleep(100);//休眠100毫秒
byte[] ReceiveDataByte = new byte[sp.BytesToRead];//创建接收字节数组
sp.Read(ReceiveDataByte, 0, ReceiveDataByte.Length);//读取接收到的数据
ReceiveDataList.Clear();
sDataChar = "";
ReceiveDataList.AddRange(ReceiveDataByte);
if (isDataFormatHex)
{
//16进制
for (int i=0;i
sDataChar += ReceiveDataByte[i].ToString("X2") + " ";
}
}
else
{
//ASCII
sDataChar = Encoding.Default.GetString(ReceiveDataList.ToArray());
}
if (this.OnReceiveMsg != null) OnReceiveMsg(sDataChar);//通过事件传递接收到的数据
}
public void SendData(string sSendData)
{
bool CanSend = true;
if (string.IsNullOrEmpty(sSendData)) CanSend = false;
if (!isOpen) CanSend = false;
if (CanSend) sp.WriteLine(sSendData);
}
}
}
这样的好处是通讯只负责接收或者发送数据,处理数据有专门的类来完成,代码是相互独立分开的。
引用:
//创建串口对象
SerialPortCommunication YBDWCommPort =new SerialPortCommunication();//串口通讯
//设置串口属性
FrmSetCommPort Frm1 = new FrmSetCommPort();
Frm1.TransmitEvent += Transmit;
if (Frm1.ShowDialog() == DialogResult.OK)
{
YBDWCommPort.CommPortProperty.sPort = DawnCommPortProperty.sPort;//串口号
YBDWCommPort.CommPortProperty.sBaudRate = DawnCommPortProperty.sBaudRate;//波特率
YBDWCommPort.CommPortProperty.sStopBit = DawnCommPortProperty.sStopBit;//停止位
YBDWCommPort.CommPortProperty.sDataBit = DawnCommPortProperty.sDataBit;//数据位
YBDWCommPort.CommPortProperty.sParity = DawnCommPortProperty.sParity;//奇偶校验位
YBDWCommPort.CommPortProperty.sDataFormat = DawnCommPortProperty.sDataFormat;//数据格式
YBDWCommPort.CommPortProperty.sReadTimeout = DawnCommPortProperty.sReadTimeout;//超时读取时间
YBDWCommPort.SetCommPortProperty();
//启动侦听,接收串口数据
YBDWCommPort.OnReceiveMsg += OnReceiveMsg;
YBDWCommPort.Start();
this.button1.Text = "关闭串口";
}
//数据处理
private void OnReceiveMsg(string s1)
{
if (string.IsNullOrEmpty(s1))
{
//跳过
}
else
{
//接收数据的处理
this.BeginInvoke(new Action(() => {
//异步处理,不影响其他的进程,关闭串口也不会出现错误
}));
{
}
网站题目:自定义串口通讯类的实现
路径分享:http://ybzwz.com/article/pdcipi.html