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.
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.
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).
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
I'm parsing a 277 byte stream -
with 2 byte header - 256 byte data 1 byte separator 16 of data and then 2 byte checksum.
Use a function node - and in the code section for that - define a variable - for your beginning or record indicator (BORI) -find it by index of(variable) then, create outputs for BORI+1, BORI +2 etc.
hope that makes sense -here's a chunk of that code-
// Extract header
let headerStart = msg.payload.indexOf(Buffer.from([0x55, 0xaa]));
// If header found
if(headerStart !== -1) {
let headerEnd = headerStart + 2;
// Extract rest of the data according to structure
let header = msg.payload.slice(headerStart, headerEnd);
let data1 = msg.payload.slice(headerEnd, headerEnd + 256);
let spacer = msg.payload.slice(headerEnd + 256, headerEnd + 257);
let data2 = msg.payload.slice(headerEnd + 257, headerEnd + 257 + 16);
let checksum = msg.payload.slice(headerEnd + 257 + 16, headerEnd + 257 + 16 + 2);
let rpm = msg.payload.slice(headerEnd +8, headerEnd + 8+1);
let mph = msg.payload.slice(headerEnd+6, headerEnd+6+1)
let map = msg.payload.slice(headerEnd+7, headerEnd+7+1)
// Output the data
return [{
topic: "header",
payload: header
}, {
topic: "data1",
payload: data1
}, {
topic: "spacer",
payload: spacer