update 电单车协议

This commit is contained in:
Guoqs
2024-08-26 15:48:22 +08:00
parent c091b6dea7
commit 8d278745b4
11 changed files with 126 additions and 26 deletions

View File

@@ -35,4 +35,26 @@ public class YouDianUtils {
log.info("计算的校验值:{}, 接收到的校验值:{}", calculatedChecksum, receivedChecksum);
return calculatedChecksum == receivedChecksum;
}
/**
* 计算校验字段
*/
public static int calculateCheckField(byte[] bytes) {
// 计算累加和
int sum = 0;
for (int i = 0; i < bytes.length - 2; i++) {
sum += (bytes[i] & 0xFF); // 将每个字节视为无符号值进行累加
}
// 取累加和的低 2 字节16 位)
return sum & 0xFFFF;
}
/**
* 获取校验字段byte数组
*/
public static byte[] getCheckFieldBytes(byte[] bytes) {
int calculatedChecksum = calculateCheckField(bytes);
return BytesUtil.intToBytesLittle(calculatedChecksum);
}
}

View File

@@ -67,7 +67,10 @@ public class EBikeDataProtocol {
* @return 报文
*/
public String getHEXString() {
byte[] bytes = Bytes.concat(this.head, this.length, this.physicalId, this.messageId, this.command, this.msgBody, this.checksum);
return BytesUtil.binary(bytes, 16);
return BytesUtil.binary(this.getBytes(), 16);
}
public byte[] getBytes() {
return Bytes.concat(this.head, this.length, this.physicalId, this.messageId, this.command, this.msgBody, this.checksum);
}
}