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,7 +74,10 @@
        protected override void Stop()
        {
            client.Close();
            if (client != null && client.Connected)
            {
                client.Close();
            }
        }
        #endregion
@@ -90,30 +95,41 @@
            {
                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)
        {
            int dataLength = client.GetStream().EndRead(ar);
            if (dataLength > 0)
            try
            {
                byte[] data = buffer.Take(dataLength).ToArray();
                int dataLength = client.GetStream().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();
                    AnalyzeData(dataStr);
                    client.GetStream().BeginRead(buffer, 0, buffer.Length, OnDateReceived, 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,7 +189,7 @@
                             for (int i = resultStr.Length - 1; i >= 0; i--)
                             {
                                 newValues.Add(resultStr[i]);
                                 newValues.Add(int.Parse(resultStr[i].ToString()));
                             }
                             monitorHandle.Set();
@@ -248,17 +264,18 @@
            if (isMonitorMsg && !canMonitor)
                return;
            lock (this)
            //lock (this)
            {
                client.GetStream().Write(bytes, 0, bytes.Length);
                if (isWaitReply)
                {
                    replyHandleDict[msg.ID].WaitOne(IConfig.ReplyTimeout);
            }
                    if (replyHandleList.Contains(msg.ID))
                    {
                        throw new ProcessException("反馈数据超时\r\n" + msg.GetDisplayText(), null);
                    }
            if (isWaitReply)
            {
                replyHandleDict[msg.ID].WaitOne(IConfig.ReplyTimeout);
                if (replyHandleList.Contains(msg.ID))
                {
                    throw new ProcessException("反馈数据超时\r\n" + msg.GetDisplayText(), null);
                }
            }
        }
@@ -272,8 +289,9 @@
        AutoResetEvent monitorHandle = new AutoResetEvent(false);
        public List<int> GetMonitorValues(int startAddress, int length)
        {
            scanMsg.ID = SID;
            SendMsg(scanMsg, true, true);
            monitorHandle.WaitOne();
            monitorHandle.WaitOne(IConfig.ReplyTimeout);
            return newValues;
        }
@@ -288,6 +306,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 +333,11 @@
        {
            IConfig.MonitorSetCollection.ForEach(m =>
            {
                if (m.TriggerIndex < 0 || m.TriggerIndex >= tempNew.Count)
                {
                    return;
                }
                int newValue = tempNew[m.TriggerIndex];
                int oldValue = tempOld[m.TriggerIndex];