Node-red-contrib-modbus: Dynamically Change tcp host IP Address?

Hey all,

If anyone has excessive experience with node-red-contrib-modbus, I would love to hear any thoughts on how I could accomplish this.

I am using a Modbus Read node for data communication with an IP device. This has been working great for a long time already.

However, the IP address has to be configured as part of the server configuration within Node-RED itself. This is OK for fixed IP devices, but we are moving to a point where we would like to dynamically update IP address if a new device is added to the network. This involves setting the Modbus server IP address programmatically, instead of having a human go in the node and manually edit the server configuration.

If I could somehow inject the IP address of the modbus server as part of a message object, that would work perfectly, but I haven't had any luck finding reference to this capability in the modbus node documentation.

In flows.json, it's clear where the server is defined, including the IP address:

{
        "id": "4a162f0b3a41154f",
        "type": "modbus-client",
        "name": "SP-A",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": true,
        "queueLogEnabled": true,
        "failureLogEnabled": false,
        "tcpHost": "192.168.10.132",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": 1,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": true
    },

One possibility is to edit the flows.json and restart node-RED, but if there is a way to pass the server info within Node-RED, that would be much simpler for obvious reasons.

I appreciate any thoughts or ideas in advance.

Hi .. the modbus read/write nodes dont accept a msg directly to change the IP configuration
but there is the Modbus Flex Connector node in the same collection that does that.

image

You can send it a msg like :

msg.payload = { 'connectorType': 'TCP', 'tcpHost': '127.0.0.1', 'tcpPort': '10502', 'unitId': 2 } return msg

I havent used it in a project but i did some tests and you may need some delay before polling
in order to wait for the configuration settings to actually change

1 Like

Hey, cool! I'll look into it. Thanks for testing too.

I'm curious if it's necessary to set the TCP configuration settings at a poll rate, or if they persist after settings once. But that's great to know, that's definitely what I was searching around for and i'll try it out.

No you just set it once for each time you want to change the Modbus configuration and then you do the polling. The only problem is that you have to wait a while for the changes and i couldnt find a way to do that on the same line in the flow.

Im sharing an old flow i had for testing. In the example it just changes the port number of the device but you can easily modify it to change the IP. The lower part of the Flow is a Flex Sequencer test doing 3 polls for a group of values.

[{"id":"9004bc09af812650","type":"modbus-flex-sequencer","z":"54efb553244c241f","name":"","sequences":[{"name":"1","unitid":"1","fc":"FC1","address":"1","quantity":"1"}],"server":"ebeab3422d31a778","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":600,"y":2180,"wires":[["a30c60aee58f6ff6"],[]]},{"id":"fffb9eb71e3cd468","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":220,"y":2180,"wires":[["5a551e73b1f86fe5"]]},{"id":"5a551e73b1f86fe5","type":"function","z":"54efb553244c241f","name":"","func":"msg.sequences = [\n    {\n        name: \"Foo\",\n        unitid: 1,\n        fc: 3,\n        address: 1,\n        quantity: 1\n    },\n    {\n        name: \"Bar\",\n        unitid: 1,\n        fc: 3,\n        address: 10,\n        quantity: 2\n    },\n    {\n        name: \"Baz\",\n        unitid: 1,\n        fc: 'FC3',\n        address: 20,\n        quantity: 3\n    }]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":2180,"wires":[["9004bc09af812650"]]},{"id":"98dd63049eec6db2","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1190,"y":2180,"wires":[]},{"id":"a30c60aee58f6ff6","type":"join","z":"54efb553244c241f","name":"","mode":"custom","build":"object","property":"responseBuffer.buffer","propertyType":"msg","key":"modbusRequest.name","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":830,"y":2180,"wires":[["1a3f3a612009c111"]]},{"id":"1a3f3a612009c111","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"responseBuffer.buffer","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":2180,"wires":[["98dd63049eec6db2"]]},{"id":"65ff3f856d30b5a3","type":"modbus-flex-connector","z":"54efb553244c241f","name":"","maxReconnectsPerMinute":4,"emptyQueue":false,"showStatusActivities":true,"showErrors":true,"server":"ebeab3422d31a778","x":630,"y":1980,"wires":[["5252edb659de6b81"]]},{"id":"9abf7f4eaac7fe36","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1940,"wires":[["aa046f1a3abde1a6"]]},{"id":"859e584e2c9c2928","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":2020,"wires":[["159a3075aad4aece"]]},{"id":"aa046f1a3abde1a6","type":"function","z":"54efb553244c241f","name":"Device port 502","func":"msg.payload = {\n    'connectorType': 'TCP',\n    'tcpHost': '192.168.0.66',\n    'tcpPort': '502',\n    'unitId': 1\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":1940,"wires":[["65ff3f856d30b5a3"]]},{"id":"159a3075aad4aece","type":"function","z":"54efb553244c241f","name":"Device port 503","func":"msg.payload = {\n    'connectorType': 'TCP',\n    'tcpHost': '192.168.0.66',\n    'tcpPort': '503',\n    'unitId': 1\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":2020,"wires":[["65ff3f856d30b5a3"]]},{"id":"e649ad6b1f2fbf70","type":"comment","z":"54efb553244c241f","name":"Change Mod. Config Settings","info":"","x":660,"y":1920,"wires":[]},{"id":"5252edb659de6b81","type":"debug","z":"54efb553244c241f","name":"debug 25","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":1980,"wires":[]},{"id":"ebeab3422d31a778","type":"modbus-client","name":"192.168.0.66","clienttype":"tcp","bufferCommands":false,"stateLogEnabled":true,"queueLogEnabled":false,"failureLogEnabled":false,"tcpHost":"192.168.0.66","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"","unit_id":0,"commandDelay":50,"clientTimeout":500,"reconnectOnTimeout":false,"reconnectTimeout":2000,"parallelUnitIdsAllowed":false}]
2 Likes

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