# Read serial and extract reading

**URL:** <https://discourse.nodered.org/t/read-serial-and-extract-reading/19513>\
**Category:** General\
**Created:** [23 December 2019 16:53 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513 "2019-12-23T16:53:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bjoern77](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Bjoern77](https://discourse.nodered.org/u/Bjoern77)\
**Post date:** [23 December 2019 16:53 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513/1 "2019-12-23T16:53:24Z")

</div>

Hello  
Perhaps you can help me. I am new with Node-Red.  
I want to read from my serial (USB) on a Raspberry.  
The incoming data are coming from a oiltank level ultrasonic sensor.  
The sensor is sending every 60min data. The data are 22byte.  
I am only interested in 2 bytes. In my case i need byte 16 and byte 17.  
I found that i have to multiply the value of byte 16 and add the value from byte 17.  
The result will be my remaining liters beeing in the oiltank.  
Actual i found the Serial in node and configured it.  
My Debug gives me after a few hours some data:

Raw:  
[83,73,0,22,2,16,11,30,27,80,94,82,94,0,0,0,9,217,15,57,70,169,83,73,0,22,2,16,12,2,25,80,94,82,94,0,0,0,9,217,15,57,40,6,83,73,0,22,2,16,12,34,23,80,94,82,94,87,0,56,9,217,15,57,232,232,83,73,0,22,2,16,13,6,20,80,94,82,94,0,0,0,9,217,15,57,67,195,83,73,0,22,2,16,13,38,18,80,94,82,94,88,0,56,9,217,15,57,18,108,83,73,0,22,2,16,14,10]

I guess i need this part:  
83,73,0,22,2,16,11,30,27,80,94,82,94,0,0,0,9,217,15,57,70,169

Now the question: How can i split and convert the values and how can i calculate with them?  
The result must be 2521.  
The flow will be: Serial in --\> Function (With some code) ---\> Text (UI) with my liters?  
What will be in the function?  
Thanks a lot!  
Best,  
Bjoern

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [23 December 2019 17:38 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513/2 "2019-12-23T17:38:04Z")

</div>

Hi Bjoern, welcome to the Forum.

So, you want to extract byte 16, multiple it by 256 and add the result to byte 17.

A simple approach is to transform your buffer into an array and then do the calculation in a function node. The code in the function node will looks like below.

```auto
let arr = [];
arr.push(...msg.payload);

let result = 256 * arr[16] + arr[17];
msg.payload = result;
return msg;

```

Testing flow:

```auto
[{"id":"aec7271c.7e2cc8","type":"tab","label":"Buffer to Array","disabled":false,"info":""},{"id":"de279ecf.c999e","type":"inject","z":"aec7271c.7e2cc8","name":"Go","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":200,"wires":[["42ed8e64.d7325"]]},{"id":"42ed8e64.d7325","type":"function","z":"aec7271c.7e2cc8","name":"Dataset Buffer","func":"msg.payload = Buffer.from([83,73,0,22,2,16,11,30,27,80,94,82,94,0,0,0,9,217,15,57,70,169,83,73,0,22,2,16,12,2,25,80,94,82,94,0,0,0,9,217,15,57,40,6,83,73,0,22,2,16,12,34,23,80,94,82,94,87,0,56,9,217,15,57,232,232,83,73,0,22,2,16,13,6,20,80,94,82,94,0,0,0,9,217,15,57,67,195,83,73,0,22,2,16,13,38,18,80,94,82,94,88,0,56,9,217,15,57,18,108,83,73,0,22,2,16,14,10]);\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":200,"wires":[["c1ecba39.570e38"]]},{"id":"3524ee2a.fdd192","type":"debug","z":"aec7271c.7e2cc8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":730,"y":200,"wires":[]},{"id":"c1ecba39.570e38","type":"function","z":"aec7271c.7e2cc8","name":"Calculate result","func":"let arr = [];\narr.push(...msg.payload);\n\nlet result = 256 * arr[16] + arr[17];\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":200,"wires":[["3524ee2a.fdd192"]]}]

```

---

<div class="post-metadata">

**Author:** ![Bjoern77](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Bjoern77](https://discourse.nodered.org/u/Bjoern77)\
**Post date:** [23 December 2019 18:29 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513/3 "2019-12-23T18:29:05Z")

</div>

Hello Andrej  
Thank you very much for your answer.  
I am not sure what kind of data will come from my serial. Buffer of String.  
The bad thing is that my device only send every 60min. So long time to wait ☹  
What do you think about this in my function node:

```auto
var buf = Buffer.from(msg.payload);
var one = buf.readUInt8(16);
var two = buf.readUInt8(17);

msg.payload = one * 256 + two;

return msg;

```

One question to the serial buffer. Will it be empty after reading? Or do i have to clear it after reading?  
Thanks again.  
Best, Bjoern

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [23 December 2019 18:42 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513/4 "2019-12-23T18:42:25Z")

</div>

> [@Bjoern77](#):
>
> I am not sure what kind of data will come from my serial. Buffer of String.

You can chose between binary buffer or string when you configure the serial in node.

 ![r-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6ca1362c8ad4f69ffe39ed2dc3fabdf42ce3c56f.png)

> [@Bjoern77](#):
>
> What do you think about this in my function node:

Works perfectly. Nice usage of Node.js buffer. Thumbs up !

> [@Bjoern77](#):
>
> One question to the serial buffer. Will it be empty after reading? Or do i have to clear it after reading?

You chose the event that will make the serial node to pass the message to the next node in the flow.

 ![r-02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/596c343b1d50d568008e9543f3be39ef3c7e708a.png)

Once the serial in sends the data its buffer is cleared.

---

<div class="post-metadata">

**Author:** ![Bjoern77](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Bjoern77](https://discourse.nodered.org/u/Bjoern77)\
**Post date:** [24 December 2019 08:49 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513/5 "2019-12-24T08:49:00Z")

</div>

Merry Christmas  
Thanks for answer. Unlikely i get only errors over night.  
And the most not positive point is that i deleted them...  
So, my function is not working. I think that my sensor deliver the data in a different format then i mentioned  
before. Can it be possible that i get HEX from sensor?  
How can i convert? How i can find out what format i get?  
I added a debug node to serial. But the result was that what i wrote in first post.  
Perhaps the debug node has changed the kind of view?  
Sorry for stupid questions but i am at the very beginning.  
Thanks and best,  
Bjoern

---

<div class="post-metadata">

**Author:** ![Bjoern77](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Bjoern77](https://discourse.nodered.org/u/Bjoern77)\
**Post date:** [24 December 2019 09:35 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513/6 "2019-12-24T09:35:08Z")

</div>

Hello again  
Now i got again a debug node:

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[83]

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[73]

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[0]

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[22]

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[2]

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[16]

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[10]

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[24]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[52]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[80]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[94]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[82]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[94]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[0]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[0]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[0]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[9]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[215]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[15]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[57]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

[212]

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)

msg.payload : buffer[1]

[225]

**It looks like real decimal values. But when i click on the small arrow in front of each value i get this:**

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x53

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x49

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x0

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x16

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x2

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x10

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0xa

24.12.2019, 10:14:47[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x18

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x34

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x50

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x5e

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x52

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x5e

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x0

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x0

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x0

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x9

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0xd7

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0xf

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0x39

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)msg.payload : buffer[1]

buffer[1]

0: 0xd4

24.12.2019, 10:14:48[node: 55a73bc4.20ba9c](http://192.168.7.200:1880/?instance=0#)

msg.payload : buffer[1]

buffer[1]0: 0xe1

Is this HEX? Do i have to convert? How can i convert?  
Thanks and best regards,  
Bjoern

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 December 2019 10:07 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513/7 "2019-12-24T10:07:31Z")

</div>

As they say - they are buffers - and clicking them just shows them in different formats. so decimal 83 is the same as hex 0x53.  
They are the same value so nothing to convert...

---

<div class="post-metadata">

**Author:** ![Bjoern77](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Bjoern77](https://discourse.nodered.org/u/Bjoern77)\
**Post date:** [24 December 2019 11:39 UTC](https://discourse.nodered.org/t/read-serial-and-extract-reading/19513/8 "2019-12-24T11:39:58Z")

</div>

Hello  
It is a Christmas miracle: It is working now.  
The problem was the setting of the serial node.  
I changed in settings the value for "Split input" to "on the character". That is wrong.  
I need "after a timeout of 10000ms"  
Perhaps it is a little long but it is working 🙂  
Thanks to all and Merry Christmas.  
Bjoern
