| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 | using System;using System.IO.Ports;using System.Text;using Aitex.Core.RT.Event;using Aitex.Core.RT.Log;using MECF.Framework.Common.Utilities;namespace MECF.Framework.Common.Communications{        public class AsyncSerialPort : IDisposable    {        public string PortName { get { return _port.PortName; } set {        {            _port.PortName = value;        } } }        public event Action<string> OnErrorHappened;        public event Action<string> OnDataChanged;        public event Action<byte[]> OnBinaryDataChanged;        private static Object _locker = new Object();        protected SerialPort _port;        private string _buff = "";        public bool EnableLog { get; set; }        private bool _isAsciiMode;        public AsyncSerialPort(string name, int baudRate, int dataBits, Parity parity = Parity.None, StopBits stopBits = StopBits.One, string newline = "\r", bool isAsciiMode=true)        {            _isAsciiMode = isAsciiMode;            _port = new SerialPort();            _port.PortName = name;            _port.BaudRate = baudRate;            _port.DataBits = dataBits;            _port.Parity = parity;            _port.StopBits = stopBits;            _port.RtsEnable = false;            _port.DtrEnable = false;            _port.ReadTimeout = 1000;            _port.WriteTimeout = 1000;            _port.NewLine = newline;            _port.Handshake = Handshake.None;            _port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);            _port.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorReceived);        }        public void Dispose()        {            Close();        }        public bool Open()        {                        if (_port.IsOpen) Close();            try            {                _port.Open();                _port.DiscardInBuffer();                _port.DiscardOutBuffer();                _buff = "";            }            catch (Exception e)            {                string reason = _port.PortName + " port open failed,please check configuration。" + e.Message;                 //ProcessError( reason );                //LOG.Write(reason);                return false;            }            return true;        }        public bool IsOpen()        {            return _port.IsOpen;        }        public bool Close()        {            if (_port.IsOpen)            {                try                {                    _port.Close();                }                catch (Exception e)                {                    string reason = _port.PortName + " port close failed。" + e.Message;                    ProcessError( reason );                    return false;                }            }            return true;        }        //结束符号, 由调用者 负责加上        public bool Write(string msg)        {            try            {                lock (_locker)                {                    if (_port.IsOpen)                    {                        _port.Write(msg);                        if (EnableLog)                        {                            //LOG.Info(string.Format("Communication {0} Send {1} succeeded.", _port.PortName, msg));                        }                                            }                }                return true;            }            catch (Exception e)            {                string reason = string.Format("Communication {0} Send {1} failed. {2}.", _port.PortName, msg, e.Message);                //LOG.Info(reason);                ProcessError( reason );                return false;            }        }        public bool Write(byte[] msg)        {            try            {                lock (_locker)                {                    if (_port.IsOpen)                    {                        _port.Write(msg, 0, msg.Length);                        if (EnableLog)                        {                            //LOG.Info(string.Format("Communication {0} Send {1} succeeded.", _port.PortName, string.Join(" ", Array.ConvertAll(msg, x => x.ToString("X2")))));                        }                    }                }                return true;            }            catch (Exception e)            {                string reason = string.Format("Communication {0} Send {1} failed. {2}.", _port.PortName, string.Join(" ", Array.ConvertAll(msg, x => x.ToString("X2"))), e.Message);                //LOG.Info(reason);                ProcessError(reason);                return false;            }        }        public void DataReceived(object sender, SerialDataReceivedEventArgs e)        {            if (!_port.IsOpen)            {                //LOG.Write($"discard {_port.PortName} received data, but port not open");                return;            }            if (_isAsciiMode)            {                AsciiDataReceived();            }            else            {                BinaryDataReceived();            }        }        private void AsciiDataReceived()        {            string str = _port.ReadExisting(); //字符串方式读            _buff += str;                int index = _buff.LastIndexOf(_port.NewLine, StringComparison.Ordinal);                if (index > 0)                {                    index += _port.NewLine.Length;                    string msg = _buff.Substring(0, index);                    _buff = _buff.Substring(index);                if (EnableLog)                {                    //LOG.Info(string.Format("Communication {0} Receive {1}.", _port.PortName, msg));                }                if (OnDataChanged != null)                    OnDataChanged(msg);            }        }        public void BinaryDataReceived( )        {            byte[] readBuffer = new byte[_port.BytesToRead];            int readCount = _port.Read(readBuffer, 0, readBuffer.Length);            if (readCount == 0)            {                //LOG.Write($"read zero length data, {_port.PortName}");                return;            }            byte[] buffer = new byte[readCount];            Buffer.BlockCopy(readBuffer, 0, buffer, 0, readCount);            if (EnableLog)            {                StringBuilder str = new StringBuilder(512);                Array.ForEach(buffer, x => str.Append(x.ToString("X2") + " "));                //LOG.Info(string.Format("Communication {0} Receive {1}.", _port.PortName, str));            }            if (OnBinaryDataChanged != null)                OnBinaryDataChanged(buffer);        }        void ErrorReceived(object sender, SerialErrorReceivedEventArgs e)        {            string reason = string.Format("Communication {0} {1}.", _port.PortName, e.EventType.ToString());            //LOG.Error(reason);            ProcessError( reason );        }        public void ClearPortBuffer()        {            _port.DiscardInBuffer();            _port.DiscardOutBuffer();        }        void ProcessError(string reason)        {            if (OnErrorHappened != null)                OnErrorHappened(reason);        }    }}
 |