Serial Ascii data

Hi, im trying to read serial data from a RS232 device and need help to parse it to respective variables for the GUI.

Data is on a format
$ISAGM,a.aaa,a.aaa,a.aaa,g.ggg,g.ggg,g.ggg,m.mmm,m.mmm,m.mmm*xx

Any suggestions of how i can achieve this?

Thanks

If it is always this exact format you could use substr functions or regex in a function node.

If it is always this format but values vary in length, then split by comma in a function node and assign the values from the resulting array to an object.

Here is an example...

image

image

Demo flow...

[{"id":"46fd36cc.0d35e8","type":"inject","z":"59c1e9ca.dd6988","name":"$ISAGM,1.234,2.345,3.456,9.876,8.765,7.654,5.555,6.666,7.777*10","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ISAGM,1.234,2.345,3.456,9.876,8.765,7.654,5.555,6.666,7.777*10","payloadType":"str","x":1110,"y":260,"wires":[["86e56eee.fb24f"]]},{"id":"86e56eee.fb24f","type":"function","z":"59c1e9ca.dd6988","name":"Serial Data to Object","func":"var elements = msg.payload.split(\",\");\nvar el9 = elements[9].split(\"*\");\nmsg.payload = {\n    aLat: parseFloat(elements[1]),\n    aLon: parseFloat(elements[2]),\n    aAlt: parseFloat(elements[3]),\n    gPh1A: parseFloat(elements[4]),\n    gPh2A: parseFloat(elements[5]),\n    gPh3A: parseFloat(elements[6]),\n    mA: parseFloat(elements[7]),\n    mB: parseFloat(elements[8]),\n    mC: parseFloat(el9[0]),\n    multiplier: parseInt(el9[1]),\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1070,"y":320,"wires":[["c8d55540.9ff008"]]},{"id":"c8d55540.9ff008","type":"debug","z":"59c1e9ca.dd6988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1270,"y":320,"wires":[]}]

Thank you, Steve.

I think this is getting closer. I got this debug msg when i connected the RS232 node as input.

4/28/2021, 10:05:55 PMnode: Serial Data to Objectfunction : (error)

"TypeError: Cannot read property 'split' of undefined"

Seems to work fine with your insert node with repetitions of 1 sec.

Any ideas?

Regards
Jan

try changing the code in the function node to this...

node.warn(["payload", msg.payload]);
var elements = msg.payload.split(",");
node.warn(["elements", elements]);
var el9 = elements[9].split("*");
node.warn(["el9", el9]);
msg.payload = {
    aLat: parseFloat(elements[1]),
    aLon: parseFloat(elements[2]),
    aAlt: parseFloat(elements[3]),
    gPh1A: parseFloat(elements[4]),
    gPh2A: parseFloat(elements[5]),
    gPh3A: parseFloat(elements[6]),
    mA: parseFloat(elements[7]),
    mB: parseFloat(elements[8]),
    mC: parseFloat(el9[0]),
    multiplier: parseInt(el9[1]),
}
return msg;

then expand all the orange warning messages in the debug sidebar & show me

Steve, I get this

4/28/2021, 10:22:49 PMnode: Serial Data to Objectfunction : (warn)

array[2]

0: "payload"

1: "1.00479,"

4/28/2021, 10:22:49 PMnode: Serial Data to Objectfunction : (warn)

array[2]

0: "elements"

1: array[2]

0: "1.00479"

1: ""

4/28/2021, 10:22:49 PMnode: Serial Data to Objectfunction : (error)

"TypeError: Cannot read property 'split' of undefined"

4/28/2021, 10:22:49 PMnode: Serial Data to Objectfunction : (warn)

array[2]

0: "payload"

1: "-0.14108,"

4/28/2021, 10:22:49 PMnode: Serial Data to Objectfunction : (warn)

array[2]

0: "elements"

1: array[2]

0: "-0.14108"

1: ""

So the payload coming from your serial node is ...

  • actually "1.00479,"
  • and NOT "$ISAGM,a.aaa,a.aaa,a.aaa,g.ggg,g.ggg,g.ggg,m.mmm,m.mmm,m.mmm*xx"

... as you said in your original post - this is the reason for the error.


When i provided that solution I did ask if the format was always the same.

is there any documentation for the serial device? I am interested in whether is has an end code you can split on (set the end code in the serial config)

Also, copy + paste a screenshot of your serial node config into a reply.

Link to the manual: Page 19

The RS232 node is set to 9600 8,N,1

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