src/Bro.Device.AuboRobot/AuboRobotDriver.cs
@@ -34,6 +34,8 @@
        protected override void Init()
        {
            oldValues = new List<int>();
            if (string.IsNullOrWhiteSpace(IConfig.EndChar))
            {
                throw new ProcessException("协议文本的结束字符不可为空,请检查配置", null);
@@ -72,11 +74,15 @@
        protected override void Stop()
        {
            client.Close();
            if (client != null && client.Connected)
            {
                client.Close();
            }
        }
        #endregion
        TcpClient client = new TcpClient();
        NetworkStream stream = null;
        byte[] buffer = new byte[1024];
        private void OnConnect(IAsyncResult ar)
@@ -84,36 +90,49 @@
            try
            {
                client.EndConnect(ar);
                client.GetStream().BeginRead(buffer, 0, buffer.Length, OnDateReceived, null);
                stream = client.GetStream();
                stream.BeginRead(buffer, 0, buffer.Length, OnDataReceived, null);
            }
            catch (Exception ex)
            {
                OnLog?.Invoke(DateTime.Now, this, ex.GetExceptionMessage());
                if (client != null && client.Connected)
                {
                    client.Close();
                }
                client.BeginConnect(IPAddress.Parse(IConfig.RobotIP), IConfig.RobotPort, OnConnect, null);
            }
        }
        private void OnDateReceived(IAsyncResult ar)
        private void OnDataReceived(IAsyncResult ar)
        {
            int dataLength = client.GetStream().EndRead(ar);
            if (dataLength > 0)
            try
            {
                byte[] data = buffer.Take(dataLength).ToArray();
                int dataLength = stream.EndRead(ar);
                string dataStr = System.Text.Encoding.ASCII.GetString(data).Trim();
                AnalyzeData(dataStr);
                client.GetStream().BeginRead(buffer, 0, buffer.Length, OnDateReceived, null);
            }
            else
            {
                if (!client.Connected)
                if (dataLength > 0)
                {
                    OnLog?.Invoke(DateTime.Now, this, "返回空数据,连接中断");
                    client.BeginConnect(IPAddress.Parse(IConfig.RobotIP), IConfig.RobotPort, OnConnect, null);
                    byte[] data = buffer.Take(dataLength).ToArray();
                    string dataStr = System.Text.Encoding.ASCII.GetString(data).Trim();
                    //OnLog?.BeginInvoke(DateTime.Now, this, $"{Name}接收数据:{dataStr}", null, null);
                    AnalyzeData(dataStr);
                    stream.BeginRead(buffer, 0, buffer.Length, OnDataReceived, null);
                }
                else
                {
                    if (!client.Connected)
                    {
                        OnLog?.Invoke(DateTime.Now, this, "返回空数据,连接中断");
                        client.BeginConnect(IPAddress.Parse(IConfig.RobotIP), IConfig.RobotPort, OnConnect, null);
                    }
                }
            }
            catch (Exception ex)
            {
                OnLog?.Invoke(DateTime.Now, this, $"{Name}数据接收异常:{ex.GetExceptionMessage()}");
            }
        }
@@ -173,10 +192,10 @@
                             for (int i = resultStr.Length - 1; i >= 0; i--)
                             {
                                 newValues.Add(resultStr[i]);
                                 newValues.Add(int.Parse(resultStr[i].ToString()));
                             }
                             monitorHandle.Set();
                             MonitorHandle.Set();
                         }
                         else
                         {
@@ -216,6 +235,7 @@
            msg.Datas = new List<string>((paras ?? new List<float>()).ConvertAll(i => i.ToString()));
            OnLog?.BeginInvoke(DateTime.Now, this, $"{Name}发送指令:{msg.GetDisplayText()}", null, null);
            SendMsg(msg, true);
        }
@@ -230,6 +250,7 @@
        }
        bool canMonitor = true;
        object monitorLock = new object();
        public void SendMsg(RobotMsg msg, bool isWaitReply = true, bool isMonitorMsg = false)
        {
            if (isWaitReply)
@@ -240,25 +261,29 @@
            byte[] bytes = msg.GetMsgBytes(IConfig.Seperator, IConfig.EndChar);
            if (!isMonitorMsg)
            lock (monitorLock)
            {
                canMonitor = false;
                if (!isMonitorMsg)
                {
                    canMonitor = false;
                }
                if (isMonitorMsg && !canMonitor)
                    return;
                //lock (this)
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
            if (isMonitorMsg && !canMonitor)
                return;
            lock (this)
            if (isWaitReply)
            {
                client.GetStream().Write(bytes, 0, bytes.Length);
                if (isWaitReply)
                {
                    replyHandleDict[msg.ID].WaitOne(IConfig.ReplyTimeout);
                replyHandleDict[msg.ID].WaitOne(IConfig.ReplyTimeout);
                    if (replyHandleList.Contains(msg.ID))
                    {
                        throw new ProcessException("反馈数据超时\r\n" + msg.GetDisplayText(), null);
                    }
                if (replyHandleList.Contains(msg.ID))
                {
                    throw new ProcessException("反馈数据超时\r\n" + msg.GetDisplayText(), null);
                }
            }
        }
@@ -269,11 +294,15 @@
        protected List<int> oldValues = new List<int>();
        List<int> newValues = new List<int>();
        AutoResetEvent monitorHandle = new AutoResetEvent(false);
        public ManualResetEvent MonitorHandle { get; set; } = new ManualResetEvent(false);
        //public ManualResetEvent IOChangedHandle { get; set; } = new ManualResetEvent(true);
        public List<int> GetMonitorValues(int startAddress, int length)
        {
            MonitorHandle.Reset();
            scanMsg.ID = SID;
            SendMsg(scanMsg, true, true);
            monitorHandle.WaitOne();
            MonitorHandle.WaitOne(IConfig.ReplyTimeout);
            return newValues;
        }
@@ -288,6 +317,11 @@
                    if (newValues == null || newValues.Count == 0)
                        continue;
                    if (oldValues.Count == 0)
                    {
                        oldValues = newValues.ConvertAll(s => -1).ToList();
                    }
                    if (oldValues.Count == newValues.Count)
                    {
@@ -310,6 +344,11 @@
        {
            IConfig.MonitorSetCollection.ForEach(m =>
            {
                if (m.TriggerIndex < 0 || m.TriggerIndex >= tempNew.Count)
                {
                    return;
                }
                int newValue = tempNew[m.TriggerIndex];
                int oldValue = tempOld[m.TriggerIndex];