示例数据(包括:帧头、包长、数据包内容、CRC 校验、帧尾):
AA AA AA 00 01 30 30 31 31 31 31 30 32 30 36 30 39 30 30 30 31 47 31 30 32 4C
32 30 36 31 32 30 32 32 35 00 00 01 D6 07 08 11 05 0F 00 04 0B 33 19 00 2A 00 00 00
03 00 53 03 00 66 02 00 68 00 00 00 02 00 61 00 00 00 00 00 00 00 00 00 0C 33 19 00
2A 00 00 00 02 00 67 02 00 54 01 00 68 00 00 00 02 00 4D 00 00 00 00 00 00 00 00 00
1F 33 19 00 2A 00 00 00 02 00 5C 01 00 6C 01 00 55 01 00 56 01 00 46 00 00 00 00 00
00 00 00 00 20 33 19 00 2A 00 00 00 02 00 5F 02 00 50 01 00 5A 01 00 6C 02 00 53 00
00 00 00 00 00 00 00 00 E0 9F EE EE
数据包分解为:
AA AA 2 个字节的时间间隔的帧头
AA 00 数据包内容长度,170 字节
01 数据包类型,实时交通数据包
30 30 31 31 31 31 30 32 30 36 30 39 30 30 30 31 设备身份识别码
47 31 30 32 4C 32 30 36 31 32 30 32 32 35 00 站点编号
00 设备硬件错误码,正常
01 调查内容,调查所有项目(不含预留字段)
D6 07 年份,2006 年
byte[] newBytes = Arrays.copyOf(bytes, bytes.length);
String logMsg=CommUtils2.toHexString(newBytes);//设备状态表中的部分错误信息(因可能超长,应用时可将其置为"")
//拆分数组,查询标识符及长度进行拆分
//注:ArrayUtils.subarray(newBytes, 0,5) 截取下角标从0到4的内容
int splitFlag=0;
int splitNextFlag=0;
//int byteLength=1;
//int byteLengthTemp=newBytes[2]& 0xFF;
//byteLength=byteLengthTemp+2;
//boolean runFlag=true;
while(splitNextFlag<newBytes.length){
try {
splitFlag=splitNextFlag;//每次都更新读取的起始位置
if(newBytes[splitFlag] != (byte)0xFF){
throw new RuntimeException("设备"+recEntity.getDeviceId()+"接收数据异常,信息分组首字节不是FF : "+CommUtils2.toHexString(newBytes));
// throw new RuntimeException("接收数据异常,信息分组首字节不是FF:" + logMsg);
}
byte recFlag=newBytes[splitFlag+1]; //标识符位
int byteLengthTemp=newBytes[splitFlag+2]& 0xFF;//车道字节所占长度
int splitFlagTemp=splitFlag;
splitNextFlag=splitFlagTemp+byteLengthTemp+4;//读取下条数据起始位置
switch (recFlag) {
case 0x20://32
flowBytes2=ArrayUtils.subarray(newBytes, splitFlag,splitNextFlag);
System.out.println("中车流量"+CommUtils2.toHexString(flowBytes2));
break;
case 0x1B://27
flowBytes3=ArrayUtils.subarray(newBytes, splitFlag,splitNextFlag);
System.out.println("长车流量"+CommUtils2.toHexString(flowBytes3));
break;
public static String toHexString(byte[] src) {
if (src == null || src.length <= 0) {
throw new IllegalArgumentException("[toHexString] this byteArray must not be null or empty");
}
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
stringBuilder.append("0x");
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv.toUpperCase());
}
return stringBuilder.toString();
}