2021-09-17 串口通讯协议拼接
2021-09-17 本文已影响0人
fjasmin
动态区 拼接数据示例
a5 a5 a5 a5 a5 a5 a5 a5 02 00 00 80 00 00 00 00 00 01 51 02 2a 00 a3 06 01 00 00 00 01 21 00 06 00 00 00 00 08 00 20 00 00 00 00 02 00 00 00 00 01 01 01 00 00 0a 06 00 00 00 48 65 6c 6c 6f 21 60 33 5a
数据解析
帧头
a5 a5 a5 a5 a5 a5 a5 a5
包头数据
02 00 //DstAddr 屏号2
00 80 //SrcAddr 客户端
00 00 00 00 //Reserved
00 //CheckMode
01 //DisplayMode
51 //DeviceType 5K1
02 //ProtocoIVersion
2a 00 //DataLen
数据域(实时信息)
a3 //CmdGroup
06 //Cmd
01 //Response
00 //ProcessMode
00 //Reserved
00 //DeleteAreaNum DeleteAreaId不发送
01 //AreaNum
21 00 //AreaDataLen0
06 //AreaType 动态区类型在6以上
00 00 //AreaX 0
00 00 //AreaY 0
08 00 //AreaWidth 64
20 00 //AreaHeight 32
00 //DynamicAreaLoc 0
00 //Lines_sizes
00 //Runmode
02 00 //Timeout
00 //SoundMode SoundPerson\SoundVolume\SoundSpeed\SoundDataLen\SoundData不发送
00 00 //Reserved
01 //SingleLine 单行
01 //NewLine 手动换行
01 //DisplayMode 静止
00 //ExitMode
00 //Speed
0a //StayTime
06 00 00 00 //DataLen
48 65 6c 6c 6f 21 //Data Hello!
包校验
60 33
拼接代码
private void test(){
//头部
final byte[] HEAD_LED = new byte[]{(byte) 0xA5, (byte) 0xA5,(byte) 0xA5,(byte) 0xA5,(byte) 0xA5,(byte) 0xA5,(byte) 0xA5,(byte) 0xA5};
//尾部
final byte[] END_LED = new byte[]{(byte) 0x5A};
String text = "Hello!";
byte[] textBuffer = text.getBytes(StandardCharsets.UTF_8);
int textBufferLength = textBuffer.length;
int areaX = 0;
int areaY = 0;
int areaWidth = 64;
int areaHeight = 32;
int timeout = 2;
int screenNumber = 2;
int srcAddress = 0X8000;
//1.区域数据
ByteBuffer areaBuffer = ByteBuffer.allocate(512);
areaBuffer.put((byte)0X06);//AreaType
areaBuffer.put((byte)(areaX & 0XFF));//x
areaBuffer.put((byte)((areaX >>> 8)&0XFF));
areaBuffer.put((byte)(areaY & 0XFF));//y
areaBuffer.put((byte)((areaY >>> 8)&0XFF));
areaBuffer.put((byte)(areaWidth & 0XFF));//areaWidth
areaBuffer.put((byte)((areaWidth >>> 8)&0XFF));
areaBuffer.put((byte)(areaHeight & 0XFF));//areaHeight
areaBuffer.put((byte)((areaHeight >>> 8)&0XFF));
areaBuffer.put((byte)0X00);
areaBuffer.put((byte)0X00);
areaBuffer.put((byte)0X00);
areaBuffer.put((byte)(timeout & 0XFF));//timeout
areaBuffer.put((byte)((timeout >>> 8)&0XFF));
areaBuffer.put((byte)0X00);
areaBuffer.putShort((short)0X00);
areaBuffer.put((byte)0X01);//SingleLine 单行
areaBuffer.put((byte)0X01);//NewLine 手动换行
areaBuffer.put((byte)0X01);//DisplayMode 静止
areaBuffer.put((byte)0X00);//ExitMode
areaBuffer.put((byte)0X00);//Speed
areaBuffer.put((byte)0X0A);//StayTime
areaBuffer.put((byte)(textBufferLength & 0XFF));//DataLength
areaBuffer.put((byte)((textBufferLength >> 8)&0XFF));
areaBuffer.put((byte)((textBufferLength >> 16)&0XFF));
areaBuffer.put((byte)((textBufferLength >> 24)&0XFF));
areaBuffer.put(textBuffer);//data
areaBuffer.flip();
int areaBufferLength = areaBuffer.limit();
byte[] areaBufferData = new byte[areaBufferLength];
areaBuffer.get(areaBufferData);
//2.数据域
ByteBuffer dataBuffer = ByteBuffer.allocate(1024);
dataBuffer.put((byte)0XA3);//cmdGroup
dataBuffer.put((byte)0X06);
dataBuffer.put((byte)0X01);
dataBuffer.put((byte)0X00);
dataBuffer.put((byte)0X00);
dataBuffer.put((byte)0X00);
dataBuffer.put((byte)0X01);//alarmNum
dataBuffer.put((byte)(areaBufferLength & 0XFF));//length
dataBuffer.put((byte)((areaBufferLength >> 8)&0XFF));
dataBuffer.put(areaBufferData);
dataBuffer.flip();
int dataBufferLength = dataBuffer.limit();
byte[] dataBufferData = new byte[dataBufferLength];
dataBuffer.get(dataBufferData);
//3.包头数据
ByteBuffer headBuffer = ByteBuffer.allocate(14);
headBuffer.put((byte)(screenNumber & 0XFF));//dstAddress
headBuffer.put((byte)((screenNumber >> 8)&0XFF));
headBuffer.put((byte)(srcAddress & 0XFF));//srcAddress
headBuffer.put((byte)((srcAddress >> 8)&0XFF));
headBuffer.put((byte)0X00);
headBuffer.put((byte)0X00);
headBuffer.put((byte)0X00);
headBuffer.put((byte)0X00);//Option
headBuffer.put((byte)0X02);//CheckMode 无需校验 注意这里?
headBuffer.put((byte)0X01);//DisplayMode
headBuffer.put((byte)0X51);//DeviceType
headBuffer.put((byte)0X02); //ProtocolVersion
headBuffer.put((byte)(dataBufferLength & 0XFF));//DataLen
headBuffer.put((byte)((dataBufferLength >> 8)&0XFF));
headBuffer.flip();
int headBufferLength = headBuffer.limit();
byte[] headBufferData = new byte[headBufferLength];
headBuffer.get(headBufferData);
//4.校验位
short bufferCheck = 0;
ByteBuffer.allocate(dataBufferLength & 0XFF).order(ByteOrder.LITTLE_ENDIAN);
//5.完整包
ByteBuffer buffer = ByteBuffer.allocate(2048);
buffer.put(HEAD_LED);
buffer.put(headBufferData);
buffer.put(dataBufferData);
buffer.putShort(bufferCheck);//这里要与CheckMode的值对应上 =2,不需要校验
buffer.put(END_LED);
buffer.flip();
LogUtils.e(bytes2hex(conver(buffer))+"-----------------------");
}
//必须调用完后flip()才可以调用此方法
public static byte[] conver(ByteBuffer byteBuffer){
int len = byteBuffer.limit() - byteBuffer.position();
byte[] bytes = new byte[len];
if(byteBuffer.isReadOnly()){
return null;
}else {
byteBuffer.get(bytes);
}
return bytes;
}
public static String bytes2hex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
String tmp = null;
for (byte b : bytes) {
// 将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制
tmp = Integer.toHexString(0xFF & b);
if (tmp.length() == 1) {
tmp = "0" + tmp;
}
sb.append(tmp);
}
return sb.toString();
}