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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.hdl.sdk.ttl_sdk.bean;
 
import com.hdl.sdk.ttl.HDLDeviceManger.Core.Crc;
 
/**
 * Created by JLChen on 2019/9/27
 */
public class ZigBeeCommand {
 
    public static final int COMMAND_HEARTBEAT_SEND = 0x2000;
    public static final int COMMAND_BACK_OPEN_RECEIVING_MODE = 0x2002;//代表有缓存数据
    public static final int COMMAND_BACK_CLOSE_RECEIVING_MODE = 0x2001;//代表没缓存数据
    public static final int COMMAND_SEND_DATA = 0x1002;//发送数据 (魔镜->STM32)
    public static final int COMMAND_SEND_DATA_BACK = 0x2003;//发送数据回复 1byte0XF8成功 0XF5失败
 
    public static final int COMMAND_RECEIVE_DATA = 0x1001;//(STM32->魔镜)
    public static final int COMMAND_RECEIVE_DATA_BACK = 0x2003;//发送数据 1byte0XF8成功 0XF5失败
 
 
    private static int DeviceType = 0x6666;
//    private int Command1002 =  0x1002;
//    private int CommandHeartbeat =  0x2000;
//    private int DeviceID =   0x01;
//    private static int sendCount = 0;//发送序列号
 
    /**
     * @return
     */
    public static byte[] GetHeartbeatBytes(int sendCount) {
        byte[] crcBytes = new byte[13];
        byte[] sendBytes = new byte[2 + crcBytes.length];
        //Boot code
        sendBytes[0] = (byte) 0xAA;
        sendBytes[1] = (byte) 0xAA;
        crcBytes[0] = (byte) (crcBytes.length / 256);
        crcBytes[1] = (byte) (crcBytes.length % 256);
        crcBytes[2] = (byte) 0x01;
        crcBytes[3] = (byte) 0x01;
        crcBytes[4] = (byte) (DeviceType / 256);
        crcBytes[5] = (byte) (DeviceType % 256);
        crcBytes[6] = (byte) (COMMAND_HEARTBEAT_SEND / 256);
        crcBytes[7] = (byte) (COMMAND_HEARTBEAT_SEND % 256);
        crcBytes[8] = (byte) 0x02;
        crcBytes[9] = (byte) 0x02;
        crcBytes[10] = (byte) sendCount;
 
        Crc.ConCRC(crcBytes, crcBytes.length - 2);
        System.arraycopy(crcBytes, 0, sendBytes, 2, crcBytes.length);
        return sendBytes;
    }
 
    /**
     * @return
     */
    public static byte[] GetSendBytes(byte[] addBytes, int mCommand, int sendCount) {
 
        byte[] crcBytes = new byte[15 + addBytes.length];
        byte[] sendBytes = new byte[2 + crcBytes.length];
        //Boot code
        sendBytes[0] = (byte) 0xAA;
        sendBytes[1] = (byte) 0xAA;
        crcBytes[0] = (byte) (crcBytes.length / 256);
        crcBytes[1] = (byte) (crcBytes.length % 256);
        crcBytes[2] = (byte) 0x01;
        crcBytes[3] = (byte) 0x01;
        crcBytes[4] = (byte) (DeviceType / 256);
        crcBytes[5] = (byte) (DeviceType % 256);
        crcBytes[6] = (byte) (mCommand / 256);
        crcBytes[7] = (byte) (mCommand % 256);
        crcBytes[8] = (byte) 0x02;
        crcBytes[9] = (byte) 0x02;
        crcBytes[10] = (byte) sendCount;
        crcBytes[11] = (byte) (addBytes.length / 256);
        crcBytes[12] = (byte) (addBytes.length % 256);
        System.arraycopy(addBytes, 0, crcBytes, 13, addBytes.length);
        Crc.ConCRC(crcBytes, crcBytes.length - 2);
        System.arraycopy(crcBytes, 0, sendBytes, 2, crcBytes.length);
        return sendBytes;
    }
 
 
    /**
     * @return
     * COMMAND_RECEIVE_DATA_BACK
     */
    public static byte[] GetReplyBytes(int sendCount, boolean bSuccess) {
        byte[] crcBytes = new byte[14];
        byte[] sendBytes = new byte[2 + crcBytes.length];
        //Boot code
        sendBytes[0] = (byte) 0xAA;
        sendBytes[1] = (byte) 0xAA;
        crcBytes[0] = (byte) (crcBytes.length / 256);
        crcBytes[1] = (byte) (crcBytes.length % 256);
        crcBytes[2] = (byte) 0x01;
        crcBytes[3] = (byte) 0x01;
        crcBytes[4] = (byte) (DeviceType / 256);
        crcBytes[5] = (byte) (DeviceType % 256);
        crcBytes[6] = (byte) (COMMAND_RECEIVE_DATA_BACK / 256);
        crcBytes[7] = (byte) (COMMAND_RECEIVE_DATA_BACK % 256);
        crcBytes[8] = (byte) 0x02;
        crcBytes[9] = (byte) 0x02;
        crcBytes[10] = (byte) sendCount;
        crcBytes[11] = bSuccess ? (byte) 0xF8 : (byte) 0xF5;
        Crc.ConCRC(crcBytes, crcBytes.length - 2);
        System.arraycopy(crcBytes, 0, sendBytes, 2, crcBytes.length);
        return sendBytes;
    }
 
}