思路:使用Netty的ByteBuf进行粘包拆包处理。需自定义其属性,解析处理的协议格式下图。代码如下:
static SerialPortParameter parameter;
static InputStream is = null;
static int length ;
static byte[] data = null;
static byte[] bytesData = null;
static ByteBuf buf = Unpooled.compositeBuffer();//无限制长度
static ByteBuffer frameBytes = null;
static int bufflenth ;
public static byte[] readData(SerialPort serialPort) {
try {
//获得串口的输入流
is = serialPort.getInputStream();
//获得数据长度
bufflenth = is.available();
if (bufflenth != 0) {//如果缓冲区无数据就不读取
buf.writeByte(is.read());//每次读取一个字节
//判断是否满足字节最小数目,不满足直接return等待数据到来
if (buf.readableBytes() < ProtocolConstant.FRAME_SIZE_MIN) {
return null;
}
int beginReader;//记录包头标志
while (true) {
// 获取包头开始的index
beginReader = buf.readerIndex();
// 标记包头开始的index
buf.markReaderIndex();
// 读到了协议的开始标志,结束while循环
if (buf.readByte() == ProtocolConstant.FRAME_HEAD_FLAG) {
//跳过VendorID 和 DEV_ID
buf.readBytes(10);
//读取CMD加Payload长度
byte[] lengthBytes1 = new byte[2];
lengthBytes1[1] = buf.readByte();
lengthBytes1[0] = buf.readByte();
length = FrameCommUtil.bytes2Int(lengthBytes1, 0, lengthBytes1.length);//读取长度
//如果长度符合完整报文长度继续执行。
if(buf.readableBytes() == length + 2){
//跳过数据区
buf.readBytes(length);
//跳过校验和
buf.readByte();
//还原到包头位置
buf.resetReaderIndex();
//读取包头标志,因为默认包头已经读过
// buf.readByte();
break;
}else{
buf.resetReaderIndex();
return null;
}
}
// 未读到包头,略过一个字节
// 每次略过,一个字节,去读取,包头信息的开始标记
buf.resetReaderIndex();
buf.readByte();
// 当略过,一个字节之后,
// 数据包的长度,又变得不满足
// 此时,应该结束。等待后面的数据到达
if (buf.readableBytes() < ProtocolConstant.FRAME_SIZE_MIN) {
return null;
}
}
if (buf.readableBytes() < length + ProtocolConstant.FRAME_SIZE_MIN) {
buf.readerIndex(beginReader);
return null;
}
buf.resetReaderIndex();
//创造一个长度为length的容纳数据的数组
data = new byte[length + ProtocolConstant.FRAME_SIZE_MIN];
try {
buf.readBytes(data);//将数据写入数组
frameBytes = ByteBuffer.allocate(15 + length);
frameBytes.put(data);//放入数据
bytesData = frameBytes.array();//转换为数组
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bytesData;//返回完整报文
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
数据格式