public byte[] BuildCmdPackage(byte[] buf) { byte[] pack = new byte[buf.Length + 8]; pack[0] = (byte)'R'; pack[1] = (byte)'E'; pack[2] = (byte)'Q'; pack[3] = (byte)(buf.Length % 0x100); pack[4] = (byte)(buf.Length / 0x100); pack[5] = (byte)(pack[3] << 1 + pack[4]); Array.Copy(buf, 0, pack, 6, buf.Length); Crc16Ccitt crc = new Crc16Ccitt(Crc16Ccitt.InitialCrcValue.Zeros); byte[] crcbuf = crc.ComputeChecksumBytes(buf); Array.Copy(crcbuf, 0, pack, buf.Length + 6, 2); return pack; }
public bool ProcessReceivedBuffer(byte[] buff,int length) { for (int bufIndex = 0; bufIndex < length; bufIndex++) { switch (currCmdProcess) { case 0: switch (currCmdSubProcess) { case 0: if (buff[bufIndex] == 'A') currCmdSubProcess++; else resetProcessStatus(); break; case 1: if (buff[bufIndex] == 'S') currCmdSubProcess++; else resetProcessStatus(); break; case 2: if (buff[bufIndex] == 'N') currCmdSubProcess++; else resetProcessStatus(); break; case 3: packlen_1 = buff[bufIndex]; currCmdSubProcess++; break; case 4: packlen_2 = buff[bufIndex]; currCmdSubProcess++; break; case 5: if (buff[bufIndex] == (byte)(packlen_1 << 1 + packlen_2)) { packagelen = (ushort)(packlen_1 + packlen_2 * 0x100); currCmdSubProcess = 0; currCmdProcess++; } else resetProcessStatus(); break; } break; case 1: if (currCmdSubProcess == 0) receivedBuf = new byte[packagelen]; if (currCmdSubProcess < packagelen) { receivedBuf[currCmdSubProcess++] = buff[bufIndex]; if (currCmdSubProcess >= packagelen) { currCmdProcess++; currCmdSubProcess = 0; } } else { resetProcessStatus(); } break; case 2: if (currCmdSubProcess == 0) { crc_1 = buff[bufIndex]; currCmdSubProcess++; } else { crc_2 = buff[bufIndex]; Crc16Ccitt crc = new Crc16Ccitt(Crc16Ccitt.InitialCrcValue.Zeros); byte[] bCrc = crc.ComputeChecksumBytes(receivedBuf); if (bCrc[0] == crc_1 && bCrc[1] == crc_2) { ProcessANS(); } resetProcessStatus(); } break; } } return false; }
public void BuildAnsPackage(byte[] buf) { byte[] pack = new byte[buf .Length+ 8]; pack[0] = (byte)'A'; pack[1] = (byte)'S'; pack[2] = (byte)'N'; pack[3] = (byte)(buf.Length % 0x100); pack[4] = (byte)(buf.Length / 0x100); pack[5] = (byte)(pack[3]<<1+pack[4]); Array.Copy(buf, 0, pack, 6, buf.Length); Crc16Ccitt crc = new Crc16Ccitt(Crc16Ccitt.InitialCrcValue.Zeros); byte[] crcbuf= crc.ComputeChecksumBytes(buf); Array.Copy(crcbuf, 0, pack, buf.Length + 6, 2); ResponseBuf = pack; HaveResponse = true; }