简体   繁体   English

Lora 数据包数据编码为十六进制字符串,但解码为 ASCII 显示无意义

[英]Lora packet data is encoded as hex string but decoding to ASCII is showing nonsense

I am using 2 LHT65N sensors, a Wisgate Edge Lite 2 Gateway, forwarding the payload to a Mosquitto broker, and to display data I am using mqtt.js.我正在使用 2 个 LHT65N 传感器、一个 Wisgate Edge Lite 2 网关,将有效载荷转发到 Mosquitto 代理,并显示我正在使用 mqtt.js 的数据。

I am receiving packets from the sensors and the data I get is a hex string, but when I am decoding it to plaintext I get no useful data.我从传感器接收数据包,我得到的数据是一个十六进制字符串,但是当我将它解码为明文时,我没有得到任何有用的数据。

So this is the object I get:所以这是我得到的 object:

{
"applicationID": "1",
"applicationName": "ptxd_temp_humid",
"devEUI": "a84041a777864399",
"deviceName": "sensor_uphill",
"timestamp": 1674051438,
"fCnt": 179,
"fPort": 2,
"data": "CBCF09200203017FFF7FFF",
"data_encode": "hexstring",
"adr": true,
"rxInfo": [
    {
        "gatewayID": "ac1f09fffe08bf79",
        "loRaSNR": 0,
        "rssi": -59,
        "location": {
            "latitude": 0,
            "longitude": 0,
            "altitude": 0
        }
    }
],
"txInfo": {
    "frequency": 868800000,
    "dr": 7
}

You can see that the packet data is encoded as a hex string.您可以看到数据包数据被编码为十六进制字符串。

I am using this method:我正在使用这种方法:

decoder(bytes: any) {
  var batt_v = ((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000;
  var temp_int = ((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2);
  var temp_ext = ((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2);
  var hum_int = ((bytes[4]<<8 | bytes[5])/10).toFixed(1);
  var ext_sen = 
     {
       "0":"No external sensor",
       "1":"Temperature Sensor",
     }[bytes[6]&0x7F];

  return {
    Ext_sensor: ext_sen,
    BatV: batt_v,
    TempC_SHT: temp_int,
    Hum_SHT: hum_int,
    TempC_DS: temp_ext,
  };

To achieve this:为达到这个:

{
   "BatV": 1,
   "TempC_SHT": "27.56",
   "Hum_SHT": "50.6",
   "TempC_DS": "25.23"
 }

But I get:但我得到:

{
  "BatV": 0,
  "TempC_SHT": "0.00",
  "Hum_SHT": "0.9",
  "TempC_DS": "0.00"
}

I feel that the problem is the gateway.我感觉是网关的问题。 The system log of the gateway is already receiving the encoded data.网关的系统日志已经在接收编码数据。 I tried a hex converter and also the payload formatter of the LHT65N but neither gave me the correct data.我尝试了十六进制转换器和 LHT65N 的有效载荷格式化程序,但都没有给我正确的数据。 I also triple checked the API key, but it is correct.我还三次检查了 API 密钥,但它是正确的。

Is there any configuration that I have to do to receive the correct data?是否需要进行任何配置才能接收正确的数据?

Solution解决方案

I was passing the wrong type into the method.我将错误的类型传递给方法。 I was confused because I compared my data to others and mine appeared to be too short so I was pretty confused.我很困惑,因为我将自己的数据与其他人进行了比较,而我的数据似乎太短了,所以我很困惑。 I am decoding my hexstring to a UInt8Array now and it's working as expected.我现在正在将我的 hexstring 解码为 UInt8Array,它按预期工作。

    client.on('message', (topic, message: Buffer, packet: 
              mqtt.IPublishPacket) => {
    
      let payload = JSON.parse(message.toString())
      console.log(Buffer.from(payload.data, 'hex'))
      this.data = this.Decoder(Buffer.from(payload.data, 'hex'));
      console.log(this.data)
  
    })

The hex string "data": "CBCF09200203017FFF7FFF" that's received decodes properly with the code you showed.收到的十六进制字符串"data": "CBCF09200203017FFF7FFF"使用您显示的代码正确解码。 Running it against the hex string, I get:针对十六进制字符串运行它,我得到:

{
  Ext_sensor: 'Temperature Sensor',
  BatV: 3.023,
  TempC_SHT: '23.36',
  Hum_SHT: '51.5',
  TempC_DS: '327.67'
}

So that seems to be working.所以这似乎有效。 If this arrives via the gateway all the way to the MQTT broker, the problem isn't with the gateway then.如果它通过网关一直到达 MQTT 代理,那么问题就不在网关上了。 It is with either the broker, or the decoder itself, but then again, as I said, the decoder's code seems to be working.它与代理或解码器本身有关,但话又说回来,正如我所说,解码器的代码似乎可以正常工作。 It looks like you are getting wrong data, somewhere between it is sent to the decoder and the time it is returned to you.看起来你得到了错误的数据,在它被发送到解码器和它返回给你的时间之间的某个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM