2

I'm sending a water depth in mm to my NodeRed server where I'm currently attempting to reconstruct a 16-bit Int that has been split into 8 high bits and 8 low bits.

I've tried just storing the high bits in a var and shifting them left 8 spaces and then adding the low bits to the variable by OR'ing the two but I've had no success.

I saw somewhere someone suggested trying this

var _firstNumber = (((number8Bit2 & 0xff) << 8) | (number8Bit1 & 0xff));

Any suggestions friends?

Here is the node red function

msg1 = {};
msg2 = {};

var buf1 = msg.payload.slice(0,1);
var buf2 = msg.payload.slice(1, 2);

var lvl = (((buf2 & 0xff) << 8) | (buf1 & 0xff));

var buf3 = msg.payload.slice(2,3);
var batt = buf3.readUInt8();

batt = (batt + 127)/100;

msg1.payload = lvl;
msg1.topic = 'waterlevel';
msg2.payload = batt;
msg2.topic = 'battery';

return [msg1, msg2];
Ben Sefton
  • 938
  • 6
  • 10
  • I don't know how this would work. [JavaScript stores all numbers in IEEE-754 floating-point format](https://stackoverflow.com/questions/3605925/does-javascript-have-double-floating-point-number-precision), using 8 bytes. –  Mar 11 '19 at 23:32
  • @Amy - `&` and `<<` etc result in 32 bit "integers" (though they are still numbers of course) – Jaromanda X Mar 11 '19 at 23:35
  • Ahh that's unfortunate, I have the opportunity to decode it before arrival in TheThingsNetwork however the packet decoders are in Javascript also.. Thanks Amy! – Ben Sefton Mar 11 '19 at 23:36
  • Hm, I didn't know this. [operands of bitwise operators are converted to signed 32-bit integers.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) –  Mar 11 '19 at 23:36
  • `I saw somewhere someone suggested trying this ... Any suggestions friends?` I suggest you try the suggested code because it looks correct - I mean, **have** you even tried? – Jaromanda X Mar 11 '19 at 23:36
  • 2
    `but I've had no success` - what happened? was the result wrong? what was the input values? what was the result you got? what was the result you expected? Details of your failure may help us determine how you failed – Jaromanda X Mar 11 '19 at 23:39
  • Yeah of course I've tried but the result is 256 when it should be 300 so there's an overflow. I'll post my node red function if I can get the tags to work – Ben Sefton Mar 11 '19 at 23:40
  • yeah, code in comments is **unreadable** ... `256` is NOT an overflow ... what was the input bytes ... `number8Bit1 === 44` and `number8Bit2 === 1`? – Jaromanda X Mar 11 '19 at 23:42
  • 2
    `number8Bit2 *256 + number8Bit1` should work if input was converted correctly. – Garr Godfrey Mar 11 '19 at 23:44
  • The payload is FF 31 E4 however E4 is the battery level – Ben Sefton Mar 11 '19 at 23:44
  • how do you convert the payload to javascript variables? – Garr Godfrey Mar 11 '19 at 23:45
  • is msg.payload an `UInt8Array`? – Jaromanda X Mar 11 '19 at 23:45
  • by the way FF 31 would be 49 * 256 + 255 ... so, nothing like 256 or 300 – Jaromanda X Mar 11 '19 at 23:46
  • I've updated my initial post with the function from NodeRed, the low bits are buf1 and the high buf2 – Ben Sefton Mar 12 '19 at 00:15
  • it looks like `buf1` and `buf2` are both a unit-length [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), not numbers. – Patrick Roberts Mar 12 '19 at 00:17
  • The other important question is probably, is the value Little endian or Big endian? – hardillb Mar 12 '19 at 10:08
  • Edit the question to show the actual input and it's format. Namely how are you receiving the data (MQTT, serial) and is it arriving as a buffer? – hardillb Mar 12 '19 at 13:25

1 Answers1

0

Assuming you actually have a buffer, and not a string... It depends on big-endian/little-endian, but you can do something like the following:

const buffer = Buffer.from([0xFF, 0x31, 0xE4]);
const readInt16BE = buffer.readInt16BE(0);
const readInt16LE = buffer.readInt16LE(0);

msg.payload = {
    buffer: buffer,
    readInt16BE: readInt16BE,
    readInt16LE: readInt16LE
}

return msg;
Michael
  • 8,891
  • 3
  • 29
  • 42