Extract value from payload

Hello,

I'm receiving a payload with the next format:

I want to extract the value read on the channel A sensor (Bytes 3, 4 and 5) and convert into a decimal value as explains in the example (in my case the type os sensor is 0-10V).

This is an example of payload I'm receiving: "4200012d6d2400000000"

How can I do it?

Thanks!

Attach a debug node to show the value and copy/paste the result here. That will allow us to see exactly the format of the data.

1 Like

Right, that was worth asking because it is not a string but a buffer. The next issue is that the table you showed has 11 bytes, whereas yours only has 10, has the first byte has already been dropped off? If so then it is actually bytes 2, 3 4 that you want, Is that correct?

Try

The "X" under the Byte 10 in the Data frame specifications indicates that in this type of frame is not used. Then I want 3, 4 and 5 bytes.

You can see that the Byte 0 has the value "0x42" ("66" in decimal) as sepecifies the document and it's the first value of the buffer.

The second byte is 0x42 in the table also, whereas yours is not, so that didn't help. However you want bytes 3,4,5 from a buffer. You can use something like
var value = buffer.readUIntBE(3, 3);
or
var value = buffer.readUIntLE(3, 3);
dependent on whether your processor is little endian or big endian. You haven't said what h/w you are running on so I don't know. Try them both and see which gives the right answer.

The second is a status byte with a counter and has different value in every frame. I don't know why the table has the 0x42 value. You can see here:

Otherwise, I'm running node-red in a Multitech Conduit.

I put this code in a function node:

buffer = msg.payload;
var newMsg = buffer.readUIntBE(3, 3);
return newMsg;

And I'm receiving the next error in the debug console:

image

Sorry, because I'm a beginner programmer :stuck_out_tongue:

If you're on a Conduit, you are probably on quite an old version of Node.js - which will explain why the readUIntBE method isn't recognised. I don't believe you can easily upgrade either.

You also need to return a message object from the function, rather than a raw value.

You can do it with the code:

msg.payload = (msg.payload[3] << 16) + (msg.payload[4] << 8) + msg.payload[5];
return msg;

The Multitech Conduit runs an old version of node.js and can't easily be upgraded - so you may need to use older versions of any calls - eg - https://nodejs.org/docs/latest-v0.10.x/api/buffer.html
Maybe you mean readUInt32BE or one of the many variants...

Thanks!! that works fine! :smiley:

Thanks dceejay for your response!