温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

自定义串口通讯类的实现

发布时间:2020-06-12 05:33:58 来源:网络 阅读:404 作者:dawn0919 栏目:编程语言

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

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;//接收数据事件

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI