Modbus 32 bits Float Conversion

Conversion for two Holding 16bits Modbus register to 32 bits float.

[{"id":"50c06583.5345bc","type":"function","z":"42f2cefc.1b542","name":"Convert_32bits_Foat","func":"var lowBytes = msg.payload.data[0];\nvar highBytes = msg.payload.data[1];\nvar array = [];\nfor ( i = 0 ; i < 32; i++)\n{\n   if ( i < 16)\n   {\n       if ( highBytes % 2 == 0)\n       {\n           array[i] = 0;\n       } else {\n           array[i] = 1;\n       }\n       highBytes = Math.floor(highBytes / 2);\n   } else {\n       if ( lowBytes % 2 == 0)\n       {\n           array[i] = 0;\n       } else {\n           array[i] = 1;\n       }\n       lowBytes = Math.floor(lowBytes / 2);\n   }\n}\nvar sign = Math.pow((-1),array[31]);\nvar e = 1 * array[23] + 2 * array[24] + 4 * array[25] + 8 * array[26] + 16 * array[27] + 32 * array[28] + 64 * array[29] + 128 * array[30];\nvar matissa = 1 + array[22] * Math.pow(2,-1) + array[21] * Math.pow(2,-2) + array[20] * Math.pow(2,-3) + array[19] * Math.pow(2,-4) + array[18] * Math.pow(2,-5) + array[17] * Math.pow(2,-6) + array[16] * Math.pow(2,-7) + array[15] * Math.pow(2,-8) + array[14] * Math.pow(2,-9) + array[13] * Math.pow(2,-10) + array[12] * Math.pow(2,-11) + array[11] * Math.pow(2,-12) + array[10] * Math.pow(2,-13) + array[9] * Math.pow(2,-14) + array[8] * Math.pow(2,-15) + array[7] * Math.pow(2,-16) + array[6] * Math.pow(2,-17) + array[5] * Math.pow(2,-18) + array[4] * Math.pow(2,-19) + array[3] * Math.pow(2,-20) + array[2] * Math.pow(2,-21) + array[1] * Math.pow(2,-22) + array[0] * Math.pow(2,-23);\nvar value = sign * Math.pow(2,(e-127)) * matissa;\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":900,"wires":[["7041f45d.e3d39c"]]}]

Flow Info

Created 8 months, 3 weeks ago
Rating: not yet rated

Owner

Node Types

Core
  • function (x1)

Tags

  • Modbus
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option