How to form a new number with two bytes?

Hello everyone. I am facing the following problem:

In MQTT, i'm suscribe to a topic and i'm constantly receiving a frame consisting in three bytes like so: (SoF) ( byte1) (byte2).
Where (SoF) represents the start of the frame ( it's just a char) and (byte1),(byte2) the most significant and less significant bits of a uint16.

My question is, how can I "sense" when a frame is comming and then concatenate the byte1 and the byte2 in a single unsigned int variable?

In C it's quite easy to do it, but since this is my first time programming in .js I have no clue of where to start.

Any help is more than wellcome.

Try feeding your payload to a function node then shift byte1 right by 8 then add together. Much as you would in C.

Not sure what you mean by that bit. Each time there is a new value for the subscribed topic in mqtt the MQTT In node will pass on a message, which you can feed into a function node to do your manipulation.
If you need further help then feed the output of the mqtt node into a debug node and paste what you get here so we can see exactly what the format of the data is.

Thanks for the reply.
That's exactly what i want to do, but don't know how to do it in javascript.

Can you possible make an example about it?
I imagine that it has to be something like:

var number |= (byte1 << 8);
number |= (byte2);

Thanks again

That Is what i already achieved

That is what i already achieved. Know what i need to do Is to program the funtion that "understand" that when a (SoF) is coming the next two bytes that comes are the bits of a 16 bits variable.

I don't have a clue of how to shift those bits into a new variable in .js ( and particulary with the payload).

Any code as example about it is more than welcome

Again you have not made it clear what you mean by "understand that when a (SoF) is coming the next two bytes that comes are the bits of a 16 bits variable". Do you mean that you get other types of messages coming in and you need to differentiate them? If so then you can use a Switch node for that. To access the payload in a function node you use msg.payload. Have a look at the nodered docs there are is a lot of useful information there about accessing data in messages and so on.
As for how to shift data in javascript an internet search for 'javascript bit shift' will take you to sites such as https://www.w3schools.com/js/js_bitwise.asp

Operators are well documented...

So when I said "shift byte1 by 8 then add together" I meant...

var i = (byte1 << 8) + byte2
Alternatively...
var i = (byte1 * 256) + byte2

Of course it depends on endianness which way around you put the bytes.

Then you could use a nodejs buffer...
https://nodejs.org/api/buffer.html#buffer_buf_readuint16le_offset

There are many ways.

as it's already coming from MQTT node it may already be a binary buffer... which would make that a good way to do it.

I don't know if this example may help;

Thanks everyone of you! The information came more than handly!

1 Like