# Convert serial messages into object

**URL:** <https://discourse.nodered.org/t/convert-serial-messages-into-object/70223>\
**Category:** General\
**Created:** [7 November 2022 15:03 UTC](https://discourse.nodered.org/t/convert-serial-messages-into-object/70223 "2022-11-07T15:03:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zinocks](https://avatars.discourse-cdn.com/v4/letter/z/dec6dc/32.png) [@zinocks](https://discourse.nodered.org/u/zinocks)\
**Post date:** [7 November 2022 15:03 UTC](https://discourse.nodered.org/t/convert-serial-messages-into-object/70223/1 "2022-11-07T15:03:51Z")

</div>

Hi,

I have an MPPT Solar controller that sends me on serial port, every second, this three separated messages:

CS: 5  
VPV: 12  
HDS: 11

Node red treats them as separated messages, because they force me to split them by "\n" How could I join them and get an object like {"CS": 5, "VPV": 12, "HDS": 11} ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 November 2022 15:23 UTC](https://discourse.nodered.org/t/convert-serial-messages-into-object/70223/2 "2022-11-07T15:23:09Z")

</div>

> [@zinocks](#):
>
> this three separated messages:

> [@zinocks](#):
>
> they force me to split them by "\n"

That doesn't make sense. First you say the device sends you three messages, then you say you had to split it, which suggests they come in one message.

---

<div class="post-metadata">

**Author:** ![zinocks](https://avatars.discourse-cdn.com/v4/letter/z/dec6dc/32.png) [@zinocks](https://discourse.nodered.org/u/zinocks)\
**Post date:** [7 November 2022 15:27 UTC](https://discourse.nodered.org/t/convert-serial-messages-into-object/70223/3 "2022-11-07T15:27:48Z")

</div>

Sorry if not explained well. The MPPT Solar controller sends the message not splitted but

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4fb8f2e30107924f3fab540306e45b954f8500b.png)

This node forces me to split by "\n". If I remove "\n", it splits every char... I don't want to split the message.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 November 2022 16:38 UTC](https://discourse.nodered.org/t/convert-serial-messages-into-object/70223/4 "2022-11-07T16:38:59Z")

</div>

If you want to join them back up again then I suggest not splitting in the serial node. Can you use a timeout of, maybe 500ms to give you the messages? If so then in a function node you can use the `split("\n")` to give you an array of three elements, then split each one of those using `:` and then you can build the object from those.

If you want to go down that route but the javascript is too complex then get the serial node going first to give you the complete message. Then feed that to a debug node and you can use the little Copy button that pops up when you hover over it to capture one of the strings exactly. Paste that here and it can be used in an Inject node to simulate your data and can be used to test the code.

---

<div class="post-metadata">

**Author:** ![zinocks](https://avatars.discourse-cdn.com/v4/letter/z/dec6dc/32.png) [@zinocks](https://discourse.nodered.org/u/zinocks)\
**Post date:** [7 November 2022 21:01 UTC](https://discourse.nodered.org/t/convert-serial-messages-into-object/70223/5 "2022-11-07T21:01:05Z")

</div>

I did something similar, I found a character that "more or less" limits the entire message and with some regex I parsed it to a json object.

Thank you for your inspiration

---

<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:** [21 November 2022 21:01 UTC](https://discourse.nodered.org/t/convert-serial-messages-into-object/70223/6 "2022-11-21T21:01:58Z")

</div>

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