This commit is contained in:
snow 2024-11-24 13:35:58 +08:00
parent 2f7ba359e4
commit 8b0b219fbb
2 changed files with 20 additions and 1 deletions

View File

@ -82,7 +82,11 @@
<artifactId>spring-boot-starter-amqp</artifactId> <artifactId>spring-boot-starter-amqp</artifactId>
</dependency> </dependency>
<!-- tools --> <!-- tools -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId> <artifactId>druid-spring-boot-3-starter</artifactId>

View File

@ -3,6 +3,7 @@ package com.iot.modbus_rtcp.netty;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import com.iot.modbus_rtcp.config.EquipmentIPProperties; import com.iot.modbus_rtcp.config.EquipmentIPProperties;
import com.iot.modbus_rtcp.dto.ModbusCommandDto; import com.iot.modbus_rtcp.dto.ModbusCommandDto;
import com.iot.modbus_rtcp.utils.CRCUtil;
import com.iot.modbus_rtcp.utils.HexUtil; import com.iot.modbus_rtcp.utils.HexUtil;
import com.iot.modbus_rtcp.utils.SpringUtil; import com.iot.modbus_rtcp.utils.SpringUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -10,6 +11,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.springframework.amqp.core.QueueBuilder; import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitAdmin;
@ -88,8 +90,21 @@ public class ModbusDecoder extends ByteToMessageDecoder {
if (bytesCache.length < (message.getLength() / 2)) { if (bytesCache.length < (message.getLength() / 2)) {
DataCache.put(identity, bytesCache); DataCache.put(identity, bytesCache);
} else if (bytesCache.length == (message.getLength() / 2)) { } else if (bytesCache.length == (message.getLength() / 2)) {
// if (!verifyCRC(bytesCache)) {
// return;
// }
out.add(HexUtil.bytesToHexString(bytesCache)); out.add(HexUtil.bytesToHexString(bytesCache));
} }
} }
private boolean verifyCRC(byte[] bytes) {
byte[] crc = ByteUtils.subArray(bytes, bytes.length - 2, bytes.length);
byte[] data = ByteUtils.subArray(bytes, 0, bytes.length - 2);
String generate = CRCUtil.getCRC(data);
String original = HexUtil.bytesToHexString(crc);
return original.equalsIgnoreCase(generate);
}
} }