package com.hdl.sdk.ttl.HDLDeviceManger.Core; import com.hdl.sdk.ttl.Config.MCUConstants; /** * Created by JLChen on 2019/7/12 */ public class MCUCrc { public byte[] addBytes; public int mcuCommand = 0; public MCUCrc(int command, byte[] addBytes) { this.mcuCommand = command; this.addBytes = addBytes; } /** * 魔镜MCU协议 * @return */ public byte[] getMCUSendBytes() { byte[] sendBytes = new byte[5 + this.addBytes.length]; //Boot code sendBytes[0] = (byte) MCUConstants.MCU_BOOT_HEAD; sendBytes[1] = (byte) mcuCommand; sendBytes[2] = (byte) ((this.addBytes.length) / 256); //数据长度:去除引导头的长度 sendBytes[3] = (byte) ((this.addBytes.length) % 256); //数据长度:去除引导头的长度 System.arraycopy(this.addBytes, 0, sendBytes, 4, this.addBytes.length); sendBytes[sendBytes.length-1] = getMcuChecksum(sendBytes); //设置校验码 return sendBytes; } /** * 校验码计算 * @param packet * @return * 头引导码和尾校验码不检验 所以i=1,开始 i < (packet.length - 2) */ public static byte getMcuChecksum(byte[] packet) { byte mSum = 0; for(int i = 0; i < (packet.length - 1); i++) { mSum += (byte)packet[i]; } mSum = (byte) ((~mSum) + 1); return mSum; } }