update 修改crc校验方法

This commit is contained in:
Lemon
2023-06-29 09:16:50 +08:00
parent f742f08fba
commit caddcd9fd9
2 changed files with 29 additions and 19 deletions

View File

@@ -49,12 +49,17 @@ public class YKCUtils {
String sourceCRC = String.format("%04x", BytesUtil.bytesToInt(crcByte, 0));
String targetCRC = String.format("%04x", CRC16Util.calcCrc16(data));
String oldTargetCRC = String.format("%04x", CRC16Util.calcCrc16Old(data));
if (!sourceCRC.equalsIgnoreCase(targetCRC)) {
log.error("CRC校验不通过, 源crc:{}, 计算得出crc:{}, 老crc计算:{}, 帧类型:{}, 报文:{}, msg:{}"
, sourceCRC, targetCRC, oldTargetCRC, YKCUtils.frameType2Str(frameType), BytesUtil.binary(msg, 16), Arrays.toString(msg));
return false;
// 将高低位位置反转得出新的crc
String lowString = StringUtils.substring(targetCRC, 0, 2);
String highString = StringUtils.substring(targetCRC, 2, 4);
String crc = highString + lowString;
// 若目标crc和高低位反转前/后的crc都不一致则校验不通过
if (sourceCRC.equalsIgnoreCase(targetCRC) || sourceCRC.equalsIgnoreCase(crc)) {
return true;
}
return true;
log.error("CRC校验不通过, 源crc:{}, 计算得出crc:{}, 老crc计算:{}, 高低位反转后crc:{}, 帧类型:{}, 报文:{}, msg:{}"
, sourceCRC, targetCRC, oldTargetCRC, crc, YKCUtils.frameType2Str(frameType), BytesUtil.binary(msg, 16), Arrays.toString(msg));
return false;
}
/**
@@ -127,6 +132,11 @@ public class YKCUtils {
// System.out.println(binary);
// System.out.println(aaa);
String targetCRC = "0abb";
String substring = StringUtils.substring(targetCRC, 0, 2);
String substring1 = StringUtils.substring(targetCRC, 2, 4);
String crc = substring1 + substring;
String hexString = "4f";
byte[] bytes = new byte[]{0x4f};