public bool SendData(List <byte> bytelist, FunctionCode fc) { try { Tcpsocket.Send(bytelist.ToArray());//发送字节 if (fc == FunctionCode.Write) { Thread.Sleep(60); } int offlinecount = 0; int allheadleftlengh = 6; int receivedlengh = 0; byte[] bufferhead = new byte[6]; while (allheadleftlengh - receivedlengh > 0) { byte[] buffertemp = new byte[allheadleftlengh - receivedlengh]; int lengh = Tcpsocket.Receive(buffertemp); if (lengh <= 0) { if (offlinecount == 3) { throw new Exception("Socket 错误!"); } offlinecount += 1; Thread.Sleep(1000 * 2); } Buffer.BlockCopy(buffertemp, 0, bufferhead, receivedlengh, lengh); receivedlengh += lengh; } offlinecount = 0; receivedlengh = 0; int allcontentleftlengh = int.Parse(Convert.ToString(bufferhead[5], 10)); byte[] buffercontent = new byte[allcontentleftlengh]; while (allcontentleftlengh - receivedlengh > 0) { byte[] buffertemp = new byte[allcontentleftlengh - receivedlengh]; int lengh = Tcpsocket.Receive(buffertemp); if (lengh <= 0) { if (offlinecount == 3) { throw new Exception("Socket 错误!"); } offlinecount += 1; Thread.Sleep(1000 * 2); } Buffer.BlockCopy(buffertemp, 0, buffercontent, receivedlengh, lengh); receivedlengh += lengh; } if (fc == FunctionCode.Read && buffercontent[1] == 0x0f) { IODeviceInfo io = new IODeviceInfo(); io.ID = this.DeviceID; //解析 //1-8口 char[] bytestr = Convert.ToString(buffercontent[4], 2).PadLeft(8, '0').ToArray(); for (int i = 0; i < 8; i++) { IOPortInfo ioport = new IOPortInfo(); ioport.PortNo = 8 - i; ioport.PortState = bytestr[i] == '1' ? 1 : 0; io.DIPortList.Add(ioport); } //9-16口 bytestr = Convert.ToString(buffercontent[3], 2).PadLeft(8, '0').ToArray(); for (int i = 0; i < 8; i++) { IOPortInfo ioport = new IOPortInfo(); ioport.PortNo = 16 - i; ioport.PortState = bytestr[i] == '1' ? 1 : 0; io.DIPortList.Add(ioport); } //17-24口 bytestr = Convert.ToString(buffercontent[6], 2).PadLeft(8, '0').ToArray(); for (int i = 0; i < 8; i++) { IOPortInfo ioport = new IOPortInfo(); ioport.PortNo = 24 - i; ioport.PortState = bytestr[i] == '1' ? 1 : 0; io.DIPortList.Add(ioport); } //25-32口 bytestr = Convert.ToString(buffercontent[5], 2).PadLeft(8, '0').ToArray(); for (int i = 0; i < 8; i++) { IOPortInfo ioport = new IOPortInfo(); ioport.PortNo = 32 - i; ioport.PortState = bytestr[i] == '1' ? 1 : 0; io.DIPortList.Add(ioport); } DelegateState.InvokeIOFeedBackEvent(io); LastRecTime = DateTime.Now; return(true); } if (fc == FunctionCode.Write && buffercontent[1] == 0x0f) { LastRecTime = DateTime.Now; return(true); } return(false); } catch (Exception ex) { return(false); } }
/// <summary> /// 处理命令反馈 /// </summary> public bool GetCallBack() { try { int offlinecount = 0; int allheadleftlengh = 6; int receivedlengh = 0; byte[] bufferhead = new byte[6]; while (allheadleftlengh - receivedlengh > 0) { byte[] buffertemp = new byte[allheadleftlengh - receivedlengh]; int lengh = Tcpsocket.Receive(buffertemp); if (lengh <= 0) { if (offlinecount == 3) { throw new Exception("Socket 错误!"); } offlinecount += 1; Thread.Sleep(1000 * 2); } Buffer.BlockCopy(buffertemp, 0, bufferhead, receivedlengh, lengh); receivedlengh += lengh; } offlinecount = 0; receivedlengh = 0; int allcontentleftlengh = int.Parse(Convert.ToString(bufferhead[5], 10)); byte[] buffercontent = new byte[allcontentleftlengh]; while (allcontentleftlengh - receivedlengh > 0) { byte[] buffertemp = new byte[allcontentleftlengh - receivedlengh]; int lengh = Tcpsocket.Receive(buffertemp); if (lengh <= 0) { if (offlinecount == 3) { throw new Exception("Socket 错误!"); } offlinecount += 1; Thread.Sleep(1000 * 2); } Buffer.BlockCopy(buffertemp, 0, buffercontent, receivedlengh, lengh); receivedlengh += lengh; } //读返回 if (buffercontent[1] == 0x03) { IODeviceInfo IOInfo = new IODeviceInfo(); IOInfo.ID = this.DeviceID; //解析 1-8口 char[] bytestr = Convert.ToString(buffercontent[4], 2).PadLeft(8, '0').ToArray(); for (int i = 0; i < 8; i++) { IOPortInfo ioport = new IOPortInfo(); ioport.PortNo = 8 - i; ioport.PortState = bytestr[i] == '1' ? 1 : 0; IOInfo.DIPortList.Add(ioport); } //9-16口 bytestr = Convert.ToString(buffercontent[3], 2).PadLeft(8, '0').ToArray(); for (int i = 0; i < 8; i++) { IOPortInfo ioport = new IOPortInfo(); ioport.PortNo = 16 - i; ioport.PortState = bytestr[i] == '1' ? 1 : 0; IOInfo.DIPortList.Add(ioport); } //17-24口 bytestr = Convert.ToString(buffercontent[6], 2).PadLeft(8, '0').ToArray(); for (int i = 0; i < 8; i++) { IOPortInfo ioport = new IOPortInfo(); ioport.PortNo = 24 - i; ioport.PortState = bytestr[i] == '1' ? 1 : 0; IOInfo.DIPortList.Add(ioport); } //25-32口 bytestr = Convert.ToString(buffercontent[5], 2).PadLeft(8, '0').ToArray(); for (int i = 0; i < 8; i++) { IOPortInfo ioport = new IOPortInfo(); ioport.PortNo = 32 - i; ioport.PortState = bytestr[i] == '1' ? 1 : 0; IOInfo.DIPortList.Add(ioport); } DelegateState.InvokeIOFeedBackEvent(IOInfo); LastRecTime = DateTime.Now; return(true); } //写返回 if (buffercontent[1] == 0x10) { LastRecTime = DateTime.Now; return(true); } return(false); } catch (Exception ex) { LogHelper.WriteLog("AGV解析编解码错误!" + ex.Message); } return(false); }