# Serial Ascii data

**URL:** <https://discourse.nodered.org/t/serial-ascii-data/44832>\
**Category:** General\
**Created:** [28 April 2021 06:11 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832 "2021-04-28T06:11:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JanUlr](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@JanUlr](https://discourse.nodered.org/u/JanUlr)\
**Post date:** [28 April 2021 06:11 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/1 "2021-04-28T06:11:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 April 2021 06:34 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/2 "2021-04-28T06:34:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 April 2021 07:04 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/3 "2021-04-28T07:04:20Z")

</div>

Here is an example...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35afd007887886e293bb60910687f356ce70601d.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27741724491f336d986bbcca558c660a6c999b48.png)

Demo flow...

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![JanUlr](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@JanUlr](https://discourse.nodered.org/u/JanUlr)\
**Post date:** [28 April 2021 20:07 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/4 "2021-04-28T20:07:38Z")

</div>

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 PM[node: Serial Data to Object](http://127.0.0.1:1880/#)function : (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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 April 2021 20:12 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/5 "2021-04-28T20:12:49Z")

</div>

> [@JanUlr](#):
>
> Any ideas?

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

```auto
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

---

<div class="post-metadata">

**Author:** ![JanUlr](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@JanUlr](https://discourse.nodered.org/u/JanUlr)\
**Post date:** [28 April 2021 20:29 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/6 "2021-04-28T20:29:17Z")

</div>

Steve, I get this

4/28/2021, 10:22:49 PM[node: Serial Data to Object](http://127.0.0.1:1880/#)function : (warn)

array[2]

0: "payload"

1: "1.00479,"

4/28/2021, 10:22:49 PM[node: Serial Data to Object](http://127.0.0.1:1880/#)function : (warn)

array[2]

0: "elements"

1: array[2]

0: "1.00479"

1: ""

4/28/2021, 10:22:49 PM[node: Serial Data to Object](http://127.0.0.1:1880/#)function : (error)

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

4/28/2021, 10:22:49 PM[node: Serial Data to Object](http://127.0.0.1:1880/#)function : (warn)

array[2]

0: "payload"

1: "-0.14108,"

4/28/2021, 10:22:49 PM[node: Serial Data to Object](http://127.0.0.1:1880/#)function : (warn)

array[2]

0: "elements"

1: array[2]

0: "-0.14108"

1: ""

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 April 2021 20:33 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/7 "2021-04-28T20:33:14Z")

</div>

> [@JanUlr](#):
>
> array[2]
> 
> 0: "payload"
> 
> 1: "1.00479,"

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.

---

<div class="post-metadata">

**Author:** ![JanUlr](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@JanUlr](https://discourse.nodered.org/u/JanUlr)\
**Post date:** [28 April 2021 20:38 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/8 "2021-04-28T20:38:34Z")

</div>

Link to the manual: Page 19

> **[ISM3D-Manual-Rev-1.4.pdf](https://www.impactsubsea.co.uk/wp-content/uploads/2020/04/ISM3D-Manual-Rev-1.4.pdf)**
>
> 1289.31 KB

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 June 2021 20:39 UTC](https://discourse.nodered.org/t/serial-ascii-data/44832/9 "2021-06-27T20:39:04Z")

</div>

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