fix: 透传-modbus组件

V0.5.x
jay 2023-07-25 15:34:34 +08:00
parent 933104863c
commit f4a6517037
3 changed files with 25 additions and 6 deletions

View File

@ -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,

View File

@ -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));