mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 02:55:04 +08:00
使用YKCDataProtocol处理
This commit is contained in:
@@ -84,6 +84,60 @@ public class YKCUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean checkMsg(YKCDataProtocol ykcDataProtocol) {
|
||||
// log.info("checkMsg:{}", BytesUtil.binary(msg, 16));
|
||||
// 起始标志
|
||||
byte[] head = ykcDataProtocol.getHead();
|
||||
// 数据长度
|
||||
byte[] length = ykcDataProtocol.getLength();
|
||||
// 序列号域
|
||||
byte[] serialNumber = ykcDataProtocol.getSerialNumber();
|
||||
// 加密标志
|
||||
byte[] encryptFlag = ykcDataProtocol.getEncryptFlag();
|
||||
// 帧类型标志
|
||||
byte[] frameType = ykcDataProtocol.getFrameType();
|
||||
// 消息体
|
||||
byte[] msgBody = ykcDataProtocol.getMsgBody();
|
||||
// 帧校验域
|
||||
byte[] crcByte = ykcDataProtocol.getCrcByte();
|
||||
|
||||
//起始位必须是0x68
|
||||
if (0x68 != head[0]) {
|
||||
log.error("起始位必须是0x68");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果是0x03帧,则不进行crc校验
|
||||
if (0x03 == frameType[0]) {
|
||||
log.info("0x03帧,则不进行crc校验");
|
||||
return true;
|
||||
}
|
||||
|
||||
// 序列号域+加密标志+帧类型标志+消息体
|
||||
byte[] data = Bytes.concat(serialNumber, encryptFlag, frameType, msgBody);
|
||||
// 校验长度
|
||||
if (data.length != BytesUtil.bytesToIntLittle(length)) {
|
||||
log.error("数据长度不正确, 数据长度:{}, 实际长度:{}", BytesUtil.bytesToIntLittle(length), data.length);
|
||||
return false;
|
||||
}
|
||||
// CRC校验 source target
|
||||
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));
|
||||
// 将高低位位置反转,得出新的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;
|
||||
}
|
||||
log.error("CRC校验不通过, 源crc:{}, 计算得出crc:{}, 老crc计算:{}, 高低位反转后crc:{}, 帧类型:{}, 帧名称:{}, 报文:{}"
|
||||
, sourceCRC, targetCRC, oldTargetCRC, crc, YKCUtils.frameType2Str(frameType),
|
||||
YKCFrameTypeCode.getFrameTypeStr(YKCUtils.frameType2Str(frameType)), BytesUtil.binary(ykcDataProtocol.getBytes(), 16));
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取结果报文
|
||||
* @param ykcDataProtocol
|
||||
|
||||
Reference in New Issue
Block a user