//根据头得到数据头长度 protected override int GetBodyLengthFromHeader(IBufferStream bufferStream, int length) { int datalength = 0; byte[] headByte = new byte[2]; bufferStream.Read(headByte, 0, 2); int head = DataTransform.bytesToUshort(headByte, 0); if (head == 10004 || head == 20004 || head == 30004 || head == 40004 || head == 30000) { datalength = 6; return(datalength); } if (head == 10005 || head == 20005 || head == 30005 || head == 40005) { datalength = 8; return(datalength); } return(datalength); }
//解析数据 public override PackageInfo ResolvePackage(IBufferStream bufferStream) { byte CKCode = 0; //校验位 PackageInfo datapackage = new PackageInfo(); List <byte> byteData = new List <byte>(); byteData.Add(bufferStream.Buffers.Last().Array[0]); //头(10003/10005) byteData.Add(bufferStream.Buffers.Last().Array[1]); byteData.AddRange(bufferStream.Buffers.Last().Array.Skip(headerSize).Take(GetBodyLengthFromHeader(bufferStream, 0)).ToArray()); //数据 byte[] data = byteData.ToArray(); for (int i = 0; i < data.Length - 1; i++) //校验前n-1个数据 { CKCode += data[i]; } if (data[data.Length - 1] == CKCode) //如果校验通过 { if (data.Length == 9) { datapackage.Head = DataTransform.bytesToUshort(data, 0); datapackage.TaskID = DataTransform.bytesToInt(data, 2); datapackage.TaskStepID = DataTransform.bytesToUshort(data, 6); } else if (data.Length == 11) { datapackage.Head = DataTransform.bytesToUshort(data, 0); datapackage.TaskID = DataTransform.bytesToInt(data, 2); datapackage.TaskStepID = DataTransform.bytesToUshort(data, 6); datapackage.CarID = DataTransform.bytesToUshort(data, 8); } else { datapackage.Head = 0; datapackage.TaskID = 0; datapackage.TaskStepID = 0; datapackage.CarID = 0; } } return(datapackage); }
public void SendMessage_P(DTUSession session) { byte[] data = new byte[150]; data[0] = 0x05; data[1] = 0x0A; data[2] = 0x05; data[3] = 0x0A; //消息头标识 data[4] = 0x80; //通讯标志位 data[5] = 0x02; data[6] = 0x0A; data[7] = 0x00; data[8] = 0x01; //命令字 data[9] = 0x00; data[10] = 0x82; //数据长度 130 固定值 data[11] = 0x00; data[12] = 0x01; //包序 固定值 data[13] = 0x01; //最后一包 固定值 data[20] = 0x29; data[21] = 0xFF; //平台心跳 data[23] = SystemState; //系统状态 data[24] = (byte)((P_Order_Enable) | (Hand_Order_Enabel << 1) | (OrderControl << 3)); //订单控制 data[39] = (byte)((Raw_Tray_Out) | (Inform_P_Raw_Tray << 1)); data[41] = (byte)((AGV_Manual_Up_GET) | (AGV_Manual_Down_GET << 1) | (AGV_Process1_GET << 2) | (AGV_Process2_GET << 3) | (AGV_Manual_Up_PUT << 4) | (AGV_Manual_Down_PUT << 5) | (AGV_Process1_PUT << 6) | (AGV_Process2_PUT << 7)); data[42] = Maunal_Up_RFID; byte[] cmd = new byte[4]; cmd = DataTransform.floatToByte(Manual_Up_RFID_Para); data[46] = cmd[0]; data[47] = cmd[1]; data[48] = cmd[2]; data[49] = cmd[3]; for (int i = 20; i < 150; i++) //消息体校验字 { data[14] += data[i]; } for (int i = 0; i < 19; i++) //消息头校验字 { data[19] += data[i]; } session.Send(data, 0, 150); }
//解析数据 public override WHPackageInfo ResolvePackage(IBufferStream bufferStream) { //byte CKCode = 0; //校验位 WHPackageInfo datapackage = new WHPackageInfo(); List <byte> byteData = new List <byte>(); byteData.Add(bufferStream.Buffers.Last().Array[0]); //消息头 byteData.Add(bufferStream.Buffers.Last().Array[1]); byteData.AddRange(bufferStream.Buffers.Last().Array.Skip(headsize).Take(GetBodyLengthFromHeader(bufferStream, 0)).ToArray()); //数据 byte[] data = byteData.ToArray(); //for (int i = 0; i < data.Length - 1; i++) //校验前n-1个数据 //{ // CKCode += data[i]; //} if (true) //始终校验通过 { if (data.Length == 8) { datapackage.Head = DataTransform.bytesToUshort(data, 0); //头 datapackage.SerialNumber = DataTransform.bytesToInt(data, 2); //流水号 datapackage.InlibMatchingID = DataTransform.bytesToUshort(data, 6); //入库匹配号 } else if (data.Length == 10) { datapackage.Head = DataTransform.bytesToUshort(data, 0); //头 datapackage.SerialNumber = DataTransform.bytesToInt(data, 2); //流水号 datapackage.TrayStyle = DataTransform.bytesToUshort(data, 6); //托盘类型代号 datapackage.OutlibMatchingID = DataTransform.bytesToUshort(data, 8); //出库匹配号 } else { datapackage.Head = 0; //头 datapackage.SerialNumber = 0; //流水号 datapackage.TrayStyle = 0; //托盘类型代号 } } return(datapackage); }