通过java实现自定义协议的相关知识

2020-03-20  本文已影响0人  真胖大海

假设要有java实现下面的协议


协议

一.整体思路

  1. 整个协议报文使用字节数组表示
  2. 字节数组中的每一个字节表示协议中的字段
    上图的协议可以表示为
new byte[]{
                0X00,//Length_H
                0x17,//Length_L
                0x00,//Cmd_H
                (byte) 0x81,//Cmd_L
                (byte) 0xFF,
                (0x00),
                (byte) 0xFF,
                (0x00),
                (byte) 0xFF,
                (0x00),
                (byte) 0xFF,
                (0x00),
                (0x00),//direction
                //json data
                0x7b,
                0x22,
                0x61,
                0x22,
                0x3a,
                0x22,
                0x62,
                0x22,
                0x2c,
                0x7d,
                //json data
                0x07,
                0x3d
        };

二.注意点

1. byte的取值范围为[-128,127],但是表示的字段的值的范围为[0,255],可能会超过byte的取值范围

所以此时要强转( 比如:(byte)0xff ),原理暂且不知道。

2. 累加校验和的计算方式

    /**
     * 获取校验和
     */
    public static int getCheckedSum(byte[] msg) {
        int mSum = 0;

        /** 逐Byte添加位数和 */
        for (byte byteMsg : msg) {
            long mNum = ((long) byteMsg >= 0) ? (long) byteMsg : ((long) byteMsg + 256);
            mSum += mNum;
        } /** end of for (byte byteMsg : msg) */
        System.out.println("校验和" + mSum);
        System.out.println("0x"+Integer.toHexString(mSum));
        return mSum;
    }

3. 将长度为两个字节的数据取出高位和地位分别放入两个字节中

比如:校验和为0x073d,现在取出器高位0x04,0x3d,放入到字节数组中

  /**
     * 获取一个整数转16进制后的高低位
     * 比如
     * 十进制 1163
     * 十六进制 0x48b
     * 校验和 高位:0x04
     * 校验和 低位:0x8b
     *
     * @param accSum 整数
     * @return
     */
    public static byte[] getHeightLowByte(int accSum){
        String accHexStr = Integer.toHexString(accSum);

        //补0
        if (accHexStr.length() == 1) {
            accHexStr = "000" + accHexStr;
        } else if (accHexStr.length() == 2) {
            accHexStr = "00" + accHexStr;
        } else if (accHexStr.length() == 3) {
            accHexStr = "0" + accHexStr;
        }

        System.out.println("校验和16进制: 0x"+accHexStr);

        String accHeightStr = accHexStr.substring(0, 2);
        String accLowStr = accHexStr.substring(2, 4);

        byte accHeightByte= (byte) Integer.parseInt(accHeightStr,16);
        byte accLowByte= (byte) Integer.parseInt(accLowStr,16);
        return new byte[]{accHeightByte,accLowByte};
    }
  public static byte[] getHeightLowByte(int num) {
        byte highOrder = (byte) ((num & 0xff00) >> 8);
        byte lowOrder = (byte) (num & 0x00ff);
        return new byte[]{highOrder, lowOrder};
    }

4.通过高位和低位得到int

//注意:位移操作前,先做&操作
 public static int fromHighAndLowByteToInt(byte highOrder, byte lowOrder){
        return (highOrder & 0xff) << 8 | (lowOrder & 0xff);
    }

5.将协议抽象为类,方便编程

上述的协议抽象为

class StructAppSendData {
    public byte header1 = (byte) (0xBB);//固定头1
    public byte header2 = (byte) (0x55);//固定头2
    public byte len1 = (0x00);//APP向蓝牙发送的数据包,长度固定为13
    public byte len2 = (0x0d);
    public byte cmd1 = (0x00);//命令
    public byte cmd2 = (byte) 0x81;//命令
    public byte UserID_1 = (byte) 0xff;
    public byte UserID_2 = 0x00;
    public byte UserID_3 = (byte) 0xff;
    public byte UserID_4 = 0x00;
    public byte UserID_5 = (byte) 0xff;
    public byte UserID_6 = 0x00;
    public byte UserID_7 = (byte) 0xff;
    public byte UserID_8 = 0x00;
    public byte direction = (0x01);
    public byte acc1 = 0;//需要计算
    public byte acc2 = 0;//需要计算
}

5. 为了方便编程,可以先使用List<Byte>存储数据

上一篇 下一篇

猜你喜欢

热点阅读