How to Decode 16 Bit signed Integer

not really sure where your going with this???

So, in an effort to put you on the right track I will try to explain what I think you are attempting to do.

If i'm not mistaken, the data you see in the payload is a NodeJS Buffer.

Looking at the decoded image you attached in your first message, the data is formatted like a protocol (I don't know what protocol) however, the picture gives some clues to the end part of the message.

Based on what I can see, you should really decode the whole msp.payload reply (since the parts of data before the "measured value" are probably important)

That said, the below should get you started....

testcode...

//fake the msg.payload (simulate serial reply)
var msg = {};
msg.payload = new Buffer.from([0x40, 0x1, 0x2, 0x4, 0x4, 0x1, 0xa, 0xa6, 0x18, 0x7e, 0x1, 0x0, 0x0, 0x0, 0x29, 0x17, 0x0c]);

//decode the status record
var statusRecord = {};
statusRecord.attribute = msg.payload.readInt16LE(11);
statusRecord.success = msg.payload.readInt8(13);
statusRecord.dataType = msg.payload.readInt8(14);
statusRecord.measuredValue = msg.payload.readInt16LE(15);

//todo: Check the success flag is 0
//todo: Check dataType before calling the next buffer.readXXXX
//      e.g if dataType is byte then call readInt8() to get measuredValue

if(statusRecord.success === 0){
    console.log(`Read was successful :)`);
    console.log(statusRecord);
    console.log(`Measured Value: ${statusRecord.measuredValue / 100.0} [*C]`);
} else {
    console.log(`Read was unsuccessful! :(`);
}

output...

Read was successful :)
{ attribute: 0, success: 0, dataType: 41, measuredValue: 3095 }
Measured Value: 30.95 [*C]

You can play with it here >> Compile and Execute C++0x Onlinej

Hope that helps

1 Like