前面写串口通讯是有界面的,后面的项目感觉串口通讯只是辅助的作用,应该专门写一个不可视的类来做,这样的好处是通讯模块是独立的,要用的时候直接引用就行了。
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 ReceiveDataList = new 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 {
//异步处理,不影响其他的进程,关闭串口也不会出现错误
}));
{
}