Decimal value to 32bit Floating Value

Hello,

I am fetching data from the Energy meter 32bit value as voltage parameter, but it is showing in decimal value, i want to convert decimal value into 32bit Float value.

image

Br.
Devendra

Use a function node, where you can do the conversion in javascript

Have a look at this flow: https://flows.nodered.org/flow/359ead34237b7ab6ec0465ee85a34b62

Hello,

I tried but value not showing properly as 226.2 in node red. It is showing (3.1415927410125732) in node red. could you please check my Node Red flow and function node program.

My NodeRed Flow,

[{"id":"f3969f0a.e49d5","type":"modbus-read","z":"4fd9bdd3.eb6384","name":"","topic":"","showStatusActivities":false,"showErrors":false,"unitid":"6","dataType":"HoldingRegister","adr":"0142","quantity":"2","rate":"1","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"545b1a02.09e564","x":150,"y":200,"wires":[["3c024e91.f2db22","128d3072.39e31","48c4f35.c3f3c0c"],[]]},{"id":"3c024e91.f2db22","type":"ui_gauge","z":"4fd9bdd3.eb6384","name":"","group":"3315cd51.7edf92","order":0,"width":0,"height":0,"gtype":"gage","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":610,"y":160,"wires":[]},{"id":"128d3072.39e31","type":"function","z":"4fd9bdd3.eb6384","name":"Array Combine","func":"\nmsg.payload=parseInt(msg.payload[0]+msg.payload[1]); \n\n//msg.payload =float32bit(msg.payload);\nreturn msg;\n\n","outputs":1,"noerr":0,"x":360,"y":240,"wires":[["9c753e62.d81ad","833eced1.0a81e","ea0ce3b8.8788d"]]},{"id":"48c4f35.c3f3c0c","type":"debug","z":"4fd9bdd3.eb6384","name":"","active":false,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":400,"y":140,"wires":[]},{"id":"9c753e62.d81ad","type":"debug","z":"4fd9bdd3.eb6384","name":"","active":false,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":620,"y":260,"wires":[]},{"id":"833eced1.0a81e","type":"ui_gauge","z":"4fd9bdd3.eb6384","name":"","group":"3315cd51.7edf92","order":0,"width":0,"height":0,"gtype":"gage","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":590,"y":220,"wires":[]},{"id":"ea0ce3b8.8788d","type":"function","z":"4fd9bdd3.eb6384","name":"Float Convert","func":"/* Converts from an number, string, buffer or array representing an IEEE-754 value\n * to a javascript float.\n * The following may be given in msg.payload:\n *      A string representing a number, which may be hex or binary\n *        examples, \"1735\"  \"0x02045789\"  0b01000000010010010000111111011011\n *      An integer value\n *      A four element array or buffer of 8 bit values, most significant byte first.\n*/ \n// first make a number from the given payload if necessary\nlet intValue;\nif (typeof msg.payload === \"number\") {\n    intValue = msg.payload;\n} else if (typeof msg.payload === \"string\") {\n    intValue = Number(msg.payload);\n} else if (msg.payload.length == 4) {\n    // four byte array or buffer\n    intValue = (((((msg.payload[0] << 8) + msg.payload[1]) << 8))); // + msg.payload[2]) << 8) + msg.payload[3];\n} else {\n    node.warn(\"Unrecognised payload type or length\");\n}\n\nmsg.payload = Int2Float32(intValue);\nreturn msg;\n\nfunction Int2Float32(bytes) {\n    var sign = (bytes & 0x80000000) ? -1 : 1;\n    var exponent = ((bytes >> 52) & 0xFF) - 1023;\n    var significand = (bytes & ~(-1 << 52));\n\n    if (exponent == 1) \n        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n    if (exponent == -1023) {\n        if (significand === 0) return sign * 0.0;\n        exponent = -1022;\n        significand /= (1 << 51);\n    } else significand = (significand | (1 << 52)) / (1 << 52);\n\n    return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":360,"y":300,"wires":[["4479ad05.a0cb34","767354a8.35537c"]]},{"id":"4479ad05.a0cb34","type":"ui_gauge","z":"4fd9bdd3.eb6384","name":"","group":"3315cd51.7edf92","order":0,"width":0,"height":0,"gtype":"gage","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":590,"y":300,"wires":[]},{"id":"767354a8.35537c","type":"debug","z":"4fd9bdd3.eb6384","name":"","active":false,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":620,"y":340,"wires":[]},{"id":"8b1e824e.1a57","type":"debug","z":"4fd9bdd3.eb6384","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":540,"y":560,"wires":[]},{"id":"545b1a02.09e564","type":"modbus-client","z":"","name":"","clienttype":"serial","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB0","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"even","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"},{"id":"3315cd51.7edf92","type":"ui_group","z":"","name":"Energy Meter","tab":"458e0644.624dc8","disp":true,"width":"6"},{"id":"458e0644.624dc8","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

Are you saying it is incorrectly showing a value remarkably close to the value of pi. That is a remarkable coincidence.

3 Likes

I see that you changed some lines in the function node, why?
This function was exactly doing what you wanted, now not any more. :frowning: that's why you get that ridiculous number out of it.
Do you have a datasheet from the energy meter you have? Want to know if the 4 bytes are big or little endian.

1 Like

@colin you made me think of this https://en.wikipedia.org/wiki/Indiana_Pi_Bill

I too was thinking "how the heck is that msg.payload skirting around PI"

1 Like

The example in this flow https://flows.nodered.org/flow/359ead34237b7ab6ec0465ee85a34b62 uses Pi for demonstration purpose.
So I think that's where Pi is coming from.

I tried with the same code as it is, but showing different value.

Energy meter Sending Frequency (49.5)hz in 32bit Float value and Voltage (234.6)volt variable value over MODBUS RTU. The frequency & Voltages Vary as per the supply coming. but right now i am getting value as (1.3596233292942613). i need value like this 49.5 or 234.6 floating value.

This is my requirement.

Hi,

i am able to represent 32bit float value. below screenshot.

1 Like

hey man you did it I am facing the same problem. I am collecting data from my plc Siemen s7-300 to node-red using raspberry pi 4B (2GB) model over ethernet but my PLC has 317-2DP CPU that's why there is no ethernet port so I am trying to fetch data from Weintek HMI MT8071iE. HMI is connected to PLC through MPI and HMI is connected to raspberry pi using the ethernet cable. I am able to collect the values like these 1106500423 from sensors however HMI shows -134C which is the temperatures. I tried to use the range block but I am not getting proper values it shows some error. Can you help me out?

Hi Umair,

Could you please send me a debug message screenshot? you are receiving in bytes/buffer values formate (Hex/ASCII) and what is the exact value you want to display/show in Float value.

Br,
Devendra

I have figured it out. There was some error in calibration because label on sensor wasn't its own which causes the scaling problem. Thanks for your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.