From ff18fa3c007656bd37cad91fac9a9cb03f4070b8 Mon Sep 17 00:00:00 2001
From: patrick <patrick.xu@broconcentric.com>
Date: 星期日, 20 十月 2019 14:13:16 +0800
Subject: [PATCH] 1. 修改SeerAGV驱动通信代码 2. 修改机器人通信部分代码 3. 取消原有流程任务队列模式,使用即时方法调用。

---
 src/Bro.Device.AuboRobot/AuboRobotDriver.cs |   78 ++++++++++++++++++++++++++------------
 1 files changed, 53 insertions(+), 25 deletions(-)

diff --git a/src/Bro.Device.AuboRobot/AuboRobotDriver.cs b/src/Bro.Device.AuboRobot/AuboRobotDriver.cs
index 7007dc8..d2ae516 100644
--- a/src/Bro.Device.AuboRobot/AuboRobotDriver.cs
+++ b/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];
 

--
Gitblit v1.8.0