I'm reading 32Bit float in form of 2x16 Bits. to build the value I'm trying to use Nodejs Buffer function as followed :
// Create new Buffer based on array bytes
var buf = Buffer.from([0x122f, 0x3A53]);
// Represent these bytes as 32-bit unsigned int
const value = buf.readUInt32BE();
// save the value
msg.payload = value;
return msg;
I'm running this on Node-red, and I get the following error:
RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: Attempt to write outside buffer bounds
any idea what I'm doing wrong here ? thanks in advance !
Update After @hardillb answer, the error has been solved, nevertheless, I still can get the float values ? here is what I get :
so the question is how to build float out those 2 uint16.
The problem is how you are defining the Buffer. Buffer.from()
is expecting an array of bytes, not 2 16bit numbers
You can do it by writing the 16bit numbers to a new buffer e.g.
var buf = Buffer.alloc(4);
buf.writeUInt16BE(0x122f);
buf.writeUInt16BE(0x3a53);
msg.payload = buf.readUInt32BE();
return msg;
To build it from an array of 16bit values already in msg.payload
var buf = Buffer.alloc(msg.payload.length * 2)
for (var i=0; i< msg.payload.length); i++) {
buf.writeUInt16BE(msg.payload[i], (i*2));
}
msg.payload = buf.readUInt32BE();
return msg;