Send and response with serial

Good day!
I want to do communication node-red (raspbery 3+) with arudino using rs485 protocol and have some questions.
I can send and receive messages through the serial port node. It is very easy with node-red.
But I want to slightly modify my "protocol" and want to make response of receipt and sending of data. But I dont understand how.
For example, I send data to several arduinos and set the send flag to 1 (Flow context) using the Set node. Then I receive a response from the arduino like "1OK", "2OK"... etc., where the first char is the ID of the arduino, "OK" - response of data receipt. I need:

  1. Check if 2 and 3 char to equal "OK";
  2. If it equal, then check what the first character is;
  3. Set the corresponding ID flag equal to 1.

How to do it? I think about function, but it little hard for me.
Can I do this without using functions? With standard nodes?

If you have control over the format of data then i would recommend sending JSON formatted data (then its very simple to convert the strings into usable JS objects).

Also, I would decide on a terminator constant (like 0x3 ETX) so that you can safely and reliably split serial strings.

As for confirmation, many protocols employ a standard sequence like


or perhaps...


it is up to you how you determine your protocol - you could even look up and utelise a basic ASCII Protocol - like modbus


SOH  = Start of Header
SEQ  = a message ID / Sequence number (so that you can confirm responses against that number)
STX  = Start of Text/data
DATA = standard JSON string with your DATA in
ETX  = End of Text/data
EOT  = End of transmission
ACK  = GOOD Acknoledgement

See the ASCII table for value of SOH, STX, ETX, ACK etc

In otherwords, if you send SEQ 77 with some DATA, you MUST get a reply of 77 ACK

If you receive 78 - you are out of sequnece,
If you receive 77 NAK - the data was recieved but the other device "didnt like it" so replied with a "negative ACK"

If you are not familiar with JSON - dont worry, it is quite easy to produce and very useful in this (javascript) environment as it is a native data transfer format. (just ask if you need further direction)

I will try to explain with a picture what I want to do.

Ok, step-by-step.
I reciver buffer data from serial. For example [2,1,3]. I want to check that the third byte is equal to the sum of the first and second bytes. How can I do it?

Assuming it is in the payload then
if (msg.payload[3] ==msg.payload[1]+msg.payload[2])

It is code for function node?
Can i do it with standart node? Switch, Set etc...

You can do it in a switch or change node using JSONata

[{"id":"e80c63e9.b052e8","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[2,1,3]","payloadType":"bin","x":130,"y":3860,"wires":[["6b997dfc.dbcb6c","2756679c.c38ed8"]]},{"id":"6b997dfc.dbcb6c","type":"switch","z":"8d22ae29.7df6d","name":"","property":"[2]","propertyType":"msg","rules":[{"t":"eq","v":"[0] +[1]","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":290,"y":3920,"wires":[["2756679c.c38ed8"]]},{"id":"2756679c.c38ed8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"$keys(payload)","statusType":"auto","x":500,"y":3920,"wires":[]}]

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