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
110
package safeluck.drive.evaluation.bean;
 
import android.util.Log;
 
import com.anyun.im_lib.util.ByteUtil;
 
/**
 * MyApplication2
 * Created by lzw on 2019/12/17. 15:39:23
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public abstract class DriveExamProtocol {
 
    private static final String TAG = "DriveExamProtocol";
    /***========================
                消息结构表
    标识位    消息头    消息体    校验码    标识位
     0x7E                                                       0x7E
    ======================**/
   // 标识位
    private byte MESSAGE_HEAD = 0x7e;
    //标识位
    private byte MESSAGE_TAIL = 0x7e;
//校验码  先暂时写死
    private int checkCode = 0x78;
 
 
/***===========以下是消息头=============***/
    //协议版本号    BYTE    235,固定
    private short protocol_version = 235;
//消息ID
    private short msg_id;
 
    //消息体属性
    private int msg_property;
 
    //终端手机号
    private String phoneOnTerminal;
//13    消息流水号    WORD    按发送顺序从0开始循环累加
    public static short msg_serial_num=0;
 
    //15    预留    BYTE    预留
    private byte reserve;
    /***===========消息头结束=============***/
 
    /**
     * 消息体
     */
    protected abstract byte[] createMessageBody();
 
    /**
     * 构造函数
     * @param msg_id 消息ID
     */
    public DriveExamProtocol(short msg_id) {
        this.msg_id = msg_id;
    }
 
    public byte[] toBytes(){
        byte[] desBytes = new byte[1+16+2+1+1];
        int pos = 0;
 
 
        //标识位
        desBytes[pos] = MESSAGE_HEAD;
        pos++;
        //协议版本号
        byte[] protoVersion = ByteUtil.shortGetByte(protocol_version);
        System.arraycopy(protoVersion,0,desBytes,pos,protoVersion.length);
        pos +=protoVersion.length;
 
//消息ID
        byte[] msgIdBytes = ByteUtil.shortGetBytes(msg_id);
        System.arraycopy(msgIdBytes,0,desBytes,pos,msgIdBytes.length);
        pos+=msgIdBytes.length;
 
//消息体属性
        byte[] msg_pro_bytes = ByteUtil.shortGetBytes((short)2);
        System.arraycopy(msg_pro_bytes,0,desBytes,pos,msg_pro_bytes.length);
        pos+=msg_pro_bytes.length;
        //终端手机号
        byte[] phoneBytes = ByteUtil.str2Bcd("1234567890121518");
        System.arraycopy(phoneBytes,0,desBytes,pos,phoneBytes.length);
        pos+=phoneBytes.length;
//消息流水号
        byte[] msg_serialNum = ByteUtil.shortGetBytes(msg_serial_num++);
        System.arraycopy(msg_serialNum,0,desBytes,pos,msg_serialNum.length);
        pos+=msg_serialNum.length;
//保留
        desBytes[pos] = 0;
        pos++;
 
        //消息体
        byte[] messageBodyBytes = createMessageBody();
        System.arraycopy(messageBodyBytes,0,desBytes,pos,messageBodyBytes.length);
        pos+=messageBodyBytes.length;
 
        //校验码
        desBytes[pos] = 0x01;
        pos++;
        //末尾结束标识位
        desBytes[pos] = MESSAGE_TAIL;
 
        Log.i(TAG, "包长度="+(pos+1));
        Log.i(TAG, "包内容: "+ByteUtil.byte2HexStr(desBytes));
        return desBytes;
    }
 
}