Working between Floating Point and Int. data types

I have a piece of equipment which has the ability to send data out either as floating point or int. If I set it to output in Int. I can use a Modbus Read and see the values fine.

However if I set it to Float, I get values which I need to combine to make the same Int values. For example in a PLC program I could set the Low Word to 0 and the High Word to 17052 and get the result of 78. I have tried to use Colins IEEE-754 to Float, but have not been able to make it work for me yet. What approach would work for this task?

Thanks,

You could probably use a function node with these buffer methods:
https://nodejs.org/api/buffer.html#buffer_buf_readdoublebe_offset

17052 in hex is 429c, so 17052,0 is 0x429c0000.
Using the flow you referenced and feeding 0x429c0000 into it yields 78 as you suggested. The flow will accept a byte array so the easiest is probably to divide each word into two bytes and feed them in. Unless you can read them as bytes in the first place.

So I have had some success using the previously referenced method combined with the following:

https://discourse.nodered.org/t/modbus-tcp-read-32-bit-registers/1710/2

I am getting a lot of:
"Error: FSM Not Ready To Read At State STOPED"
errors, but I am assuming that is because I am not using the best method for multiple connections to the device. I will work on that, but I wanted to ask why I may be getting some variation in the output valves from what the unit display shows? For example for Load and Rate the values are right on. Load = 59 on display and 59 in Node-Red & Rate = 708 on scale display and 708 in Node-Red. However, the scale displays a totalizer value of say 16371 but Node-Red will show 16320. The Master Total on the scale is 760983 but is - 757760 in Node-Red. Is this a data type error I am not getting correct?

[{"id":"a20a0c7a.58aa5","type":"ping","z":"63c544b0.377b7c","name":"","host":"10.81.210.83","timer":"20","x":250,"y":240,"wires":[["e97704a0.3adae8"]]},{"id":"e97704a0.3adae8","type":"debug","z":"63c544b0.377b7c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":510,"y":240,"wires":[]},{"id":"d6a068f.01b6898","type":"modbus-write","z":"63c544b0.377b7c","d":true,"name":"","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"MHoldingRegisters","adr":"1025","quantity":"2","server":"ed8ff803.7cd708","x":460,"y":360,"wires":[["668471ed.2d32a"],[]]},{"id":"b1e1b014.baf61","type":"inject","z":"63c544b0.377b7c","name":"","topic":"","payload":"[17538,17538]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":320,"wires":[["d6a068f.01b6898"]]},{"id":"668471ed.2d32a","type":"debug","z":"63c544b0.377b7c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":750,"y":320,"wires":[]},{"id":"7f5cb91.df46848","type":"modbus-write","z":"63c544b0.377b7c","d":true,"name":"","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"MHoldingRegisters","adr":"1025","quantity":"2","server":"ed8ff803.7cd708","x":460,"y":440,"wires":[["2390cec6.bdce12"],[]]},{"id":"46958a85.ac4544","type":"inject","z":"63c544b0.377b7c","name":"","topic":"","payload":"[0,16]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":400,"wires":[["7f5cb91.df46848"]]},{"id":"2390cec6.bdce12","type":"debug","z":"63c544b0.377b7c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":750,"y":400,"wires":[]},{"id":"6c9c8574.63a2fc","type":"modbus-write","z":"63c544b0.377b7c","d":true,"name":"","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"MHoldingRegisters","adr":"1025","quantity":"2","server":"ed8ff803.7cd708","x":460,"y":520,"wires":[["ee6b150c.635258"],[]]},{"id":"4df7ae5.022845","type":"inject","z":"63c544b0.377b7c","name":"","topic":"","payload":"[16]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":480,"wires":[["6c9c8574.63a2fc"]]},{"id":"ee6b150c.635258","type":"debug","z":"63c544b0.377b7c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":750,"y":480,"wires":[]},{"id":"d57488d2.ba1848","type":"function","z":"63c544b0.377b7c","name":"IEEE-754 to float","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 == 2) {\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 >> 23) & 0xFF) - 127;\n    var significand = (bytes & ~(-1 << 23));\n\n    if (exponent == 128) \n        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n    if (exponent == -127) {\n        if (significand === 0) return sign * 0.0;\n        exponent = -126;\n        significand /= (1 << 22);\n    } else significand = (significand | (1 << 23)) / (1 << 23);\n\n    return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":810,"y":800,"wires":[["b583499a.503808"]]},{"id":"b583499a.503808","type":"debug","z":"63c544b0.377b7c","name":"Load","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1250,"y":1000,"wires":[]},{"id":"8c6d5df6.63056","type":"function","z":"63c544b0.377b7c","name":"Convert byte array to SIGNED 32-bit integer","func":"// Create new Buffer based on array bytes\nconst buf = Buffer.from(msg.payload.buffer);\n\n// Represent these bytes as 32-bit unsigned int\nconst value = buf.readInt32BE();\n\n// save the value\nmsg.payload = value;\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":800,"wires":[["d57488d2.ba1848"]]},{"id":"17188367.3d45cd","type":"modbus-read","z":"63c544b0.377b7c","name":"Read 920i Load","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"5","quantity":"2","rate":"3","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"ed8ff803.7cd708","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":160,"y":740,"wires":[["8a2b1e23.32eef"],["8c6d5df6.63056"]]},{"id":"8a2b1e23.32eef","type":"modbus-response","z":"63c544b0.377b7c","name":"","registerShowMax":20,"x":450,"y":740,"wires":[]},{"id":"e0b09a1a.0e54c8","type":"catch","z":"63c544b0.377b7c","name":"","scope":null,"uncaught":false,"x":120,"y":1560,"wires":[["46b17d7b.9d7fa4"]]},{"id":"46b17d7b.9d7fa4","type":"debug","z":"63c544b0.377b7c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":340,"y":1600,"wires":[]},{"id":"4f438621.5569e8","type":"function","z":"63c544b0.377b7c","name":"IEEE-754 to float","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 == 2) {\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 >> 23) & 0xFF) - 127;\n    var significand = (bytes & ~(-1 << 23));\n\n    if (exponent == 128) \n        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n    if (exponent == -127) {\n        if (significand === 0) return sign * 0.0;\n        exponent = -126;\n        significand /= (1 << 22);\n    } else significand = (significand | (1 << 23)) / (1 << 23);\n\n    return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":810,"y":960,"wires":[["190d3f03.1929c1","b583499a.503808"]]},{"id":"190d3f03.1929c1","type":"debug","z":"63c544b0.377b7c","name":"Rate","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1270,"y":1100,"wires":[]},{"id":"489f383e.5ab538","type":"function","z":"63c544b0.377b7c","name":"Convert byte array to SIGNED 32-bit integer","func":"// Create new Buffer based on array bytes\nconst buf = Buffer.from(msg.payload.buffer);\n\n// Represent these bytes as 32-bit unsigned int\nconst value = buf.readInt32BE();\n\n// save the value\nmsg.payload = value;\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":960,"wires":[["4f438621.5569e8"]]},{"id":"69db4071.2903e","type":"modbus-read","z":"63c544b0.377b7c","name":"Read 920i Rate","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"7","quantity":"2","rate":"3","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"ed8ff803.7cd708","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":160,"y":900,"wires":[["2796f80e.7d8af8"],["489f383e.5ab538"]]},{"id":"2796f80e.7d8af8","type":"modbus-response","z":"63c544b0.377b7c","name":"","registerShowMax":20,"x":450,"y":880,"wires":[]},{"id":"346fc1e.e5bf13e","type":"function","z":"63c544b0.377b7c","name":"IEEE-754 to float","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 == 2) {\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 >> 23) & 0xFF) - 127;\n    var significand = (bytes & ~(-1 << 23));\n\n    if (exponent == 128) \n        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n    if (exponent == -127) {\n        if (significand === 0) return sign * 0.0;\n        exponent = -126;\n        significand /= (1 << 22);\n    } else significand = (significand | (1 << 23)) / (1 << 23);\n\n    return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":810,"y":1140,"wires":[["16656812.be00a8","b583499a.503808"]]},{"id":"16656812.be00a8","type":"debug","z":"63c544b0.377b7c","name":"Speed","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1250,"y":1180,"wires":[]},{"id":"d499a583.9fdc88","type":"function","z":"63c544b0.377b7c","name":"Convert byte array to SIGNED 32-bit integer","func":"// Create new Buffer based on array bytes\nconst buf = Buffer.from(msg.payload.buffer);\n\n// Represent these bytes as 32-bit unsigned int\nconst value = buf.readInt32BE();\n\n// save the value\nmsg.payload = value;\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":1140,"wires":[["346fc1e.e5bf13e"]]},{"id":"c57c660f.3c1938","type":"modbus-read","z":"63c544b0.377b7c","name":"Read 920i Speed","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"9","quantity":"2","rate":"3","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"ed8ff803.7cd708","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":160,"y":1080,"wires":[["c9fea5b0.6cdf48"],["d499a583.9fdc88"]]},{"id":"c9fea5b0.6cdf48","type":"modbus-response","z":"63c544b0.377b7c","name":"","registerShowMax":20,"x":450,"y":1060,"wires":[]},{"id":"28eb8113.c339ee","type":"function","z":"63c544b0.377b7c","name":"IEEE-754 to float","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 == 2) {\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 >> 23) & 0xFF) - 127;\n    var significand = (bytes & ~(-1 << 23));\n\n    if (exponent == 128) \n        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n    if (exponent == -127) {\n        if (significand === 0) return sign * 0.0;\n        exponent = -126;\n        significand /= (1 << 22);\n    } else significand = (significand | (1 << 23)) / (1 << 23);\n\n    return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":810,"y":1300,"wires":[["dab36a79.eb4a38","b583499a.503808"]]},{"id":"dab36a79.eb4a38","type":"debug","z":"63c544b0.377b7c","name":"Totalizer","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1220,"y":1260,"wires":[]},{"id":"62f08f21.ed4a1","type":"function","z":"63c544b0.377b7c","name":"Convert byte array to SIGNED 32-bit integer","func":"// Create new Buffer based on array bytes\nconst buf = Buffer.from(msg.payload.buffer);\n\n// Represent these bytes as 32-bit unsigned int\nconst value = buf.readInt32BE();\n\n// save the value\nmsg.payload = value;\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":1300,"wires":[["28eb8113.c339ee"]]},{"id":"53d537b.f896dc8","type":"modbus-read","z":"63c544b0.377b7c","name":"Read 920i Totalizer","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"11","quantity":"2","rate":"3","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"ed8ff803.7cd708","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":170,"y":1240,"wires":[["a90e5d9d.de55e"],["62f08f21.ed4a1"]]},{"id":"a90e5d9d.de55e","type":"modbus-response","z":"63c544b0.377b7c","name":"","registerShowMax":20,"x":450,"y":1220,"wires":[]},{"id":"24342a0d.17d3f6","type":"function","z":"63c544b0.377b7c","name":"IEEE-754 to float","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 == 2) {\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 >> 23) & 0xFF) - 127;\n    var significand = (bytes & ~(-1 << 23));\n\n    if (exponent == 128) \n        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n    if (exponent == -127) {\n        if (significand === 0) return sign * 0.0;\n        exponent = -126;\n        significand /= (1 << 22);\n    } else significand = (significand | (1 << 23)) / (1 << 23);\n\n    return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":810,"y":1460,"wires":[["28e2ea6e.57de76","b583499a.503808"]]},{"id":"28e2ea6e.57de76","type":"debug","z":"63c544b0.377b7c","name":"Master Total","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1250,"y":1340,"wires":[]},{"id":"e636ffd5.da3ca","type":"function","z":"63c544b0.377b7c","name":"Convert byte array to SIGNED 32-bit integer","func":"// Create new Buffer based on array bytes\nconst buf = Buffer.from(msg.payload.buffer);\n\n// Represent these bytes as 32-bit unsigned int\nconst value = buf.readInt32BE();\n\n// save the value\nmsg.payload = value;\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":1460,"wires":[["24342a0d.17d3f6"]]},{"id":"89bb8e8f.563d3","type":"modbus-read","z":"63c544b0.377b7c","name":"Read 920i Master Total","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"15","quantity":"2","rate":"3","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"ed8ff803.7cd708","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":180,"y":1400,"wires":[["fac91d2a.8dee"],["e636ffd5.da3ca"]]},{"id":"fac91d2a.8dee","type":"modbus-response","z":"63c544b0.377b7c","name":"","registerShowMax":20,"x":450,"y":1380,"wires":[]},{"id":"ed8ff803.7cd708","type":"modbus-client","z":"","name":"10.81.210.83","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"10.81.210.83","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"}]

What word values are you getting for 16371/16320 and the 760983/-757760 readings?

So at this point the Scale displays a master total of -650866 Node is giving -647168 and I have 0 & 51486 as the values.

If you are using the function mentioned earlier to do the conversion, what values are you feeding it for that output?

I have just run a test, and as far as I can see -647168 is correct, so I cannot explain where -650866 comes from. Where are you seeing the -650866? I find it odd that the master total should be -ve.

The master total in increasing (becoming less negative). Perhaps if I can get it cleared it would make more sense? I will try to clear it next. It may have been some neg value from initialization.

OK, understood.

It is interesting that the second word of the value is zero, but to get the value -650866 requires that word not to be zero, but the 51486 in the first word is correct. What is in the word before the 51486? If the zero you have given is actually from the word before it, what is in the word after?

Ok, after resetting the Master Total I finally have a positive number. The numbers are still slightly different though. I currently have a display value of 3757 on the scale and 3744 in Node-Red. If I Poll address 15 for length of 2 I get 3744 in the Debug. The values are 0 : 17770 & 1 : 0. If I Poll address 14 for length2 I get a value of a huge negative value: -293066355245056 and values 0 :55173 & 1 : 17770.

Excellent, all is now clear. The two words you want are 55173 and 17770, but you need to switch them round, 17770 is the first (most significant) word and 55173 is the second one. 17770 is 0x456a and 55173 is 0xd785. Putting 0x456ad785 into the function gives 3757.5 which I think is what you are looking for.

@jfehl Did that help?

I apologize for the delay. I have tried to switch to two words in the IEEE-754 to float function but have not been able to make it happen. Is that the correct place that I could switch them. I edited the following:

    // four byte array or buffer
    intValue = (msg.payload[0] << 8) + msg.payload[1]; //) << 8) + msg.payload[2]) << 8) + msg.payload[3];

by changing it to :

 // four byte array or buffer
    intValue = (msg.payload[1] << 8) + msg.payload[0]; //) << 8) + msg.payload[2]) << 8) + msg.payload[3];

but that did not seem to change the order of the Array Elements.
Thanks

What is in msg.payload going into the function node?

3/25/2020, 4:49:58 PMnode: f7a85033.9c245msg : Object

object

payload: 0x-3e18bb84

values: array[2]

0: 49639

1: 17532

input: object

_msgid: "bcddb8de.446328"

Sorry, deleted my previous reply as it was not right. I don't understand what it is showing in the payload. Can you post a screenshot showing it in a debug node?

Also it would be better to correct it, so the words are the right way round, before sending to the convert function. Show us the code that builds the message that is sent to the conversion function node.

Here is that one flow:

[{"id":"24342a0d.17d3f6","type":"function","z":"63c544b0.377b7c","name":"IEEE-754 to float","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 == 2) {\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 >> 23) & 0xFF) - 127;\n    var significand = (bytes & ~(-1 << 23));\n\n    if (exponent == 128) \n        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n    if (exponent == -127) {\n        if (significand === 0) return sign * 0.0;\n        exponent = -126;\n        significand /= (1 << 22);\n    } else significand = (significand | (1 << 23)) / (1 << 23);\n\n    return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":810,"y":1440,"wires":[["28e2ea6e.57de76"]]},{"id":"28e2ea6e.57de76","type":"debug","z":"63c544b0.377b7c","name":"Master Total","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1110,"y":1440,"wires":[]},{"id":"e636ffd5.da3ca","type":"function","z":"63c544b0.377b7c","name":"Convert byte array to SIGNED 32-bit integer","func":"// Create new Buffer based on array bytes\nconst buf = Buffer.from(msg.payload.buffer);\n\n// Represent these bytes as 32-bit unsigned int\nconst value = buf.readInt32BE();\n\n// save the value\nmsg.payload = value;\n\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":1460,"wires":[["24342a0d.17d3f6","f7a85033.9c245"]]},{"id":"89bb8e8f.563d3","type":"modbus-read","z":"63c544b0.377b7c","name":"Read 920i Master Total","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"14","quantity":"2","rate":"3","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"ed8ff803.7cd708","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":180,"y":1380,"wires":[["fac91d2a.8dee"],["e636ffd5.da3ca"]]},{"id":"fac91d2a.8dee","type":"modbus-response","z":"63c544b0.377b7c","name":"","registerShowMax":20,"x":510,"y":1380,"wires":[]},{"id":"f7a85033.9c245","type":"debug","z":"63c544b0.377b7c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":1380,"wires":[]},{"id":"ed8ff803.7cd708","type":"modbus-client","z":"","name":"10.81.210.83","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"10.81.210.83","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"}]

Just the code in that one Node ahead of the conversion is:

[{"id":"e636ffd5.da3ca","type":"function","z":"63c544b0.377b7c","name":"Convert byte array to SIGNED 32-bit integer","func":"// Create new Buffer based on array bytes\nconst buf = Buffer.from(msg.payload.buffer);\n\n// Represent these bytes as 32-bit unsigned int\nconst value = buf.readInt32BE();\n\n// save the value\nmsg.payload = value;\n\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":1460,"wires":[["24342a0d.17d3f6","f7a85033.9c245"]]}]

Here you can see the two values in the response:

and here is an expanded shot of the debug:
image

here using the 32 character Node Status:

Thanks.

Try this

// Create new Buffer based on array bytes
const buf = Buffer.from(msg.payload.buffer);
// swap the words
const temp0 = buf[0];
const temp1 = buf[1];
buf[0] = buf[2];
buf[1] = buf[3];
buf[2] = temp0;
buf[3] = temp1;

// Represent these bytes as 32-bit unsigned int
const value = buf.readInt32BE();

// save the value
msg.payload = value;

return msg;

Though I don't understand where that buffer comes from when you posted earlier

where 55173 and 17770 do not appear to be in a buffer. So if my function doesn't work you will have to explain where the 55173 and 17770 came from.

1 Like