From 6428fad15dbe79e30a48ffc9aabe31e03a45426c Mon Sep 17 00:00:00 2001
From: patrick <patrick.xu@broconcentric.com>
Date: 星期四, 05 十二月 2019 12:50:13 +0800
Subject: [PATCH] 1. 添加部分log信息 2. 修改部分机器人动作流程 3. 开放OperationConfig的InputData参数

---
 src/Bro.Device.AuboRobot/AuboRobotDriver.cs |   41 ++++++++++++++++++++++++++---------------
 1 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/src/Bro.Device.AuboRobot/AuboRobotDriver.cs b/src/Bro.Device.AuboRobot/AuboRobotDriver.cs
index d2ae516..11f343f 100644
--- a/src/Bro.Device.AuboRobot/AuboRobotDriver.cs
+++ b/src/Bro.Device.AuboRobot/AuboRobotDriver.cs
@@ -82,6 +82,7 @@
         #endregion
 
         TcpClient client = new TcpClient();
+        NetworkStream stream = null;
         byte[] buffer = new byte[1024];
 
         private void OnConnect(IAsyncResult ar)
@@ -89,7 +90,8 @@
             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)
             {
@@ -103,20 +105,21 @@
             }
         }
 
-        private void OnDateReceived(IAsyncResult ar)
+        private void OnDataReceived(IAsyncResult ar)
         {
             try
             {
-                int dataLength = client.GetStream().EndRead(ar);
+                int dataLength = stream.EndRead(ar);
 
                 if (dataLength > 0)
                 {
                     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);
 
-                    client.GetStream().BeginRead(buffer, 0, buffer.Length, OnDateReceived, null);
+                    stream.BeginRead(buffer, 0, buffer.Length, OnDataReceived, null);
                 }
                 else
                 {
@@ -192,7 +195,7 @@
                                  newValues.Add(int.Parse(resultStr[i].ToString()));
                              }
 
-                             monitorHandle.Set();
+                             MonitorHandle.Set();
                          }
                          else
                          {
@@ -232,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);
         }
 
@@ -246,6 +250,7 @@
         }
 
         bool canMonitor = true;
+        object monitorLock = new object();
         public void SendMsg(RobotMsg msg, bool isWaitReply = true, bool isMonitorMsg = false)
         {
             if (isWaitReply)
@@ -256,17 +261,20 @@
 
             byte[] bytes = msg.GetMsgBytes(IConfig.Seperator, IConfig.EndChar);
 
-            if (!isMonitorMsg)
+            lock (monitorLock)
             {
-                canMonitor = false;
-            }
+                if (!isMonitorMsg)
+                {
+                    canMonitor = false;
+                }
 
-            if (isMonitorMsg && !canMonitor)
-                return;
+                if (isMonitorMsg && !canMonitor)
+                    return;
 
-            //lock (this)
-            {
-                client.GetStream().Write(bytes, 0, bytes.Length);
+                //lock (this)
+                {
+                    stream.Write(bytes, 0, bytes.Length);
+                }
             }
 
             if (isWaitReply)
@@ -286,12 +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(IConfig.ReplyTimeout);
+
+            MonitorHandle.WaitOne(IConfig.ReplyTimeout);
 
             return newValues;
         }

--
Gitblit v1.8.0