Need help to interpret & calculate buffer

#1

Hi i think it´s another Newbee question from me, but i didn`t found an answer.

In my first Topic i neede help to send commands to my central heater, now he answers.

To get the Temperatur it is need to calculate with 2Bytes (red marked in debug) in my example:
(2x256+74)/10 = 58,6°C

I tryed it first with an inject (green; its a Buffer :1234567891) to get 1byte with .slice as msg.paylode this works.
but it wont works with bothe byte`s... and with the incomming buffer from the heater i get always NaN.

Can someone give me a tipp or an link, witch can help me

0 Likes

#2

Sure, let me try to shed some light.

In your function node you are using the statement `var byte8 = buffer.slice(7,8);`

It results that var `byte8` will hold a value of type buffer. The slice method will provide you a small buffer, just that. Later on you try to perform mathematical operations with buffer types. The end result is NaN.

You have to use a statement to read the values inside the buffer. The buffer class from node.js provides a method that does exactly what you want in a single statement.

`buffer.readUInt16BE(7)`

This statement will skip 7 bytes in your buffer and will return the 8th byte multiplied by 256 and then add the 9th byte.

Replace the code in your function for the code below for testing. There are lots of `node.warn` in the code to picture what is going on.

``````var buffer = msg.payload;
node.warn(buffer);
var byte8 = buffer.slice(7,8);
node.warn(byte8);
var byte9 = buffer.slice(8,9);
node.warn(byte9);
node.warn((byte8 * 256 + byte9)/10);
return msg;

``````
0 Likes

#3

Missing to explain this mystery, right ? How come that the function does the right calculation when the payload comes from the inject node. Normally I would ask you to share your code so we could investigate the payload. In such case it seems to me that you did like shown below. If this is the case then you are not injecting a buffer as you stated. This would be an array (object) and you will get into var byte8 the value 8 and var byte9 with value9. This will output indeed 2048.9

0 Likes

#4