COM串口通讯基类

2019-08-02--提高串口接收方法效率
2019-06-26--代码优化
2018-11-27--添加字节数组合并方法
2018-08-11--优化串口接收速度
2018-04-28--优化串口接收数据
转载请保留原创地址:http://www.luofenming.com/show.aspx?id=ART2017121500002
    public class SerialCommon
    {
        public string[] portName = SerialPort.GetPortNames();//获取当前计算机的串行端口名称数组
        public  System.IO.Ports.SerialPort MySerialPort = new SerialPort();

        /// <summary>
        /// 初始化串口
        /// </summary>
        /// <param name="PortName">串口号</param>
        /// <param name="BaudRate">波特率</param>
        /// <param name="DataBits">数据位</param>
        /// <param name="stopBits">停止位</param>
        /// <param name="parity">奇偶校验</param>
        public bool InitComm(string PortName, int BaudRate, int DataBits, StopBits stopBits, Parity parity)
        {
            try
            {
                if (MySerialPort.IsOpen)
                {
                    MySerialPort.Close();
                }
                MySerialPort.PortName = PortName;
                MySerialPort.BaudRate = BaudRate;
                MySerialPort.DataBits = DataBits;
                MySerialPort.StopBits = stopBits;
                MySerialPort.Parity = parity;
                //MySerialPort.ReadBufferSize = 1024 * 20;
                //MySerialPort.WriteBufferSize = 1024 * 4;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool OpenPort()
        {
            bool result = true;
            try
            {
                if (!MySerialPort.IsOpen)
                {
                    MySerialPort.Open();
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }
        public bool ClosePort()
        {
            bool result = true;
            try
            {
                if (MySerialPort != null)
                    MySerialPort.Close();
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data"></param>
        public void SendData(byte[] data)
        {
            if (MySerialPort.IsOpen)
            {
                try
                {
                    MySerialPort.DiscardInBuffer();
                    MySerialPort.DiscardOutBuffer();
                    MySerialPort.Write(data, 0, data.Length); 
                }
                catch (Exception ex)
                {
                }
            }
        }
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="_Timeout"></param>
        /// <returns></returns>
        public byte[] ReceiveData(int _Timeout)
        {
            byte[] buffer = new byte[] { };
            DateTime t = DateTime.Now;
            try
            {
                #region 接收到数据中断 或 在规定时间内无任何数据中断
                while (true)
                {
                    if (MySerialPort.BytesToRead > 0) { break; }
                    if (DateTime.Compare(DateTime.Now, t.AddMilliseconds(_Timeout)) > 0) { break; }
                    Thread.Sleep(5);
                } 
                #endregion
                while (true)
                {
                    Thread.Sleep(50);
                    int n = MySerialPort.BytesToRead;
                    byte[] buf = new byte[n];
                    if (n < 1) { break; }//无数据中断退出
                    MySerialPort.Read(buf, 0, n);
                    buffer = UniteArray(buffer, buf);
                    if (DateTime.Compare(DateTime.Now, t.AddMilliseconds(_Timeout)) > 0)
                    {
                        break;
                    }
                }
                return buffer;
            }
            catch (Exception ex)
            {
                return buffer;
            }
        }
        ///合并数组
        public byte[] UniteArray(byte[] ArrayMain, byte[] ArrayItme)
        {
            if (ArrayMain == null) { return ArrayItme; }
            if (ArrayItme == null) { return ArrayMain; }
            byte[] val = new byte[ArrayMain.Length + ArrayItme.Length];
            for (int i = 0; i < val.Length; i++)
            {
                if (i < ArrayMain.Length)
                {
                    val[i] = ArrayMain[i];
                }
                else
                {
                    val[i] = ArrayItme[i - ArrayMain.Length];
                }
            }
            return val;
        }
    }

评论