public PLCConnect(IPEndPoint ip) { IP = ip.Address.ToString(); Port = ip.Port; SocketReconnect(); //发送数据 new System.Threading.Thread(() => { byte[] senddata = null; while (true) { System.Threading.Thread.Sleep(100); lock (SendCache) { if (SendCache.Count > 10) { SendCache.Clear(); } if (SendCache.Count > 0) { senddata = SendCache[0]; SendCache.RemoveAt(0); } } if (senddata == null) { continue; } if (s == null) { SocketReconnect(); System.Threading.Thread.Sleep(5000); } try { s.Send(senddata); IsCommcation = true; } catch { SocketReconnect(); System.Threading.Thread.Sleep(5000); } senddata = null; } }) { IsBackground = true }.Start(); //接收数据 new System.Threading.Thread(() => { byte[] b = new byte[1024]; while (true) { if (s == null) { continue; } if (s.Available == 0) { continue; } int len = 0; try { len = s.Receive(b); } catch { } if (len == 0) { continue; } byte[] recedata = b.Take(len).ToArray(); ReceData_Event?.Invoke(this, recedata); } }) { IsBackground = true }.Start(); }
public IngersollRandConnecter(IPEndPoint as_ip, IPEndPoint as_bindingip = null) { IP = as_ip.Address.ToString(); Port = as_ip.Port; BindingIp = as_bindingip; SocketReconnect(); //发送心跳和问询数据 new System.Threading.Thread(() => { #if 处理心跳 int count_temp = 0; #endif while (true) { System.Threading.Thread.Sleep(200); lock (SendCache) { SendCache.Add(AskTightResult); #if 处理心跳 if (++count_temp >= 5) { SendCache.Add(HeartData); count_temp = 0; } #endif } } }) { IsBackground = true }.Start(); //发送数据 new System.Threading.Thread(() => { byte[] senddata = null; while (true) { System.Threading.Thread.Sleep(100); lock (SendCache) { if (SendCache.Count > 10) { SendCache.Clear(); } if (SendCache.Count > 0) { senddata = SendCache[0]; SendCache.RemoveAt(0); } } if (senddata == null) { continue; } if (s == null) { SocketReconnect(); System.Threading.Thread.Sleep(5000); } try { s.Send(senddata); AppMessage.Add("发送数据", AppMessage.MsgType.英格索兰, false, AppMessage.ImportantEnum.Normal, senddata.ToStandardString(false, senddata.Length)); //LastCommcationTime = DateTime.Now; } catch { SocketReconnect(); System.Threading.Thread.Sleep(5000); } senddata = null; } }) { IsBackground = true }.Start(); //接收数据 new System.Threading.Thread(() => { byte[] recedata = new byte[1024]; while (true) { System.Threading.Thread.Sleep(1); if (s != null && s.Available > 0) { int rece_count_temp = s.Receive(recedata); AppMessage.Add("接收数据", AppMessage.MsgType.英格索兰, false, AppMessage.ImportantEnum.Normal, recedata.ToStandardString(false, rece_count_temp)); if (rece_count_temp == 17) { bool newdata = (recedata[9] == 0 && recedata[10] == 1); if (newdata) { bool ok = (recedata[11] == 0 && recedata[12] == 1); decimal tr = (recedata[13] * 257 + recedata[14]) / 100m; decimal angle = recedata[15] * 257 + recedata[16]; if (tr != 0 || angle != 0) //复位后可能数据都是0,需要排除 { lock (SendCache) //发送复位指令,否则下次又要接收到相同的数据了 { SendCache.Add(Reset); } OnGetData_Event(new TightResult(ok, tr, angle)); //AddReceData(new TightResult(ok, tr, angle)); } } #if 处理心跳 #else LastCommcationTime = DateTime.Now; #endif } #if 处理心跳 else if (rece_count_temp == 11)//心跳数据 { LastCommcationTime = DateTime.Now; } #endif } } }) { IsBackground = true }.Start(); }