public InspireMotorCommandMessage(uint stickID, CMD_TYPE CMDvalue, CONTRAL_TAB CTValue, uint Para) { //帧长度 uint FrameLength = (DataSectorLong(CTValue, CMDvalue) + 2); byteList.Add((Byte)(FrameLength)); //ID号 byteList.Add((Byte)stickID); //指令类型 byteList.Add((Byte)CMDvalue); //指令类型 byteList.Add((Byte)CTValue); //数据段 for (int i = 0; i < DataSectorLong(CTValue, CMDvalue); i++) { byteList.Add((Byte)((Para >> i * 8) & 0xFF)); } //校验和 uint CheckSum = 0; for (int i = 0; i < byteList.Count(); i++) { CheckSum = byteList[i] + CheckSum; } byteList.Add((Byte)(CheckSum & 0xFF)); }
public static void MotorAResolver(object sender, SerialDataReceivedEventArgs e) { //判断是否是群体操作 try { byte head1 = (byte)((SerialPort)sender).ReadByte(); byte head2 = (byte)((SerialPort)sender).ReadByte(); if (head1 == 0xAA && head2 == 0x55) { int FreamLength = ((SerialPort)sender).ReadByte(); uint StickId = (uint)((SerialPort)sender).ReadByte(); CMD_TYPE CMDvalue = (CMD_TYPE)((SerialPort)sender).ReadByte(); CONTRAL_TAB CTValue = (CONTRAL_TAB)((SerialPort)sender).ReadByte(); uint total = 0; List <byte> listbyte = new List <byte>(); for (int i = 0; i < FreamLength - 2; i++) { listbyte.Add((byte)((SerialPort)sender).ReadByte()); ; } listbyte.Reverse(); for (int i = 0; i < listbyte.Count(); i++) { total = total << 8; total = total | listbyte[i]; } //CommandMessage message = new CommandMessage(StickId, CMDvalue, CTValue, total); //FindStickByID(StickId).ReceiveMessage(message); } } catch (Exception ex) { string s = ex.ToString(); } }
//对应控制表数据长度 public static uint DataSectorLong(CONTRAL_TAB CTValue, CMD_TYPE CMDvalue) { if (CMDvalue == CMD_TYPE.CMD_RD) { return(1); } switch (CTValue) { case CONTRAL_TAB.TARGET_POSITON: return(2); case CONTRAL_TAB.OVERCURRENT: return(2); case CONTRAL_TAB.DriverID: return(1); } return(0); }
public InspireMotorMovementControlMessage(uint f_stickID, MOVE_CODE CMDvalue, CONTRAL_TAB CTValue, UInt16 f_DataValue) { byteList.Clear(); //帧长度 uint FrameLength = (Byte)0x04; byteList.Add((Byte)(FrameLength)); //ID号 byteList.Add((Byte)f_stickID); //指令类型 byteList.Add((Byte)CMDvalue); //控制表索引 byteList.Add((Byte)CTValue); //数据 小端 byteList.Add((Byte)f_DataValue); byteList.Add((Byte)(f_DataValue >> 8 & 0xFF)); //校验和 uint CheckSum = 0; for (int i = 0; i < byteList.Count(); i++) { CheckSum = byteList[i] + CheckSum; } byteList.Add((Byte)(CheckSum & 0xFF)); }
public InspireMotorBroadcastMessage(CMD_TYPE CMDvalue, CONTRAL_TAB CTValue) { this.CMDvalue = CMDvalue; this.CTValue = CTValue; }