public TcpComm(int controlpadId, string ip, int port, TcpRecvDelegate tcpRecv) { this.ControlPadId = controlpadId; this.IP = ip; this.Port = port; this.TcpRecv = tcpRecv; SimpleTcp = new SimpleTcpClient(); ControlPadState = new ControlPadState[5]; try { SimpleTcp.Connect(ip, port); SimpleTcp.DataReceived += (sender, msg) => { Decode(msg.Data); }; } catch (Exception) { IsConnection = false; Task.Run(() => { checkState(); }); } }
//构造函数,打开串口 public ControlPad(int controlpadId,TcpRecvDelegate tcpRecv) { try { //读取参数 LoadPara(controlpadId); //查找TCP连接 tcpComm = tcpComms.SingleOrDefault(t => t.ControlPadId == controlpadId); if (tcpComm==null) { tcpComm = new TcpComm(controlpadId,IP, PortNum, tcpRecv); tcpComms.Add(tcpComm); } Task.Run(() => { while (true) { Thread.Sleep(1000); tcpComm.SendData(new byte[] { 0x89, 0x00, 0x0a }); } }); } catch (Exception) { throw; } }