Read and write value from string

#1

Hello I am trying to use the function node to modify a message from the TCP in node. I am trying to interface with a controller propitary interface. I have yet only managed to send a single value to the controller. Thats done by replying "?100=16" to the message "?100" where 16 is the value.

20:33:36.265: Tx: ?100
20:33:36.312: Rx: ?100=16

When trying to send and recieve more than one value I don't have the skills to read the message in the function node and pass the correct reply. For instance I want to assign 100 in the "!14,7,100" to a variable and reply ?14=100 to confirm to the controller that the value is recieved, wrapped up like the Rx line below:

20:29:53.295: Tx: !12,1,0,0!14,7,100?14?16
20:29:53.327: Rx: !12,1,0,0!14,7,100?14=100?16=23

The TCP in node is configured as stream of string and each "command group" in the string is separated by ! or ?.

Anyone who can push me in the right direction?

0 Likes

#2

To try and get to the crux of what you are trying to do, is it right that you have a value in a variable (which might be 100) and you want to build the string "!14,7," followed by the value of that variable? If so then you can just do
answer = "!14,7," + theVariable
The fact that the first expression on the right hand side is a string will tell javascript to convert theVariable to a string and concatenate it with the first part.

0 Likes

#3

Thank you that worked for building the string. Now how do I read out "100" from !14,7,100 and stores it as a variable?

0 Likes

#4

Assuming that the commas are predictable then I would use the javascript split() function to split it at commas and then pick up the third element.

0 Likes

#5

Thanks I ended up using the slice function to slice out the parts with ! and ? in front and then used a split node to split it further down and then used the msg.parts.index to read out the different values. This page described everything I had to do with javascript glad I found it.

0 Likes