Read serial and extract reading

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

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.

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

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

Testing flow:

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

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 :frowning:
What do you think about this in my function node:

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

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

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

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

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

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

Hello again
Now i got again a debug node:

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 83 ]

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 73 ]

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 0 ]

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 22 ]

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 2 ]

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 16 ]

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 10 ]

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 24 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 52 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 80 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 94 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 82 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 94 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 0 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 0 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 0 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 9 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 215 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 15 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 57 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

[ 212 ]

24.12.2019, 10:14:48node: 55a73bc4.20ba9c

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:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x53

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x49

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x0

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x16

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x2

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x10

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0xa

24.12.2019, 10:14:47node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x18

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x34

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x50

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x5e

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x52

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x5e

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x0

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x0

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x0

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x9

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0xd7

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0xf

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0x39

24.12.2019, 10:14:48node: 55a73bc4.20ba9cmsg.payload : buffer[1]

buffer[1]

0: 0xd4

24.12.2019, 10:14:48node: 55a73bc4.20ba9c

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

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

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 :slight_smile:
Thanks to all and Merry Christmas.
Bjoern

2 Likes