JLChen
2021-11-05 de72a7843ceb868c89fc11983e315849caa28573
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
 
    }
 
}