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 | 109 +++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 74 insertions(+), 35 deletions(-) diff --git a/src/Bro.Device.AuboRobot/AuboRobotDriver.cs b/src/Bro.Device.AuboRobot/AuboRobotDriver.cs index 7007dc8..11f343f 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,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]; -- Gitblit v1.8.0