| | |
| | | #endregion |
| | | |
| | | TcpClient client = new TcpClient(); |
| | | NetworkStream stream = null; |
| | | byte[] buffer = new byte[1024]; |
| | | |
| | | private void OnConnect(IAsyncResult ar) |
| | |
| | | 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) |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | 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 |
| | | { |
| | |
| | | newValues.Add(int.Parse(resultStr[i].ToString())); |
| | | } |
| | | |
| | | monitorHandle.Set(); |
| | | MonitorHandle.Set(); |
| | | } |
| | | else |
| | | { |
| | |
| | | |
| | | 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); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | bool canMonitor = true; |
| | | object monitorLock = new object(); |
| | | public void SendMsg(RobotMsg msg, bool isWaitReply = true, bool isMonitorMsg = false) |
| | | { |
| | | if (isWaitReply) |
| | |
| | | |
| | | byte[] bytes = msg.GetMsgBytes(IConfig.Seperator, IConfig.EndChar); |
| | | |
| | | lock (monitorLock) |
| | | { |
| | | if (!isMonitorMsg) |
| | | { |
| | | canMonitor = false; |
| | |
| | | |
| | | //lock (this) |
| | | { |
| | | client.GetStream().Write(bytes, 0, bytes.Length); |
| | | stream.Write(bytes, 0, bytes.Length); |
| | | } |
| | | } |
| | | |
| | | if (isWaitReply) |
| | |
| | | |
| | | 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; |
| | | } |