ソースを参照

fix: 透传-modbus组件

jay 1 年間 前
コミット
f4a6517037

BIN
data/components/750da259-ff77-4869-a5a5-12a5433b0e4c/iot-nb-component-0.4.5-SNAPSHOT.jar


+ 22 - 5
iot-common/iot-common-core/src/main/java/cc/iotkit/common/utils/HexUtil.java

@@ -228,13 +228,30 @@ public class HexUtil {
     }
 
     /**
-     * 计算CRC16校验
+     * 16进制表示的字符串转换为字节数组
      *
-     * @param data   需要计算的数组
-     * @param offset 起始位置
-     * @param len    长度
-     * @return CRC16校验值
+     * @param s 16进制表示的字符串
+     * @return byte[] 字节数组
      */
+    public static byte[] hexStringToByteArray(String s) {
+        int len = s.length();
+        byte[] b = new byte[len / 2];
+        for (int i = 0; i < len; i += 2) {
+            // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
+            b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
+                    .digit(s.charAt(i + 1), 16));
+        }
+        return b;
+
+    }
+        /**
+         * 计算CRC16校验
+         *
+         * @param data   需要计算的数组
+         * @param offset 起始位置
+         * @param len    长度
+         * @return CRC16校验值
+         */
     public static int calcCrc16(byte[] data, int offset, int len) {
         byte[] crc16_tab_h = {
                 (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0,

+ 3 - 1
iot-components/iot-nb-component/src/main/java/cc/iotkit/comp/nb/NBVerticle.java

@@ -11,6 +11,7 @@ package cc.iotkit.comp.nb;
 
 import cc.iotkit.common.enums.ErrCode;
 import cc.iotkit.common.exception.BizException;
+import cc.iotkit.common.utils.HexUtil;
 import cc.iotkit.comp.IMessageHandler;
 import cc.iotkit.comp.model.ReceiveResult;
 import io.netty.handler.codec.mqtt.MqttConnectReturnCode;
@@ -211,7 +212,8 @@ public class NBVerticle extends AbstractVerticle {
         if (endpoint == null) {
             throw new BizException(ErrCode.SEND_DESTINATION_NOT_FOUND);
         }
-        Future<Integer> result = endpoint.publish(topic, Buffer.buffer(msg),
+        byte[] bytes = HexUtil.hexStringToByteArray(msg);
+        Future<Integer> result = endpoint.publish(topic, Buffer.buffer(bytes),
                 MqttQoS.AT_LEAST_ONCE, false, false);
         result.onFailure(e -> log.error("public topic failed", e));
         result.onSuccess(integer -> log.info("publish success,topic:{},payload:{}", topic, msg));