TCP parsing String

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

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"...

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)

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

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

I'll correct the post for future readers.

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

hi together

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

the payload in the att.
PG.txt (288 Bytes)

Hi
No there is no way to change the string :frowning:

So i try it, but on:

if(parts.length != 2) continue;

the sript is stopping

In your screenshot you posted...

... there are newlines image 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...

Demo flow...

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

Sorry! It's all new for me.... :upside_down_face:

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 :slightly_smiling_face:
i will give you a answer.

thanks!
pat

Sorry, but i'm still searching... :roll_eyes: :roll_eyes:

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

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?

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

thanks
pat
tcp

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