IEEE-754 Floating Point Converter

You haven't got enough zeros on the mask for the sign bit (it should be 0x8000000000000000) but the more fundamental problem is that the max integer value in js is 2^53-1 so you can't turn it into a 64 bit integer and then extract the sign and exponent. You will have to extract the sign and exponent from the first word and then build the mantissa up from the rest of the first word and the other three.

1 Like

Unless the algorithm is for academic interest... why not use the built in Buffer functions ? readDoubleBE - https://nodejs.org/docs/latest-v8.x/api/buffer.html#buffer_buf_readdoublebe_offset_noassert

[{"id":"bc5b882d.b961d8","type":"function","z":"adc578f5.614308","name":"IEEE-754 to float","func":"\nvar buf = Buffer.from([0x40,0xa6,0x63,0x95,0x33,0x67,0x3,0xe]);\nmsg.payload = buf.readDoubleBE(0);\nreturn msg","outputs":1,"noerr":0,"x":350,"y":1580,"wires":[["61eac4ea.e82a6c"]]},{"id":"61eac4ea.e82a6c","type":"debug","z":"adc578f5.614308","name":"Value","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":560,"y":1580,"wires":[]},{"id":"5c471d6a.442cb4","type":"inject","z":"adc578f5.614308","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":1580,"wires":[["bc5b882d.b961d8"]]}]
2 Likes

Thank you all very much.
The result was the following.

[{"id":"485407f3.a2f768","type":"inject","z":"7b2adc08.02a074","name":"fromeRegesters","topic":"","payload":"{\"data\":[16550,25493,13159,782],\"buffer\":[64,166,99,149,51,103,3,14]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":80,"wires":[["9dd0a3e6.b9bf9"]]},{"id":"9dd0a3e6.b9bf9","type":"function","z":"7b2adc08.02a074","name":"4words to double","func":"//Total Active Energy Import On Peak Tariff\t2.87 kWh\n// PAC3200 801 register\nvar buf = Buffer.from(msg.payload.buffer);\n\nvar value = buf.readDoubleBE(0)*0.001; // Wh to kWh\nmsg.payload = value.toFixed(2) ;\n\n msg.topic = \"tae\"; //Total Active Energy Import On Peak Tariff\n node.status({fill:\"blue\",shape:\"ring\",text:msg.topic + \":\" + msg.payload});\n\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":80,"wires":[["da09114e.8482a"]]},{"id":"da09114e.8482a","type":"debug","z":"7b2adc08.02a074","name":"Value","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":580,"y":80,"wires":[]},{"id":"2dff6e4f.649152","type":"modbus-read","z":"7b2adc08.02a074","name":"","topic":"tap","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"801","quantity":"4","rate":"1","rateUnit":"s","delayOnStart":true,"startDelayTime":"5","server":"d0e66f0d.1db13","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":130,"y":140,"wires":[[],["9dd0a3e6.b9bf9"]]},{"id":"d0e66f0d.1db13","type":"modbus-client","z":"","name":"PAC3200","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"192.168.71.15","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]