mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 19:45:09 +08:00
update 汇付协议
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.jsowell.common;
|
||||
|
||||
import com.jsowell.common.util.BytesUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 友电电单车充电桩协议工具类
|
||||
*/
|
||||
@Slf4j
|
||||
public class YouDianUtils {
|
||||
|
||||
/**
|
||||
* 校验方法
|
||||
* 整个数据包中的每个字节(不包括校验字段本身),将它们的数值累加起来。然后取累加和的低2字节(16位),作为校验字段的值
|
||||
*/
|
||||
public static boolean validateChecksum(byte[] bytes) {
|
||||
if (bytes.length < 2) {
|
||||
return false; // 校验字段长度不足时返回 false
|
||||
}
|
||||
|
||||
// 计算累加和
|
||||
int sum = 0;
|
||||
for (int i = 0; i < bytes.length - 2; i++) {
|
||||
sum += (bytes[i] & 0xFF); // 将每个字节视为无符号值进行累加
|
||||
}
|
||||
|
||||
// 取累加和的低 2 字节(16 位)
|
||||
int calculatedChecksum = sum & 0xFFFF;
|
||||
|
||||
// 读取校验字段的值
|
||||
byte[] checksumBytes = {bytes[bytes.length - 2], bytes[bytes.length - 1]};
|
||||
int receivedChecksum = BytesUtil.bytesToIntLittle(checksumBytes);
|
||||
|
||||
// 比较计算的校验值和接收到的校验值
|
||||
log.info("计算的校验值:{}, 接收到的校验值:{}", calculatedChecksum, receivedChecksum);
|
||||
return calculatedChecksum == receivedChecksum;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user