# Send and response with serial

**URL:** <https://discourse.nodered.org/t/send-and-response-with-serial/39003>\
**Category:** General\
**Created:** [12 January 2021 14:09 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003 "2021-01-12T14:09:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sturgeon](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sturgeon](https://discourse.nodered.org/u/Sturgeon)\
**Post date:** [12 January 2021 14:09 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003/1 "2021-01-12T14:09:06Z")

</div>

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?

---

<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 14:25 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003/2 "2021-01-12T14:25:10Z")

</div>

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

Send `SOH SEQ STX DATA ETX EOT`  
Reponse `SOH SEQ ACK EOT`

or perhaps...

Send `STX SEQ ENQ DATA ETX`  
Reponse `STX SEQ ACK ETX`

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

where...

```nohighlight

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)

---

<div class="post-metadata">

**Author:** ![Sturgeon](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sturgeon](https://discourse.nodered.org/u/Sturgeon)\
**Post date:** [14 January 2021 10:46 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003/3 "2021-01-14T10:46:34Z")

</div>

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

 ![Chema](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5d8bae21a40c5a9a886feb7bba1952200d6b6e52.jpeg)

---

<div class="post-metadata">

**Author:** ![Sturgeon](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sturgeon](https://discourse.nodered.org/u/Sturgeon)\
**Post date:** [14 January 2021 21:29 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003/4 "2021-01-14T21:29:05Z")

</div>

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?

---

<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:** [15 January 2021 07:25 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003/5 "2021-01-15T07:25:51Z")

</div>

> [@Sturgeon](#):
>
> I want to check that the third byte is equal to the sum of the first and second bytes

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

---

<div class="post-metadata">

**Author:** ![Sturgeon](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sturgeon](https://discourse.nodered.org/u/Sturgeon)\
**Post date:** [15 January 2021 08:50 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003/6 "2021-01-15T08:50:32Z")

</div>

> [@Colin](#):
>
> Assuming it is in the payload then

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

---

<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:** [15 January 2021 09:00 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003/7 "2021-01-15T09:00:47Z")

</div>

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

```auto
[{"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":"payload.data[2]","propertyType":"msg","rules":[{"t":"eq","v":"payload.data[0] + payload.data[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":[]}]

```

---

<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:** [16 March 2021 09:00 UTC](https://discourse.nodered.org/t/send-and-response-with-serial/39003/8 "2021-03-16T09:00:56Z")

</div>

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