Conversion of hexadecimal (16 bits signed) values to decimal

Good evening, once again I am facing a conversion problem. I receive this data in this format. They are the values of voltage, current (which can be negative) and temperature.

Debug result: {"timestamp":1663615971682,"ext":false,"canid":854,"dlc":6,"rtr":false,"data":[107,19,127,255,228,0]}

It gives about these values. (Order of idea)
image
49.7 Volt, -1.5 Amperes, 24°

Obviously I want to convert them into a readable format. I have tried to copy and paste examples found on the site without much success. I can do it for a positive value like the voltage, but as soon as it becomes negative (Current discharge), it doesn't work anymore.
To decode the frame, I use this documentation. It mentions coding in Little Endian.

image

Thanks for any help.

I'm using node-red-contrib-buffer-parser (node) - Node-RED to handle buffers of my MODBUS devices.

Thanks, I also use it, see the top of the flow, for another pattern with success (SOC & SOH).
But for this one I can't do it.
It should certainly do the job, but I don't know how to describe the fields requested in the node for this particular case.

Here is an example of how to use the buffer-parser ..
not sure but you may need to pass the msg.payload.data array from a Function node to convert it to an actual Buffer ?

[{"id":"f49b67038f22e562","type":"inject","z":"54efb553244c241f","name":"Buffer","props":[{"p":"payload.data","v":"[107,19,127,255,228,0]","vt":"bin"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":450,"y":1880,"wires":[["e0f41245d2ef1999"]]},{"id":"e0f41245d2ef1999","type":"buffer-parser","z":"54efb553244c241f","name":"","data":"payload.data","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16le","name":"volts","offset":0,"length":1,"offsetbit":0,"scale":"0.01","mask":""},{"type":"int16le","name":"current","offset":2,"length":1,"offsetbit":0,"scale":"0.01","mask":""},{"type":"int16le","name":"temperature","offset":4,"length":1,"offsetbit":0,"scale":"0.1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":650,"y":1920,"wires":[["06e59ccf97c82817"]]},{"id":"06e59ccf97c82817","type":"debug","z":"54efb553244c241f","name":"debug 28","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":1920,"wires":[]},{"id":"a0875f6b15288714","type":"inject","z":"54efb553244c241f","name":"Array ?","props":[{"p":"payload.data","v":"[107,19,127,255,228,0]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":290,"y":1960,"wires":[["5c2aaff5157402ac"]]},{"id":"5c2aaff5157402ac","type":"function","z":"54efb553244c241f","name":"to Buffer","func":"msg.payload.data = Buffer.from(msg.payload.data)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":1960,"wires":[["e0f41245d2ef1999"]]}]

Andy, I would just like to say "whooowww" it works with the help of the function node.
Many thanks for the more than quick help :rocket:

1 Like