public byte[] PackageCMD(CommandStruct cmdStruct) { uint length = Convert.ToUInt32(7 + cmdStruct.paramsLength * 4); //start type2 pL pN state4 parameters4*num checksum end byte[] SendBytes = new byte[length]; uint j = 0; //start SendBytes[j] = cmdStartFlag; j++; //type SendBytes[j] = 0x00; j++; SendBytes[j] = (byte)cmdStruct.cmdType; j++; //pL pN SendBytes[j] = 0x04; j++; SendBytes[j] = cmdStruct.paramsLength; j++; uint i = 0; //parameters for (i = 0; i < cmdStruct.paramsLength; i++) { int t = 0; for (t = 0; t < 4; t++) { int parmTmp = Convert.ToInt32(cmdStruct.Params[i]); //little enbian SendBytes[j] = Convert.ToByte((parmTmp >> (8 * t)) % 256); j++; } } //package the checksum SendBytes[j] = getCheckSum(SendBytes); j++; //package the end SendBytes[j] = cmdEndFlag; return(SendBytes); }
public CommandStruct UnPackageBytes(byte[] byteCMD) { CommandStruct cs = new CommandStruct(); //cs.allBytes = byteCMD; cs.cmdState = CommandState.CommandState_formateError; // reponse start if (byteCMD[0] != reponseStartFlag) { return(cs); } //type cs.cmdType = (CommandType)byteCMD[2]; //pL pN uint paramsLength = byteCMD[paramsLengthIndex]; //default is 4 uint paramsNum = byteCMD[paramsNumIndex]; cs.Params = new uint[byteCMD.Length]; //state param1 byte[] state = new byte[paramsLength]; for (int i = 0; i < paramsLength; i++) { state[i] = byteCMD[paramsIndex + i]; } if (CompareArray(state, new byte[] { 0x00, 0x00, 0x00, 0x00 })) //CommandState_normal = 0 { cs.cmdState = CommandState.CommandState_normal; } else if (CompareArray(state, new byte[] { 0x00, 0x00, 0x00, 0x01 })) //CommandState_accidentlyTouchTop { cs.cmdState = CommandState.CommandState_accidentlyTouchTop; } else if (CompareArray(state, new byte[] { 0xff, 0xff, 0xff, 0xff })) //CommandState_formateError { cs.cmdState = CommandState.CommandState_formateError; } else if (CompareArray(state, new byte[] { 0xff, 0xff, 0xff, 0xfe })) //CommandState_parametersError { cs.cmdState = CommandState.CommandState_parametersError; } else if (CompareArray(state, new byte[] { 0xff, 0xff, 0xff, 0xfd })) //CommandState_busy { cs.cmdState = CommandState.CommandState_busy; } else // command error { return(cs); } //parameters uint t; for (t = 0; t < paramsNum - 1; t++) { uint param = 0; for (int j = 0; j < paramsLength; j++) { // skip state param += Convert.ToUInt32(byteCMD[paramsIndex + 4 + paramsLength * t + j] * Math.Pow(256, j)); } cs.Params[t] = param; } cs.paramsLength = (byte)(t + 1); //checksum //end if (byteCMD[byteCMD.Length - 1] != reponseEndFlag) { cs.cmdState = CommandState.CommandState_formateError; } return(cs); }