# TCP parsing String

**URL:** <https://discourse.nodered.org/t/tcp-parsing-string/38696>\
**Category:** General\
**Created:** [7 January 2021 06:20 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696 "2021-01-07T06:20:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![valbuz](https://avatars.discourse-cdn.com/v4/letter/v/c89c15/32.png) [@valbuz](https://discourse.nodered.org/u/valbuz)\
**Post date:** [7 January 2021 06:20 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/1 "2021-01-07T06:20:47Z")

</div>

Hi

I’m trying parsing a string from a TCP Output Node. The string has information of some GPIO’s and I want it for switching some Relais.

It’s a readable ASCII-String.

Question: is it needed convert it to a buffer? or is it possible to work as is?

I’m trying with a Function Node (substring/startsWith).

Thanks for any help or links.

![GPIO](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47e38fbebc59a9ee084587e482b31ecdcb5dea9f.png)

---

<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:** [7 January 2021 08:07 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/2 "2021-01-07T08:07:06Z")

</div>

Probably better suited to a regular expression that captures the PG number and status (or split on newline then split each line on space) then send each as a separate message with topic set OR build an overall object with all PGs and their state.

example of generating a message with topic and payload as value for each "PG X STATE"...

```auto
let lines = msg.payload.split("\n");
for(let i = 0; i < lines.length; i++) {
  let line = lines[i].trim();
  let parts = line.split(" ");
  if(parts.length != 2) continue;
  node.send({ topic: "mydevice/pg/" + parts[1] + "/state" , payload: parts[2]});
}

```

You could feed this function to a `join` node if you want a single key/val object (just send a msg with msg.complete set true after the for loop).

EDIT...

Forgot to say: If you have any control of the send format - I strongly recommend you send a JSON string (all this looping and splitting would be completely unecessary)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [7 January 2021 08:54 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/3 "2021-01-07T08:54:39Z")

</div>

> [@Steve-Mcl](#):
>
> `i>lines.length`

small typo .. shouldnt that be `i < lines.length` 😉  
nice script btw without even having the original payload

---

<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:** [7 January 2021 08:57 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/4 "2021-01-07T08:57:45Z")

</div>

Thanks for the heads up (I should have added it was 100% untested since the OP provided no payload to play with 🙂 )

I'll correct the post for future readers.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 January 2021 11:13 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/5 "2021-01-07T11:13:52Z")

</div>

You could also use the csv node to parse the data.

---

<div class="post-metadata">

**Author:** ![valbuz](https://avatars.discourse-cdn.com/v4/letter/v/c89c15/32.png) [@valbuz](https://discourse.nodered.org/u/valbuz)\
**Post date:** [7 January 2021 13:33 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/6 "2021-01-07T13:33:24Z")

</div>

hi together

Thanks!  
i will try...and let you know.

the payload in the att.  
[PG.txt](https://discourse.nodered.org/uploads/short-url/3xwQ0rOCv3lDi7T6tZ2lMv1T02K.txt) (288 Bytes)

---

<div class="post-metadata">

**Author:** ![valbuz](https://avatars.discourse-cdn.com/v4/letter/v/c89c15/32.png) [@valbuz](https://discourse.nodered.org/u/valbuz)\
**Post date:** [8 January 2021 09:57 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/7 "2021-01-08T09:57:07Z")

</div>

> [@Steve-Mcl](#):
>
> Forgot to say: If you have any control of the send format - I strongly recommend you send a JSON string (all this looping and splitting would be completely unecessary)

Hi  
No there is no way to change the string ☹

So i try it, but on:

```auto
if(parts.length != 2) continue;

```

the sript is stopping

---

<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:** [8 January 2021 10:55 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/8 "2021-01-08T10:55:42Z")

</div>

In your screenshot you posted...

> [@valbuz](#):
>
> ![GPIO](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47e38fbebc59a9ee084587e482b31ecdcb5dea9f.png)

... there are `newlines` ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5f4f3ff35c2c5225f4498e3fcbbe01cd2f92cc20.png) before each PG

However in your text file, there are not.

the script i wrote depends on there being new lines.

Here is a version that should work with either eventuality...

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

Demo flow...

```auto
[{"id":"46f4153f.dd33ec","type":"function","z":"ac69cd26.4f506","name":"V1: Individual Messages","func":"let lines = msg.payload.split(\"PG \");\nfor(let i = 0; i < lines.length; i++) {\n let line = lines[i].trim();\n let parts = line.split(\" \");\n if(parts.length != 2) continue;\n node.send({ topic: \"mydevice/pg/\" + parts[0] + \"/state\" , payload: parts[1]});\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":950,"y":140,"wires":[["cd39dc70.32562"]]},{"id":"bc7a8cf1.d2549","type":"inject","z":"ac69cd26.4f506","name":"PGSTATE:PG 1 ONPG 2 OFFPG 3 OFFPG 4 OFFPG ...","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"PGSTATE:PG 1 ONPG 2 OFFPG 3 OFFPG 4 OFFPG 5 OFFPG 6 OFFPG 7 OFFPG 8 OFFPG 9 OFFPG 10 OFFPG 11 OFFPG 12 OFFPG 13 OFFPG 14 OFFPG 15 OFFPG 16 OFFPG 17 OFFPG 18 OFFPG 19 OFFPG 20 OFFPG 21 OFFPG 22 OFFPG 23 OFFPG 24 OFFPG 25 OFFPG 26 OFFPG 27 OFFPG 28 OFFPG 29 OFFPG 30 OFFPG 31 OFFPG 32 OFFOK","payloadType":"str","x":1030,"y":80,"wires":[["46f4153f.dd33ec","1982e937.14cb07"]]},{"id":"cd39dc70.32562","type":"debug","z":"ac69cd26.4f506","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":140,"wires":[]},{"id":"1982e937.14cb07","type":"function","z":"ac69cd26.4f506","name":"V2 Single Object","func":"let lines = msg.payload.split(\"PG \");\nfor(let i = 0; i < lines.length; i++) {\n let line = lines[i].trim();\n let parts = line.split(\" \");\n if(parts.length != 2) continue;\n node.send({ topic: \"pg\" + parts[0], payload: parts[1]});\n}\nnode.send({complete:true})","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":200,"wires":[["d7faceb1.18a12"]]},{"id":"d7faceb1.18a12","type":"join","z":"ac69cd26.4f506","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1100,"y":200,"wires":[["2123ca5d.755726"]]},{"id":"2123ca5d.755726","type":"debug","z":"ac69cd26.4f506","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![valbuz](https://avatars.discourse-cdn.com/v4/letter/v/c89c15/32.png) [@valbuz](https://discourse.nodered.org/u/valbuz)\
**Post date:** [8 January 2021 11:39 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/9 "2021-01-08T11:39:26Z")

</div>

Sorry! It's all new for me.... 🙃

With your flow, it works.  
so i try on with the other strings....(i get some different strings from the device), but with your flow i hope i can make it work 🙂  
i will give you a answer.

thanks!  
pat

---

<div class="post-metadata">

**Author:** ![valbuz](https://avatars.discourse-cdn.com/v4/letter/v/c89c15/32.png) [@valbuz](https://discourse.nodered.org/u/valbuz)\
**Post date:** [12 January 2021 13:36 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/10 "2021-01-12T13:36:04Z")

</div>

Sorry, but i'm still searching... 🙄 🙄

There are 4 dif. String's beginning with the following ASCII:

OK /n  
JA-121T.... OK/n  
PGSTATE... OK/n  
PRFSTATE.... OK/n

But sometimes the payload is a combination of two result of 2 payloads. See the att. Image  
So it's to tricky for me to parse it.  
Is there a simple way to resolve this?  
Thank you

pat

 ![string](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe1023df0b09c6070c350ac51c7bd278fa95c2b6.png)

---

<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:** [12 January 2021 13:55 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/11 "2021-01-12T13:55:06Z")

</div>

It looks like you are not setting TCP node to split on the right part

- Is the data contineous? Do you have a specification? Is there a dedicated END Of DATA marker? e.g. is OK meant to me the end of the data stream?

- What settings do you use on the TCP node.

- What pieces of data are you interested in?

---

<div class="post-metadata">

**Author:** ![valbuz](https://avatars.discourse-cdn.com/v4/letter/v/c89c15/32.png) [@valbuz](https://discourse.nodered.org/u/valbuz)\
**Post date:** [13 January 2021 08:53 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/12 "2021-01-13T08:53:34Z")

</div>

> [@Steve-Mcl](#):
>
> - Is the data contineous? Do you have a specification? Is there a dedicated END Of DATA marker? e.g. is OK meant to me the end of the data stream?[/quote]
> 
> Only the OK(Heartbeat) is sending every 10sec. The other information only when i send the request for it.  
> Yes it seem's the OK is the end of every stream.
> 
> - What settings do you use on the TCP node.
> 
> see att.
> 
> - What pieces of data are you interested in?

- Heartbeat
- State of the Output's (PG's, see the early Post's)
- PRFSTATE (it's a binary string)

thanks  
pat  
 ![tcp](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3d3e1e05721b437358fcd405443e60dcc8a85f22.png)

---

<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:** [14 March 2021 08:53 UTC](https://discourse.nodered.org/t/tcp-parsing-string/38696/13 "2021-03-14T08:53:35Z")

</div>

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