mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 19:15:35 +08:00
update 电单车协议
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.jsowell.common;
|
||||
|
||||
import com.jsowell.common.util.BytesUtil;
|
||||
import com.jsowell.common.util.YKCUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -87,4 +88,49 @@ public class YouDianUtils {
|
||||
int calculatedChecksum = calculateCheckField(bytes);
|
||||
return BytesUtil.intToBytesLittle(calculatedChecksum);
|
||||
}
|
||||
|
||||
// public static String convertPortNumberToHex(int portNumber) {
|
||||
// if (portNumber < 1 || portNumber > 16) {
|
||||
// throw new IllegalArgumentException("Port number must be between 1 and 16.");
|
||||
// }
|
||||
//
|
||||
// // 端口号从1开始,因此需要减去1
|
||||
// int hexValue = portNumber - 1;
|
||||
//
|
||||
// // 转换为16进制字符串,并确保长度为2位
|
||||
// String hexString = String.format("0x%02X", hexValue);
|
||||
//
|
||||
// return hexString;
|
||||
// }
|
||||
|
||||
public static byte[] convertPortNumberToBytes(int portNumber) {
|
||||
if (portNumber < 1 || portNumber > 16) {
|
||||
throw new IllegalArgumentException("Port number must be between 1 and 16.");
|
||||
}
|
||||
|
||||
// 端口号从1开始,因此需要减去1
|
||||
int hexValue = portNumber - 1;
|
||||
|
||||
// 转换为字节数组
|
||||
byte[] bytes = new byte[2];
|
||||
bytes[0] = (byte) 0x00; // 前缀0x
|
||||
bytes[1] = (byte) hexValue;
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
byte[] result1 = convertPortNumberToBytes(1); // 输出: [0, 0]
|
||||
byte[] result2 = convertPortNumberToBytes(2); // 输出: [0, 1]
|
||||
byte[] result10 = convertPortNumberToBytes(10); // 输出: [0, 9]
|
||||
byte[] result11 = convertPortNumberToBytes(11); // 输出: [0, 10]
|
||||
byte[] result16 = convertPortNumberToBytes(16); // 输出: [0, 15]
|
||||
|
||||
// 打印结果
|
||||
System.out.println(YKCUtils.frameType2Str(result1));
|
||||
System.out.println(YKCUtils.frameType2Str(result2));
|
||||
System.out.println(YKCUtils.frameType2Str(result10));
|
||||
System.out.println(YKCUtils.frameType2Str(result11));
|
||||
System.out.println(YKCUtils.frameType2Str(result16));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user