How to get modbus float from register?

I want to get the true value of my MODBUS exit, but I don't know how to do that, and I try differents ways to get frecuency, but I don't know if my configuration are OK, or my connection, or my registers









Hi. Can you copy from debug (use the copy button that appears when you hover the payload in debug window), the value that comes out of your modbus node paste it into a reply.

Also, provide the (final) values you expect.

Lastly export your flow and paste it in the reply.

I'll take a look for you.

1 Like

To update, I was made a little changes like 3 differents ways to read the frequency, this should be 60Hz but the results gives different.

[{"id":"f36ea9b3.c1f848","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"ccb8d916.673918","type":"modbus-response","z":"f36ea9b3.c1f848","name":"","registerShowMax":"20","x":830,"y":140,"wires":},{"id":"82ed37e6.fbd608","type":"debug","z":"f36ea9b3.c1f848","name":"Value","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":980,"y":280,"wires":},{"id":"51b46ff.e5af79","type":"function","z":"f36ea9b3.c1f848","name":"DECODE","func":"m = msg.payload.data;\nreturn {payload: m};","outputs":1,"noerr":0,"x":560,"y":220,"wires":[["dbeccd4a.de895","9c830e95.b7a2b","3e194060.1472f","bdda5464.b6f248"]]},{"id":"dbeccd4a.de895","type":"buffer-parser","z":"f36ea9b3.c1f848","name":"none","data":"payload","dataType":"msg","specification":"{"options":{"byteSwap":false,"resultType":"buffer","msgProperty":"payload"}}","specificationType":"json","x":790,"y":220,"wires":[["82ed37e6.fbd608","dad37ba4.f55178"]]},{"id":"dad37ba4.f55178","type":"function","z":"f36ea9b3.c1f848","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 == 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 >> 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":1010,"y":220,"wires":[["91e172c.1fcd09"]]},{"id":"91e172c.1fcd09","type":"debug","z":"f36ea9b3.c1f848","name":"Value ","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1220,"y":280,"wires":},{"id":"6e024185.78f7d","type":"modbus-response","z":"f36ea9b3.c1f848","name":"","registerShowMax":"20","x":830,"y":80,"wires":},{"id":"6122db38.51e614","type":"modbus-read","z":"f36ea9b3.c1f848","name":"Lectura","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"","dataType":"InputRegister","adr":"40017","quantity":"2","rate":"5","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"7fa95da6.411994","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":390,"y":80,"wires":[["6e024185.78f7d"],["ccb8d916.673918","51b46ff.e5af79"]]},{"id":"3e194060.1472f","type":"function","z":"f36ea9b3.c1f848","name":"CHIDO","func":"const a = msg.payload;\nconst a1 = (a[0] >> 8) & 255;\nconst a2 = a[0] & 255;\nconst a3 = (a[1] >> 8) & 255;\nconst a4 = a[1] & 255;\nconst buf = Buffer.from([a4,a3,a2,a1]);\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"x":800,"y":360,"wires":[["171da40f.106ffc"]]},{"id":"171da40f.106ffc","type":"debug","z":"f36ea9b3.c1f848","name":"Resultadito1","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1040,"y":360,"wires":},{"id":"9c830e95.b7a2b","type":"debug","z":"f36ea9b3.c1f848","name":"Value","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":800,"y":280,"wires":},{"id":"bdda5464.b6f248","type":"function","z":"f36ea9b3.c1f848","name":"CHIDO","func":"const a = msg.payload;\nconst a1 = (a[0] >> 8) & 255;\nconst a2 = a[0] & 255;\nconst a3 = (a[1] >> 8) & 255;\nconst a4 = a[1] & 255;\nconst buf = Buffer.from([a1,a2,a3,a4]);\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"x":800,"y":420,"wires":[["6bc8c57f.0cd24c"]]},{"id":"6bc8c57f.0cd24c","type":"debug","z":"f36ea9b3.c1f848","name":"Resultadito2","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1040,"y":420,"wires":},{"id":"7fa95da6.411994","type":"modbus-client","z":"","name":"EQUALIZER","clienttype":"serial","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"14502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB0","serialType":"RTU-BUFFERD","serialBaudrate":"19200","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

[{"id":"f36ea9b3.c1f848","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"ccb8d916.673918","type":"modbus-response","z":"f36ea9b3.c1f848","name":"","registerShowMax":"20","x":830,"y":140,"wires":},{"id":"82ed37e6.fbd608","type":"debug","z":"f36ea9b3.c1f848","name":"Value","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":980,"y":280,"wires":},{"id":"51b46ff.e5af79","type":"function","z":"f36ea9b3.c1f848","name":"DECODE","func":"m = msg.payload.data;\nreturn {payload: m};","outputs":1,"noerr":0,"x":560,"y":220,"wires":[["dbeccd4a.de895","9c830e95.b7a2b","3e194060.1472f","bdda5464.b6f248"]]},{"id":"dbeccd4a.de895","type":"buffer-parser","z":"f36ea9b3.c1f848","name":"none","data":"payload","dataType":"msg","specification":"{"options":{"byteSwap":false,"resultType":"buffer","msgProperty":"payload"}}","specificationType":"json","x":790,"y":220,"wires":[["82ed37e6.fbd608","dad37ba4.f55178"]]},{"id":"dad37ba4.f55178","type":"function","z":"f36ea9b3.c1f848","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 == 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 >> 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":1010,"y":220,"wires":[["91e172c.1fcd09"]]},{"id":"91e172c.1fcd09","type":"debug","z":"f36ea9b3.c1f848","name":"Value ","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1220,"y":280,"wires":},{"id":"6e024185.78f7d","type":"modbus-response","z":"f36ea9b3.c1f848","name":"","registerShowMax":"20","x":830,"y":80,"wires":},{"id":"6122db38.51e614","type":"modbus-read","z":"f36ea9b3.c1f848","name":"Lectura","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"","dataType":"InputRegister","adr":"40017","quantity":"2","rate":"5","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"7fa95da6.411994","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":390,"y":80,"wires":[["6e024185.78f7d"],["ccb8d916.673918","51b46ff.e5af79"]]},{"id":"3e194060.1472f","type":"function","z":"f36ea9b3.c1f848","name":"CHIDO","func":"const a = msg.payload;\nconst a1 = (a[0] >> 8) & 255;\nconst a2 = a[0] & 255;\nconst a3 = (a[1] >> 8) & 255;\nconst a4 = a[1] & 255;\nconst buf = Buffer.from([a4,a3,a2,a1]);\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"x":800,"y":360,"wires":[["171da40f.106ffc"]]},{"id":"171da40f.106ffc","type":"debug","z":"f36ea9b3.c1f848","name":"Resultadito1","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1040,"y":360,"wires":},{"id":"9c830e95.b7a2b","type":"debug","z":"f36ea9b3.c1f848","name":"Value","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":800,"y":280,"wires":},{"id":"bdda5464.b6f248","type":"function","z":"f36ea9b3.c1f848","name":"CHIDO","func":"const a = msg.payload;\nconst a1 = (a[0] >> 8) & 255;\nconst a2 = a[0] & 255;\nconst a3 = (a[1] >> 8) & 255;\nconst a4 = a[1] & 255;\nconst buf = Buffer.from([a1,a2,a3,a4]);\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"x":800,"y":420,"wires":[["6bc8c57f.0cd24c"]]},{"id":"6bc8c57f.0cd24c","type":"debug","z":"f36ea9b3.c1f848","name":"Resultadito2","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1040,"y":420,"wires":},{"id":"7fa95da6.411994","type":"modbus-client","z":"","name":"EQUALIZER","clienttype":"serial","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"14502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB0","serialType":"RTU-BUFFERD","serialBaudrate":"19200","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

Hi. None of your exports are usable. Please go back and edit your posts after reading this...

... it is important to post code between two sets of three backticks - ``` - see this post for more details - How to share code or flow json

1 Like

Thanks Steve, it´s here my code

[{"id":"f36ea9b3.c1f848","type":"tab","label":"Flow 3","disabled":false,"info":"``[{\"id\":\"f36ea9b3.c1f848\",\"type\":\"tab\",\"label\":\"Flow 3\",\"disabled\":false,\"info\":\"``\"},{\"id\":\"ccb8d916.673918\",\"type\":\"modbus-response\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"\",\"registerShowMax\":\"20\",\"x\":830,\"y\":140,\"wires\":[]},{\"id\":\"82ed37e6.fbd608\",\"type\":\"debug\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"View Value1\",\"active\":false,\"tosidebar\":true,\"console\":true,\"tostatus\":true,\"complete\":\"payload\",\"targetType\":\"msg\",\"x\":1000,\"y\":280,\"wires\":[]},{\"id\":\"51b46ff.e5af79\",\"type\":\"function\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"DATA\",\"func\":\"m = msg.payload.data;\\nreturn {payload: m};\",\"outputs\":1,\"noerr\":0,\"x\":550,\"y\":220,\"wires\":[[\"dbeccd4a.de895\",\"9c830e95.b7a2b\",\"3e194060.1472f\",\"bdda5464.b6f248\"]]},{\"id\":\"dbeccd4a.de895\",\"type\":\"buffer-parser\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"to buffer\",\"data\":\"payload\",\"dataType\":\"msg\",\"specification\":\"{\\\"options\\\":{\\\"byteSwap\\\":false,\\\"resultType\\\":\\\"buffer\\\",\\\"msgProperty\\\":\\\"payload\\\"}}\",\"specificationType\":\"json\",\"x\":800,\"y\":220,\"wires\":[[\"82ed37e6.fbd608\",\"dad37ba4.f55178\"]]},{\"id\":\"dad37ba4.f55178\",\"type\":\"function\",\"z\":\"f36ea9b3.c1f848\",\"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 == 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 >> 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\":1010,\"y\":220,\"wires\":[[\"91e172c.1fcd09\"]]},{\"id\":\"91e172c.1fcd09\",\"type\":\"debug\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"Result 3\",\"active\":false,\"tosidebar\":true,\"console\":true,\"tostatus\":true,\"complete\":\"payload\",\"targetType\":\"msg\",\"x\":1230,\"y\":280,\"wires\":[]},{\"id\":\"6e024185.78f7d\",\"type\":\"modbus-response\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"\",\"registerShowMax\":\"20\",\"x\":830,\"y\":60,\"wires\":[]},{\"id\":\"6122db38.51e614\",\"type\":\"modbus-read\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"READ FRECUENCY\",\"topic\":\"\",\"showStatusActivities\":false,\"logIOActivities\":false,\"showErrors\":false,\"unitid\":\"\",\"dataType\":\"InputRegister\",\"adr\":\"17\",\"quantity\":\"2\",\"rate\":\"5\",\"rateUnit\":\"s\",\"delayOnStart\":false,\"startDelayTime\":\"\",\"server\":\"7fa95da6.411994\",\"useIOFile\":false,\"ioFile\":\"\",\"useIOForPayload\":false,\"emptyMsgOnFail\":false,\"x\":430,\"y\":80,\"wires\":[[\"6e024185.78f7d\"],[\"ccb8d916.673918\",\"51b46ff.e5af79\"]]},{\"id\":\"3e194060.1472f\",\"type\":\"function\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"Other way \",\"func\":\"const a =  msg.payload;\\nconst a1 = (a[0] >> 8) & 255;\\nconst a2 = a[0] & 255;\\nconst a3 = (a[1] >> 8) & 255;\\nconst a4 = a[1] & 255;\\nconst buf = Buffer.from([a4,a3,a2,a1]);\\nmsg.payload = buf.readFloatBE(0);\\nreturn msg;\",\"outputs\":1,\"noerr\":0,\"x\":810,\"y\":360,\"wires\":[[\"171da40f.106ffc\"]]},{\"id\":\"171da40f.106ffc\",\"type\":\"debug\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"Result 1\",\"active\":false,\"tosidebar\":true,\"console\":true,\"tostatus\":true,\"complete\":\"payload\",\"targetType\":\"msg\",\"x\":1030,\"y\":360,\"wires\":[]},{\"id\":\"9c830e95.b7a2b\",\"type\":\"debug\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"View Value 2\",\"active\":false,\"tosidebar\":true,\"console\":true,\"tostatus\":true,\"complete\":\"payload\",\"targetType\":\"msg\",\"x\":800,\"y\":280,\"wires\":[]},{\"id\":\"bdda5464.b6f248\",\"type\":\"function\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"Other way\",\"func\":\"const a =  msg.payload;\\nconst a1 = (a[0] >> 8) & 255;\\nconst a2 = a[0] & 255;\\nconst a3 = (a[1] >> 8) & 255;\\nconst a4 = a[1] & 255;\\nconst buf = Buffer.from([a1,a2,a3,a4]);\\nmsg.payload = buf.readFloatBE(0);\\nreturn msg;\",\"outputs\":1,\"noerr\":0,\"x\":810,\"y\":420,\"wires\":[[\"6bc8c57f.0cd24c\"]]},{\"id\":\"6bc8c57f.0cd24c\",\"type\":\"debug\",\"z\":\"f36ea9b3.c1f848\",\"name\":\"Result 2\",\"active\":false,\"tosidebar\":true,\"console\":true,\"tostatus\":true,\"complete\":\"payload\",\"targetType\":\"msg\",\"x\":1030,\"y\":420,\"wires\":[]},{\"id\":\"7fa95da6.411994\",\"type\":\"modbus-client\",\"z\":\"\",\"name\":\"EQUALIZER\",\"clienttype\":\"serial\",\"bufferCommands\":true,\"stateLogEnabled\":true,\"queueLogEnabled\":true,\"tcpHost\":\"127.0.0.1\",\"tcpPort\":\"14502\",\"tcpType\":\"DEFAULT\",\"serialPort\":\"/dev/ttyUSB0\",\"serialType\":\"RTU-BUFFERD\",\"serialBaudrate\":\"19200\",\"serialDatabits\":\"8\",\"serialStopbits\":\"1\",\"serialParity\":\"none\",\"serialConnectionDelay\":\"100\",\"unit_id\":\"1\",\"commandDelay\":\"1\",\"clientTimeout\":\"1000\",\"reconnectOnTimeout\":true,\"reconnectTimeout\":\"2000\",\"parallelUnitIdsAllowed\":true}```"},{"id":"ccb8d916.673918","type":"modbus-response","z":"f36ea9b3.c1f848","name":"","registerShowMax":"20","x":830,"y":140,"wires":[]},{"id":"82ed37e6.fbd608","type":"debug","z":"f36ea9b3.c1f848","name":"View Value1","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1000,"y":280,"wires":[]},{"id":"51b46ff.e5af79","type":"function","z":"f36ea9b3.c1f848","name":"DATA","func":"m = msg.payload.data;\nreturn {payload: m};","outputs":1,"noerr":0,"x":550,"y":220,"wires":[["dbeccd4a.de895","9c830e95.b7a2b","3e194060.1472f","bdda5464.b6f248"]]},{"id":"dbeccd4a.de895","type":"buffer-parser","z":"f36ea9b3.c1f848","name":"to buffer","data":"payload","dataType":"msg","specification":"{\"options\":{\"byteSwap\":false,\"resultType\":\"buffer\",\"msgProperty\":\"payload\"}}","specificationType":"json","x":800,"y":220,"wires":[["82ed37e6.fbd608","dad37ba4.f55178"]]},{"id":"dad37ba4.f55178","type":"function","z":"f36ea9b3.c1f848","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 == 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 >> 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":1010,"y":220,"wires":[["91e172c.1fcd09"]]},{"id":"91e172c.1fcd09","type":"debug","z":"f36ea9b3.c1f848","name":"Result 3","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1230,"y":280,"wires":[]},{"id":"6e024185.78f7d","type":"modbus-response","z":"f36ea9b3.c1f848","name":"","registerShowMax":"20","x":830,"y":60,"wires":[]},{"id":"6122db38.51e614","type":"modbus-read","z":"f36ea9b3.c1f848","name":"READ FRECUENCY","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"","dataType":"InputRegister","adr":"17","quantity":"2","rate":"5","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"7fa95da6.411994","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":430,"y":80,"wires":[["6e024185.78f7d"],["ccb8d916.673918","51b46ff.e5af79"]]},{"id":"3e194060.1472f","type":"function","z":"f36ea9b3.c1f848","name":"Other way ","func":"const a =  msg.payload;\nconst a1 = (a[0] >> 8) & 255;\nconst a2 = a[0] & 255;\nconst a3 = (a[1] >> 8) & 255;\nconst a4 = a[1] & 255;\nconst buf = Buffer.from([a4,a3,a2,a1]);\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"x":810,"y":360,"wires":[["171da40f.106ffc"]]},{"id":"171da40f.106ffc","type":"debug","z":"f36ea9b3.c1f848","name":"Result 1","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1030,"y":360,"wires":[]},{"id":"9c830e95.b7a2b","type":"debug","z":"f36ea9b3.c1f848","name":"View Value 2","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":800,"y":280,"wires":[]},{"id":"bdda5464.b6f248","type":"function","z":"f36ea9b3.c1f848","name":"Other way","func":"const a =  msg.payload;\nconst a1 = (a[0] >> 8) & 255;\nconst a2 = a[0] & 255;\nconst a3 = (a[1] >> 8) & 255;\nconst a4 = a[1] & 255;\nconst buf = Buffer.from([a1,a2,a3,a4]);\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"x":810,"y":420,"wires":[["6bc8c57f.0cd24c"]]},{"id":"6bc8c57f.0cd24c","type":"debug","z":"f36ea9b3.c1f848","name":"Result 2","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1030,"y":420,"wires":[]},{"id":"7fa95da6.411994","type":"modbus-client","z":"","name":"EQUALIZER","clienttype":"serial","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"14502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB0","serialType":"RTU-BUFFERD","serialBaudrate":"19200","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

EDIT By Steve-Mcl - I wrapped the code correctly as described in the link

```
this is how you wrap code!
```

1 Like

my problem is the way to get the float result and I don't know, whether is the way to get the float or the registers that I write ...

1 Like

Carlos,

Add a debug like this...
image

Set the debug like this...
image

then copy the result like this...
image

Then paste it back into a reply between three back ticks...
```
paste between backticks like this
```

Also, please tell me what value to actually expect it to be
(e.g. modbus returns [123,456] but I expect it to be 45.784)

Hi Steve, the result is this I expect 60.04 hz and modbus returns [0,14656]

{"topic":"polling","payload":[0,14656],"responseBuffer":{"data":[0,14656],"buffer":[0,0,57,64]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"17","quantity":"2","messageId":"5f05d2baf9034ea2d876585d"},"queueLengthByUnitId":{"unitId":1,"queueLength":0},"queueUnitId":1,"unitId":1},"sendingNodeId":"6122db38.51e614","_msgid":"d2fa4546.b77ec8"}

Are you 100% certain you are reading the correct register?

I cannot get 60.04 from [0,14656] ([0,0,57,64])

closest I get is

  • 2.890625 (floatle) or 14656 (int32be) or 1077477376 (int32le)
  • after swap16
    • i get 0.00018310546875 (floatle) or 16441 (int32be) or 960495616 (int32le)

Do any of those look like modbus values in other registers?

Perhaps you might want to read 20 items starting at register 1 - then we can look for value 60.04

We use this software called "simply Modbus Master" and we want to do the same in Node red, but we are beginners in Node red

this is the behavior of this software ... and we want to replicate this in node red

Register 17 may not be 40017 (modbus std is all over the place - sometimes addresses are zero based)

so try asking for register 15 - and get 6 values

then tell me what the data is (like before) and what it should be.

using your screen shot above for register 40017...
image

I took the byte values [0x42,0x47,0xf4,0x0] and faked a modbus reply...

As you can see - I get the expected value (below) - that leads me to suspect the values in your previous are incorrect or you are out by 1 register (the offset problem)...

The flow i used...

[{"id":"96dab12c.71f16","type":"buffer-parser","z":"45e696b4.b511b8","name":"","data":"responseBuffer.buffer","dataType":"msg","specification":"{\"options\":{\"byteSwap\":[],\"resultType\":\"object\",\"msgProperty\":\"payload\",\"singleResult\":true,\"setTopic\":true},\"items\":[{\"name\":\"frequeuncy\",\"type\":\"floatbe\",\"offset\":0}]}","specificationType":"json","x":830,"y":80,"wires":[["a1889b2c.de8a28"]]},{"id":"102736e7.2a06e9","type":"inject","z":"45e696b4.b511b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":500,"y":80,"wires":[["27efbf42.fd082"]]},{"id":"27efbf42.fd082","type":"function","z":"45e696b4.b511b8","name":"fake modbus","func":"\nreturn {\n\n    \"responseBuffer\":{\n        \"buffer\": Buffer.from([0x42,0x47,0xf4,0x0]),\n    }\n    \n};","outputs":1,"noerr":0,"x":650,"y":80,"wires":[["96dab12c.71f16"]]},{"id":"a1889b2c.de8a28","type":"debug","z":"45e696b4.b511b8","name":"Result 1","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":990,"y":80,"wires":[]},{"id":"b1e179f7.02ef98","type":"buffer-parser","z":"45e696b4.b511b8","name":"","data":"responseBuffer.buffer","dataType":"msg","specification":"{\"options\":{\"byteSwap\":[\"swap16\"],\"resultType\":\"object\",\"msgProperty\":\"payload\",\"singleResult\":true,\"setTopic\":true},\"items\":[{\"name\":\"frequeuncy\",\"type\":\"floatbe\",\"offset\":0}]}","specificationType":"json","x":850,"y":160,"wires":[["bf4a585b.ddc5d8"]]},{"id":"65d1ed7b.d5a7a4","type":"inject","z":"45e696b4.b511b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":520,"y":160,"wires":[["f7557446.d3c018"]]},{"id":"f7557446.d3c018","type":"function","z":"45e696b4.b511b8","name":"fake modbus","func":"//data is byte reversed in this one\nreturn {\n\n    \"responseBuffer\":{\n        \"buffer\": Buffer.from([0x47, 0x42, 0x0, 0xf4]),\n    }\n    \n};","outputs":1,"noerr":0,"x":670,"y":160,"wires":[["b1e179f7.02ef98"]]},{"id":"bf4a585b.ddc5d8","type":"debug","z":"45e696b4.b511b8","name":"Result 1","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1010,"y":160,"wires":[]}]
2 Likes

this is the diferent result from register 1 to 20
register 1:

{"topic":"polling","payload":[0,14720],"responseBuffer":{"data":[0,14720],"buffer":[0,0,57,128]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"1","quantity":"2","messageId":"5f05da59f9034ea2d87659dc"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"3f8e82ea.e5671e"}

register 2:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"2","quantity":"2","messageId":"5f05da78f9034ea2d87659e2"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"2c0f86c8.7b74ea"}

register 3:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"3","quantity":"2","messageId":"5f05da92f9034ea2d87659e6"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"166e9cc0.b063e3"}

register 4:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"4","quantity":"2","messageId":"5f05dab6f9034ea2d87659ed"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"ad902b75.914b58"}

register 5:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"5","quantity":"2","messageId":"5f05dad0f9034ea2d87659f2"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"fc6fdc1b.b67bc"}

register 6:

{"topic":"polling","payload":[5120,16940],"responseBuffer":{"data":[5120,16940],"buffer":[20,0,66,44]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"6","quantity":"2","messageId":"5f05daf1f9034ea2d87659f8"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"f746339b.9661e"}

register 7:

{"topic":"polling","payload":[0,32320],"responseBuffer":{"data":[0,32320],"buffer":[0,0,126,64]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"7","quantity":"2","messageId":"5f05db61f9034ea2d8765a0e"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"745fac67.06e6e4"}

register 8:

{"topic":"polling","payload":[16896,17152],"responseBuffer":{"data":[16896,17152],"buffer":[66,0,67,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"8","quantity":"2","messageId":"5f0603f2e1de2ec254d1ffa3"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"e823feb3.4231"}

register 9:

{"topic":"polling","payload":[0,14720],"responseBuffer":{"data":[0,14720],"buffer":[0,0,57,128]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"9","quantity":"2","messageId":"5f06045ce1de2ec254d1ffe2"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"2df6e45f.f9d80c"}

register 10:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"10","quantity":"2","messageId":"5f0604dde1de2ec254d20062"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"7531243b.4f5b3c"}

register 11:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"11","quantity":"2","messageId":"5f0604f0e1de2ec254d20073"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"c678b154.4b612"}

register 12:

{"topic":"polling","payload":[1536,17152],"responseBuffer":{"data":[1536,17152],"buffer":[6,0,67,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"12","quantity":"2","messageId":"5f06052be1de2ec254d200ac"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"87af8a4b.3265d8"}

register 13:

{"topic":"polling","payload":[0,32256],"responseBuffer":{"data":[0,32256],"buffer":[0,0,126,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"13","quantity":"2","messageId":"5f060546e1de2ec254d200c6"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"f6a0b4fe.93cf18"}

register 14:

{"topic":"polling","payload":[17408,17066],"responseBuffer":{"data":[17408,17066],"buffer":[68,0,66,170]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"14","quantity":"2","messageId":"5f06056ee1de2ec254d200ed"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"741c6693.bf8708"}

register 15:

{"topic":"polling","payload":[0,32352],"responseBuffer":{"data":[0,32352],"buffer":[0,0,126,96]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"15","quantity":"2","messageId":"5f060654e1de2ec254d201ce"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"5604976.bae6068"}

register 16:

{"topic":"polling","payload":[65024,17007],"responseBuffer":{"data":[65024,17007],"buffer":[254,0,66,111]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"16","quantity":"2","messageId":"5f06066de1de2ec254d201e5"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"9a22557d.d50f38"}

register 17:

{"topic":"polling","payload":[0,14656],"responseBuffer":{"data":[0,14656],"buffer":[0,0,57,64]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"17","quantity":"2","messageId":"5f061138e1de2ec254d20a0c"},"queueLengthByUnitId":{"unitId":1,"queueLength":0},"queueUnitId":1,"unitId":1},"sendingNodeId":"6122db38.51e614","_msgid":"1a1f0d1f.60cd23"}

register 18:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"18","quantity":"2","messageId":"5f0606c7e1de2ec254d2023d"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"8921b5e1.5cdcd8"}

register 19:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"19","quantity":"2","messageId":"5f0606dde1de2ec254d20251"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"e67f6494.2fd548"}

register 20:

{"topic":"polling","payload":[0,0],"responseBuffer":{"data":[0,0],"buffer":[0,0,0,0]},"input":{"topic":"polling","from":"READ FRECUENCY","payload":{"unitid":"","fc":4,"address":"20","quantity":"2","messageId":"5f0606f1e1de2ec254d20263"},"queueLengthByUnitId":{"unitId":2,"queueLength":0},"queueUnitId":2,"unitId":2},"sendingNodeId":"6122db38.51e614","_msgid":"c86ceac9.f37ae8"}

Is the sample flow I did for you not enough to help you figure this out? I have already proved / shown you it works.

Have you tried pushing these values through the buffer-parser?

Also, why did you do 20 individual polls and not just request 20 items? Polling multiple items if far more efficient and ensures data consistency (between each other that is - like a point in time sample)

What is your end goal? I assume it is more than 1 register? If you need all registers from 1 to 20, you should make 1 request (poll) for 20 items and setup the buffer-parser to convert all your values in one go.

Yes is the same flow, I can resolve if I have the correct registers, the logic of my program in node red is correct and you confirmed me about it, I think my registers are no correct the all... thank you!

My final purpose is read one register like "simply modbus" and you can select the measure than you want to see

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