update 电单车协议

This commit is contained in:
Guoqs
2024-07-16 15:00:45 +08:00
parent c3908e2ca9
commit f0e0bcc63f
2 changed files with 9 additions and 3 deletions

View File

@@ -36,23 +36,29 @@ public class ChargingPileDecoder extends ByteToMessageDecoder {
// 读取物理ID // 读取物理ID
int physicalId = in.readInt(); int physicalId = in.readInt();
log.info("physicalId:{}", physicalId);
// 读取消息ID // 读取消息ID
short messageId = in.readShort(); short messageId = in.readShort();
log.info("messageId:{}", messageId);
// 读取命令 // 读取命令
byte command = in.readByte(); byte command = in.readByte();
log.info("command:{}", command);
// 读取数据 // 读取数据
int dataLength = length - 13; // 13 = 包头(3) + 长度(2) + 物理ID(4) + 消息ID(2) + 命令(1) + 校验(2) int dataLength = length - 13; // 13 = 包头(3) + 长度(2) + 物理ID(4) + 消息ID(2) + 命令(1) + 校验(2)
byte[] data = new byte[dataLength]; byte[] data = new byte[dataLength];
in.readBytes(data); in.readBytes(data);
log.info("data:{}", data);
// 读取校验和 // 读取校验和
short checksum = in.readShort(); short checksum = in.readShort();
log.info("checksum:{}", checksum);
// 验证校验和 // 验证校验和
short calculatedChecksum = calculateChecksum(in, length); short calculatedChecksum = calculateChecksum(in, length);
log.info("calculatedChecksum:{}", calculatedChecksum);
if (checksum != calculatedChecksum) { if (checksum != calculatedChecksum) {
log.info("校验和错误,丢弃此帧"); log.info("校验和错误,丢弃此帧");
continue; continue;
@@ -60,6 +66,7 @@ public class ChargingPileDecoder extends ByteToMessageDecoder {
// 创建消息对象并添加到输出列表 // 创建消息对象并添加到输出列表
ChargingPileMessage message = new ChargingPileMessage(physicalId, messageId, command, data); ChargingPileMessage message = new ChargingPileMessage(physicalId, messageId, command, data);
log.info("ChargingPileMessage:{}", message.toString());
out.add(message); out.add(message);
} }
} }

View File

@@ -26,8 +26,7 @@ public class ElectricBicyclesServerChannelInitializer extends ChannelInitializer
@Override @Override
protected void initChannel(SocketChannel channel) throws Exception { protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline(); ChannelPipeline pipeline = channel.pipeline();
// pipeline.addLast("frameDecoder",new CustomDecoder()); pipeline.addLast("frameDecoder", new ChargingPileDecoder());
pipeline.addLast("frameDecoder", new StartAndLengthFieldFrameDecoder());
pipeline.addLast("decoder", new ByteArrayDecoder()); pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayDecoder()); pipeline.addLast("encoder", new ByteArrayDecoder());
//读超时时间设置为10s0表示不监控 //读超时时间设置为10s0表示不监控