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; } protected 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("12345678"); 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); Log.i(TAG, "包内容: "+ByteUtil.byte2HexStr(desBytes)); return desBytes; } }