自定义串口通信类的实现

  前面写串口通信是有界面的,后面的项目感受串口通信只是辅助的做用,应该专门写一个不可视的类来作,这样的好处是通信模块是独立的,要用的时候直接引用就好了。数组

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<byte> ReceiveDataList = new List<byte>();//定义一个泛型数组来接收数据
        string sDataChar;//转换成的字符串
        //声明事件
        public delegate void OnReceiveMsgHandler(string s1);
        public event OnReceiveMsgHandler OnReceiveMsg;//接收数据事件ide

        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();this

        //构造函数
        public SerialPortCommunication()
        {
            //
            //sp.sp_DataReceived += OnReceiveMsg;
        }spa

        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;
        }orm

        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<ReceiveDataByte.Length;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(() => {
                //异步处理,不影响其余的进程,关闭串口也不会出现错误
                }));
            {
        }
自定义串口通信类的实现