| | |
| | | |
| | | protected override void Init() |
| | | { |
| | | oldValues = new List<int>(); |
| | | |
| | | if (string.IsNullOrWhiteSpace(IConfig.EndChar)) |
| | | { |
| | | throw new ProcessException("协议文本的结束字符不可为空,请检查配置", null); |
| | |
| | | |
| | | protected override void Stop() |
| | | { |
| | | client.Close(); |
| | | if (client != null && client.Connected) |
| | | { |
| | | client.Close(); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | |
| | | { |
| | | 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()}"); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | for (int i = resultStr.Length - 1; i >= 0; i--) |
| | | { |
| | | newValues.Add(resultStr[i]); |
| | | newValues.Add(int.Parse(resultStr[i].ToString())); |
| | | } |
| | | |
| | | monitorHandle.Set(); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | 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; |
| | | } |
| | |
| | | |
| | | if (newValues == null || newValues.Count == 0) |
| | | continue; |
| | | |
| | | if (oldValues.Count == 0) |
| | | { |
| | | oldValues = newValues.ConvertAll(s => -1).ToList(); |
| | | } |
| | | |
| | | if (oldValues.Count == newValues.Count) |
| | | { |
| | |
| | | { |
| | | IConfig.MonitorSetCollection.ForEach(m => |
| | | { |
| | | if (m.TriggerIndex < 0 || m.TriggerIndex >= tempNew.Count) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | int newValue = tempNew[m.TriggerIndex]; |
| | | int oldValue = tempOld[m.TriggerIndex]; |
| | | |