mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 02:55:04 +08:00
Merge branch 'dev-new' into dev-new-rabbitmq
# Conflicts: # jsowell-netty/src/main/java/com/jsowell/netty/server/yunkuaichong/NettyServerChannelInitializer.java
This commit is contained in:
@@ -61,13 +61,20 @@ public class YKCDataProtocol {
|
||||
this.crcByte = new byte[]{msg[msg.length - 2], msg[msg.length - 1]};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字节数组
|
||||
*/
|
||||
public byte[] getBytes() {
|
||||
return Bytes.concat(this.head, this.length, this.serialNumber, this.encryptFlag, this.frameType, this.msgBody, this.crcByte);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为十六进制字符串
|
||||
*
|
||||
* @return 报文
|
||||
*/
|
||||
public String getHEXString() {
|
||||
byte[] bytes = Bytes.concat(this.head, this.length, this.serialNumber, this.encryptFlag, this.frameType, this.msgBody, this.crcByte);
|
||||
byte[] bytes = getBytes();
|
||||
return BytesUtil.binary(bytes, 16);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,6 +186,20 @@ public class CRC16Util {
|
||||
|
||||
/*以下方法得出的校验位:低位在前,高位在后*/
|
||||
|
||||
public static short calculateCrc(short serialNumber, byte encryptFlag, byte frameType, byte[] messageBody) {
|
||||
// short serialNumber 转byte[]
|
||||
byte[] serialNumberBytes = new byte[2];
|
||||
// byte encryptFlag 转byte[]
|
||||
byte[] encryptFlagBytes = new byte[1];
|
||||
// byte frameType 转byte[]
|
||||
byte[] frameTypeBytes = new byte[1];
|
||||
// byte[] messageBody 转byte[]
|
||||
// 序列号域+加密标志+帧类型标志+消息体
|
||||
byte[] data = Bytes.concat(serialNumberBytes, encryptFlagBytes, frameTypeBytes, messageBody);
|
||||
int i = calcCrc16(data);
|
||||
return (short) i;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param data 需要计算的数组
|
||||
* @return CRC16校验值
|
||||
|
||||
@@ -52,6 +52,13 @@ public class YKCUtils {
|
||||
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);
|
||||
// 校验长度
|
||||
@@ -77,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