|
@@ -0,0 +1,68 @@
|
|
|
+package com.gzlh.device.led.utils;
|
|
|
+
|
|
|
+import com.gzlh.device.led.action.FengLiYuanCmdEnum;
|
|
|
+import com.gzlh.utils.WordHandlerUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+public class FengLiYuanPackage {
|
|
|
+ /**
|
|
|
+ * 帧头
|
|
|
+ */
|
|
|
+ private static final String HEAD = "F501";
|
|
|
+
|
|
|
+ private static final String LedAddr="01";
|
|
|
+
|
|
|
+ private static final String send="01";
|
|
|
+
|
|
|
+
|
|
|
+ public static String buildPackage(String command,String data) {
|
|
|
+ //包数据= 起始字(两字节长度,固定0xF5,0x01)+长度(两字节长度)+屏地址+命令+发送+数据+总包校验
|
|
|
+ //长度=2Byte,从[屏地址]到[数据]最后字节。高字节在前,低字节在后。
|
|
|
+ //发送=0X00=发送,不需要应答,0X01=发送,需要应答
|
|
|
+ //数据=N Byte,可以为空
|
|
|
+ //总包校验=1byte,是从[起始字]到[数据]最后字节所有字节的异或校验
|
|
|
+
|
|
|
+ String str=LedAddr+command+send+data;
|
|
|
+
|
|
|
+ StringBuilder len = new StringBuilder(Integer.toHexString(str.length() / 2));
|
|
|
+ if (len.length()>4){
|
|
|
+ throw new RuntimeException("数据超出长度");
|
|
|
+ }
|
|
|
+ while (len.length()<4){
|
|
|
+ len.insert(0, "0");
|
|
|
+ }
|
|
|
+ String dataStr = HEAD+len+str;
|
|
|
+
|
|
|
+ return xorComputeResult(dataStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ //输入字符串进行异或校验,并加上校验结果
|
|
|
+ public static String xorComputeResult(String data) {
|
|
|
+
|
|
|
+ if (data.length()%2!=0){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ int xorCheckSum = 0;
|
|
|
+ List<String> list= new ArrayList<>();
|
|
|
+ for (int i=0;i<data.length();i+=2){
|
|
|
+ list.add(data.substring(i,i+2));
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ xorCheckSum^=Integer.valueOf(list.get(i),16);
|
|
|
+ }
|
|
|
+// System.out.println(xorCheckSum);
|
|
|
+ String hex = Integer.toHexString(xorCheckSum);
|
|
|
+ if (hex.length() == 1) {
|
|
|
+ hex = '0' + hex;
|
|
|
+ }
|
|
|
+
|
|
|
+ list.add(hex.toUpperCase(Locale.getDefault()));
|
|
|
+
|
|
|
+ String str = list.toString().substring(1, list.toString().length() - 1);
|
|
|
+ return str.replaceAll(", ", "");
|
|
|
+ }
|
|
|
+}
|