|
|
@@ -9,7 +9,7 @@ public class ProtocolUtils {
|
|
|
/**
|
|
|
* BCD 转换为十进制
|
|
|
*/
|
|
|
- public static int bcdToInt(byte b) {
|
|
|
+ private static int bcdToInt(byte b) {
|
|
|
return ((b >> 4) & 0x0F) * 10 + (b & 0x0F);
|
|
|
}
|
|
|
|
|
|
@@ -17,28 +17,37 @@ public class ProtocolUtils {
|
|
|
* 解析 6 字节 BCD 时间戳
|
|
|
* 格式:YY MM DD HH mm ss
|
|
|
*/
|
|
|
- public static LocalDateTime parseTimestamp(byte[] data, int offset) {
|
|
|
- int year = bcdToInt(data[offset]);
|
|
|
- int month = bcdToInt(data[offset + 1]);
|
|
|
- int day = bcdToInt(data[offset + 2]);
|
|
|
- int hour = bcdToInt(data[offset + 3]);
|
|
|
- int minute = bcdToInt(data[offset + 4]);
|
|
|
- int second = bcdToInt(data[offset + 5]);
|
|
|
- return LocalDateTime.of(2000 + year, month, day, hour, minute, second);
|
|
|
+ public static LocalDateTime parseTimestamp(byte[] frame, int offset) {
|
|
|
+ int year = 2000 + bcdToInt(frame[offset]);
|
|
|
+ int month = bcdToInt(frame[offset + 1]);
|
|
|
+ int day = bcdToInt(frame[offset + 2]);
|
|
|
+ int hour = bcdToInt(frame[offset + 3]);
|
|
|
+ int minute = bcdToInt(frame[offset + 4]);
|
|
|
+ int second = bcdToInt(frame[offset + 5]);
|
|
|
+ return LocalDateTime.of(year, month, day, hour, minute, second);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 小端序字节数组转 float
|
|
|
*/
|
|
|
- public static float bytesToFloatLE(byte[] data, int offset) {
|
|
|
- byte[] tmp = new byte[4];
|
|
|
- tmp[0] = data[offset];
|
|
|
- tmp[1] = data[offset + 1];
|
|
|
- tmp[2] = data[offset + 2];
|
|
|
- tmp[3] = data[offset + 3];
|
|
|
- return ByteBuffer.wrap(tmp).order(ByteOrder.LITTLE_ENDIAN).getFloat();
|
|
|
+ public static float bytesToFloatLE(byte[] bytes, int offset) {
|
|
|
+ int bits = (bytes[offset] & 0xFF)
|
|
|
+ | ((bytes[offset + 1] & 0xFF) << 8)
|
|
|
+ | ((bytes[offset + 2] & 0xFF) << 16)
|
|
|
+ | ((bytes[offset + 3] & 0xFF) << 24);
|
|
|
+ return Float.intBitsToFloat(bits);
|
|
|
+ }
|
|
|
+ public static float readFloat6(byte[] frame, int offset) {
|
|
|
+ // 小端转大端:取后4字节反转后再解析
|
|
|
+ byte[] floatBytes = new byte[4];
|
|
|
+ floatBytes[0] = frame[offset + 5];
|
|
|
+ floatBytes[1] = frame[offset + 4];
|
|
|
+ floatBytes[2] = frame[offset + 3];
|
|
|
+ floatBytes[3] = frame[offset + 2];
|
|
|
+ return ByteBuffer.wrap(floatBytes).order(ByteOrder.BIG_ENDIAN).getFloat();
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 字节数组转 Hex 字符串
|
|
|
*/
|
|
|
@@ -53,12 +62,13 @@ public class ProtocolUtils {
|
|
|
/**
|
|
|
* 16进制字符串转字节数组
|
|
|
*/
|
|
|
- public static byte[] hexStringToBytes(String s) {
|
|
|
- int len = s.length();
|
|
|
+ public static byte[] hexStringToBytes(String hex) {
|
|
|
+ hex = hex.replaceAll("\\s+", "");
|
|
|
+ int len = hex.length();
|
|
|
byte[] data = new byte[len / 2];
|
|
|
for (int i = 0; i < len; i += 2) {
|
|
|
- data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
|
|
|
- + Character.digit(s.charAt(i+1), 16));
|
|
|
+ data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
|
|
|
+ + Character.digit(hex.charAt(i + 1), 16));
|
|
|
}
|
|
|
return data;
|
|
|
}
|