Multiple values reading of Modbus addresses

This flow will help you to read multiple values from your Modbus gateway and give you a specifiedd float output.Im using Node-red-contrib-modbus node and some programming in function node.

[{"id":"86577ecec12acc09","type":"tab","label":"Code For Modbus Multiple values.","disabled":false,"info":"","env":[]},{"id":"7ae22c575f492960","type":"function","z":"86577ecec12acc09","name":"Address Generator","func":"var addressesToRead = [2999, 3000, 3001, 3002, 3003, 3004];\nvar messages = [];\n\nfor (var i = 0; i < addressesToRead.length; i++) {\n    var address = addressesToRead[i];\n    var message = {\n        payload: {\n            value: msg.payload,\n            fc: 3,         // Function code for Read Holding Registers\n            unitid: 1,     // Modbus Unit-ID\n            address: address,\n            quantity: addressesToRead.length // Set quantity to the length of addressesToRead\n        }\n    };\n    messages.push(message);\n}\n\nreturn messages;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":260,"wires":[["a9daeea8bee89ac8"]]},{"id":"a9daeea8bee89ac8","type":"modbus-flex-getter","z":"86577ecec12acc09","name":"","showStatusActivities":false,"showErrors":false,"showWarnings":true,"logIOActivities":false,"server":"c14a5c9a081f056f","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":510,"y":260,"wires":[[],["38ddf478d1ebf82b"]]},{"id":"e7a539fee9d7c16d","type":"inject","z":"86577ecec12acc09","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":120,"y":260,"wires":[["7ae22c575f492960"]]},{"id":"38ddf478d1ebf82b","type":"function","z":"86577ecec12acc09","name":"Value Parsing","func":"var valueArray = msg.payload.data;\nvar numPairs = valueArray.length / 2; // Calculate the number of pairs in the array\n\n// Check if the array contains a multiple of 2 values (pairs)\nif (numPairs >= 3) {\n    // Initialize an object to store the calculated values with the D0001, D0002, ... notation\n    var result = {};\n\n    // Loop through the pairs and calculate values\n    for (var i = 0; i < 3; i++) {\n        // Calculate the offset for the current pair of 16-bit values\n        var offset = i * 2;\n\n        // Combine the 16-bit values into a 32-bit integer\n        var combinedValue = (valueArray[offset] << 16) | valueArray[offset + 1];\n\n        // Convert the combined 32-bit integer to a floating-point value\n        var buffer = Buffer.alloc(4); // Create a buffer of 4 bytes\n        buffer.writeInt32BE(combinedValue, 0); // Write the combined value as a big-endian 32-bit integer\n\n        // Generate the key in the D0001, D0002, ... format\n        var key = \"D\" + (\"000\" + (i + 1)).slice(-4);\n\n        // Store the floating-point value in the result object with the key\n        result[key] = parseFloat(buffer.readFloatBE(0).toFixed(4));\n    }\n\n    // Set the result as the new payload\n    msg.payload = result;\n} else {\n    node.warn(\"Expected an array with at least three pairs of 16-bit values.\");\n    return null;\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":260,"wires":[["036e52eecd7c0046"]]},{"id":"036e52eecd7c0046","type":"debug","z":"86577ecec12acc09","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":260,"wires":[]},{"id":"c14a5c9a081f056f","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"failureLogEnabled":true,"tcpHost":"0.0.0.1","tcpPort":"5020","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":"","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":false,"showWarnings":true,"showLogs":true}]

Flow Info

Created 1 year, 8 months ago
Rating: 5 3

Actions

Rate:

Node Types

Core
  • debug (x1)
  • function (x2)
  • inject (x1)
Other
  • modbus-client (x1)
  • modbus-flex-getter (x1)
  • tab (x1)

Tags

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