NMEA String to array in a function node

Good day everyone,

I am trying to do the following:

I am getting a NMEA string, I split it and then try to put the split messages into msg.array
But the result is, is that I only get msg.payload out, and not an array or msg.array
image

I have this inside my function node:

//Example of data received:
//GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,

message0 = {};
message1 = {};
message2 = {};
message3 = {};
message4 = {};
message5 = {};
message6 = {};
message7 = {};
message8 = {};
message9 = {};
message10 = {};
message11 = {};

msg.string = msg.payload.split(",");

message0=msg.string[0];//GPRMC
message1=msg.string[1];//123519
message2=msg.string[2];//A
message3=msg.string[3];//4807.038
message4=msg.string[4];//N
message5=msg.string[5];//01131.000
message6=msg.string[6];//E
message7=msg.string[7];//022.4
message8=msg.string[8];//084.4
message9=msg.string[9];//230394
message10=msg.string[10];//003.1
message11=msg.string[11];//W*6A

msg.array = {
f1: message0,
f2: message1,
f3: message2,
f4: message3,
f5: message4,
f6: message5,
f7: message6,
f8: message7,
f9: message8,
f10: message9,
f11: message10,
f12: message11
}

return [msg];

What am I doing wrong?

this is a better solution...

var parts = msg.payload.split(",");
msg.payload = {
    f1: parts[0],
    f2: parts[1],
    f3: parts[2],
    f4: parts[3],
    f5: parts[4],
    f6: parts[5],
    f7: parts[6],
    f8: parts[7],
    f9: parts[8],
    f10: parts[9],
    f11: parts[10],
    f12: parts[11]
}
return msg;

Thanks Steve,
But I still get the whole string out, under payload.

Very odd.

Show me your flow.

[{"id":"28f1a191.3a73be","type":"serial in","z":"88b3310a.f89c9","name":"","serial":"ffea0674.ed51b8","x":190,"y":380,"wires":[["c8d23d1d.08b15","9f17b499.4c96e8"]]},{"id":"c8d23d1d.08b15","type":"debug","z":"88b3310a.f89c9","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":448,"y":329,"wires":[]},{"id":"dc23c87c.8960c8","type":"function","z":"88b3310a.f89c9","name":"","func":"var parts = msg.payload.split(\",\");\nmsg.payload = {\n    f1: parts[0],\n    f2: parts[1],\n    f3: parts[2],\n    f4: parts[3],\n    f5: parts[4],\n    f6: parts[5],\n    f7: parts[6],\n    f8: parts[7],\n    f9: parts[8],\n    f10: parts[9],\n    f11: parts[10],\n    f12: parts[11]\n}\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":380,"wires":[[]]},{"id":"9f17b499.4c96e8","type":"string","z":"88b3310a.f89c9","name":"","methods":[{"name":"delLeftMost","params":[{"type":"str","value":"$"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":530,"y":380,"wires":[["dc23c87c.8960c8","7afe88a.2fbbb78"]]},{"id":"7afe88a.2fbbb78","type":"debug","z":"88b3310a.f89c9","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":850,"y":380,"wires":[]},{"id":"ffea0674.ed51b8","type":"serial-port","z":"","serialport":"/dev/ttyAMA0","serialbaud":"4800","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

Hi, in order to help, I need a bit more info.

As i do not have your serial device and dont use that string node, please capture a full msg by adding a debug and use the "COPY VALUE" button on the debug panel

Using "copy value"...
image

Paste that into a reply

Actually, please provide both...

The STRING node only removes the $ character from the incoming NMEA string.
Your first debug node: $YXXDR,C,,C,WCHR,C,,C,WCHT,C,,C,HINX,P,1.0136,B,STNP4E
Your second Debug node: YXXDR,C,,C,WCHR,C,,C,WCHT,C,,C,HINX,P,1.0136,B,STNP
4E

you didnt capture the full msg as requested. i need to see where properties are in the msg

I just removed the STRING node, and this was the result.


Somehow that string node has influence on it.

Thanks for making me think :+1:

also, if you change the function node to this...

var parts = msg.payload.split(",");
msg.payload = {
    f1: (parts[0]+"").substr(1),
    f2: parts[1],
    f3: parts[2],
    f4: parts[3],
    f5: parts[4],
    f6: parts[5],
    f7: parts[6],
    f8: parts[7],
    f9: parts[8],
    f10: parts[9],
    f11: parts[10],
    f12: parts[11]
}
return msg;

it will remove the 1st character.

For splitting, can I use an OR function as well?
Right now, it does split on ",", but as you can see in F5: E48, the checksum in together with the last field recieved.
I do not always get 5 field, i sometimes get less or more, so I do not always know, what the last field is. How can I make the split also happen on "
"

image

As you are recieving different data you need a means of identiying the string format & run different routines.

Send me 2 example (different) stirngs.

Also, provide the format e.g...

String1 "standard message"

part1: header (string)
part2: elevation (number/float)
part3: atmospheres (number/float)
part4: etc

String2 "standard message with checksum"

part1: header (string)
part2: elevation (number/float)
part3: atmospheres (number/float)
part4: etc
part5+6: combined (separated by a * 5=checksum (byte), 6=bblahblah (1 char string)

I'll show you a better way to do this.

$HCHDG,0.1,0.0,E,0.8,E *4B
$WIVWR,134.2,L,1.4,N,0.7,M,2.6,K *4B
$WIMWD,230.5,T,229.7,M,1.4,N,0.7,M 52
$WIMDA,29.9405,I,1.0139,B,18.8,C,,,31.1,,1.3,C,224.7,T,223.9,M,1.3,N,0.7,M **5C
"$YXXDR,A,-2.8,D,PTCH,A,-0.6,D,ROLL
51

These are some of the sentences i am receiving from an Airmar WX150 weather station

The site seem to remove some of the astriks's.
the last 2 carachters, are the checksum wich is after an astriks

you didnt provide the parts format/definitions.

Here is something to get you started (I made up the definitions)...

[{"id":"11af16fd.dd46a9","type":"inject","z":"4b3f21a3.ba434","name":"$HCHDG","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$HCHDG,0.1,0.0,E,0.8,E *4B","payloadType":"str","x":740,"y":2320,"wires":[["ae7daa01.2afd78"]]},{"id":"b1f56fc4.3e27b","type":"inject","z":"4b3f21a3.ba434","name":"$WIVWR","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$WIVWR,134.2,L,1.4,N,0.7,M,2.6,K *4B","payloadType":"str","x":740,"y":2360,"wires":[["ae7daa01.2afd78"]]},{"id":"8ace5da4.fc83","type":"inject","z":"4b3f21a3.ba434","name":"$WIMWD","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$WIMWD,230.5,T,229.7,M,1.4,N,0.7,M 52","payloadType":"str","x":740,"y":2400,"wires":[["ae7daa01.2afd78"]]},{"id":"30d3107f.156a1","type":"inject","z":"4b3f21a3.ba434","name":"$WIMDA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$WIMDA,29.9405,I,1.0139,B,18.8,C,,,31.1,,1.3,C,224.7,T,223.9,M,1.3,N,0.7,M **5C","payloadType":"str","x":740,"y":2440,"wires":[["ae7daa01.2afd78"]]},{"id":"9defbdeb.7d00b","type":"inject","z":"4b3f21a3.ba434","name":"$YXXDR","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$YXXDR,A,-2.8,D,PTCH,A,-0.6,D,ROLL51","payloadType":"str","x":740,"y":2480,"wires":[["ae7daa01.2afd78"]]},{"id":"ae7daa01.2afd78","type":"switch","z":"4b3f21a3.ba434","name":"","property":"$substring(payload, 0, 6)","propertyType":"jsonata","rules":[{"t":"eq","v":"$HCHDG","vt":"str"},{"t":"eq","v":"$WIVWR","vt":"str"},{"t":"eq","v":"$WIMWD","vt":"str"},{"t":"eq","v":"$WIMDA","vt":"str"},{"t":"eq","v":"$YXXDR","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":6,"x":950,"y":2400,"wires":[["6726ac9a.e30534"],["9c292a3.c465bd8"],["ad7cf4a.dfe9608"],[],[],["9ab6ee4.177691"]]},{"id":"9284bec5.12121","type":"comment","z":"4b3f21a3.ba434","name":"example NMEA strings","info":"","x":760,"y":2280,"wires":[]},{"id":"6726ac9a.e30534","type":"function","z":"4b3f21a3.ba434","name":"Parse $HCHDG","func":"\n//PART   : 0      1   2   3 4   5\n//SAMPLE : $HCHDG,0.1,0.0,E,0.8,E *4B\n\nvar parts = msg.payload.split(\",\");\nvar checksum = parts[5].split(\" *\")[1];\n\nmsg.payload = {\n    raw: msg.payload,\n    type: (parts[0] + \"\").substr(1),\n    elevation: Number(parts[1]),\n    rotation: Number(parts[2]),\n    code: parts[3],\n    speed: Number(parts[4]),\n    checksumHEX: checksum,\n    checksum: parseInt(checksum, 16),//convert hexadecimal string to integer\n}\nreturn msg;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":2300,"wires":[["eb79f09e.5a732"]]},{"id":"eb79f09e.5a732","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1255,"y":2380,"wires":[],"l":false},{"id":"9c292a3.c465bd8","type":"function","z":"4b3f21a3.ba434","name":"Parse $WIVWR","func":"\n//PART   : 0      1     2 3   4 5   6 7   8\n//SAMPLE : $WIVWR,134.2,L,1.4,N,0.7,M,2.6,K *4B\n\nvar parts = msg.payload.split(\",\");\nvar checksum = parts[8].split(\"*\")[1];\n\nmsg.payload = {\n    raw: msg.payload,\n    type: (parts[0] + \"\").substr(1),\n    //TODO\n    //TODO\n    //TODO\n    //TODO\n    checksumHEX: checksum,\n    checksum: parseInt(checksum, 16),//convert hexadecimal string to integer\n}\nreturn msg;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":2340,"wires":[["eb79f09e.5a732"]]},{"id":"ad7cf4a.dfe9608","type":"function","z":"4b3f21a3.ba434","name":"Parse $WIMWD","func":"\n//PART   : 0      1     2 3     4 5   6 7   8\n//SAMPLE : $WIMWD,230.5,T,229.7,M,1.4,N,0.7,M 52\n\nvar parts = msg.payload.split(\",\");\nvar checksum = parts[8].split(\"*\")[1];\n\nmsg.payload = {\n    raw: msg.payload,\n    type: (parts[0] + \"\").substr(1),\n    //TODO\n    //TODO\n    //TODO\n    //TODO\n    checksumHEX: checksum,\n    checksum: parseInt(checksum, 16),//convert hexadecimal string to integer\n}\nreturn msg;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":2380,"wires":[["eb79f09e.5a732"]]},{"id":"9ab6ee4.177691","type":"debug","z":"4b3f21a3.ba434","name":"Unknown NMEA","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1120,"y":2480,"wires":[]},{"id":"57ab1ec2.d11ee","type":"inject","z":"4b3f21a3.ba434","name":"$XXXXX","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$XXXXX,A,-2.8,D,PTCH,A,-0.6,D,ROLL51","payloadType":"str","x":740,"y":2540,"wires":[["ae7daa01.2afd78"]]}]
1 Like

Sorry,

That varies per string, sometimes a field is a carachter, sometimes a number.
But i handle them as string. If I need to modify a number, i'll convert it then.

Thanks a million :muscle:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.