1

With an array of ASCII values I Node-RED/JS, I need to convert everything into one long string. To handle a varying number of values in the array, I found String.fromCharCode.apply(null, msg.payload); to be great, and my payload is converted as expected - or at least in part.

The payload consists of a number of bools and ints, and a string in the end. The string it output just fine, but the ints and bools (3s and 0s for the time being) are not converted. The array contains 121 ASCII values, but the string output is only ~77 characters long. In other words,

[0,45,83,51,0,0,0,0,1,0,0,0,14,0,202,0,19,162,0,0,0,0,0,0,3,0,3,0,3,0,3,0,3,0,3,0,3,0,3,0,49,53,55,52,48,55,57,54,57,50,56,48,54,48,48,48,48,48,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,80,70,86,32,1,0,0,99,103,114,111,117,112,47,99,32,0,0,0,0,0,0,0,0,0,0,0,0,71,80,82,77,67,44,49,50,50,49,51,50,46,48,48,44,65,44,52,52,52,52,46,50,48,56,48,53,44,78,44,48,48,52,52,52,46,50,55,51,50,51,44,69,44,48,46,48,49,51,44,44,49,56,49,49,49,57,44,44,44,68,42,55,55,13,0,13,0,48,48,48,42,54,68,13,0,50,44,0,0]

is converted into something like

-S3yÆ1574241874748,092421.00,A,1324.56789,N,12345.56789,E,0.024,,201119,,,D*73

while it would expect it to be something like

0,0,0,0,3,3,3,3,3,3,3,3,1574241874748,092421.00,A,1324.56789,N,12345.56789,E,0.024,,201119,,,D*73

Note: -S3yÆ isn't exactly the output at the beginning, because there is some special character in there (displayed as Æ, but I don't think it really is, and it also varies and can't be handled by the clipboard). I suspect this is the culprit which needs to be taken care of. There are a lot of null values in the input, and I suspect these are causing fromCharCode problems.

hardillb
  • 54,545
  • 11
  • 67
  • 105
KMBN
  • 73
  • 2
  • 6
  • What you need is to know the protocol and ideally the documentation (or library which would understand the protocol) but first you would need to know what the protocol is that is used to transmit the data. Without knowing the protocol you are left with reverse engineering it to figure out how to parse the data. – slebetman Nov 20 '19 at 10:22

1 Answers1

2

What you are trying to do is parse a binary packed data structure. Trying to just convert the whole thing to string a char at a time is not the right approach.

The string looks to be a NMEA GPS string so I would suggest looking at something like the npm gps-module as a starting point probably for a custom Node-RED node or at least for a hint to what you would include in a function node.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • 2
    see also https://stackoverflow.com/questions/5605108/pack-unpack-functions-for-node-js – georg Nov 20 '19 at 10:08
  • Although it is pronounced "nema" it is spelled NMEA. But I thought NMEA packets are ASCII based? This looks binary with lots of nulls in there – slebetman Nov 20 '19 at 10:24
  • You are right that the data is /in part/ a NMEA string, but only the last part. The whole array of data originates from a PLC, to which a GPS receiver is connected. The PLC outputs a number of boolean and analog values as well, and everything is then spat out as a UDP message. Using the existing netvar-receiver node works to some extent, but seems to have a bug, which makes it impossible to have two separate string values in the output, at least in our current configuration. – KMBN Nov 20 '19 at 10:25
  • Then also look at the [node-red-contrib-binary](https://flows.nodered.org/node/node-red-contrib-binary) – hardillb Nov 20 '19 at 10:27