BiancoRoyal/node-red-contrib-modbus

Modbus Flex Connector not setting unit id of modbus client

Opened this issue · 3 comments

Which node-red-contrib-modbus version are you using?

5.31.0

What happened?

I want to read modbus tcp data from different modbus devices. I created a subflow with environment variables to set the host-ip, the port, the unit id and the address to read:

image

In the "Server Config" function node the modbus client is defined and provided to the Modbus Flex Connector:

msg.payload = {
    connectorType: 'TCP',
    tcpHost: env.get("modbus_host"),
    tcpPort: env.get("modbus_port"),
    unit_id: env.get("modbus_unitid"),
};
return msg;

In the Modbus Flex Conncetor i created a server with the name "Default_Client". The server has default values:

image

In the "Define Address" function node the address to read is set and provided to the Modbus Flex Getter:

msg.payload = {
    fc: 3,
    address: env.get("modbus_address"),
    quantity: 1
};

return msg

The Server Config node is triggered once after 1 second. The Define Address node is triggered once after 1 second, then every 1 seconds.

I created a flow with two instances of the subflow. One instance for a modbus connection with unit id 1, the second for another modbus connection to a different device with unit id 10:

image

image

Whenn running the flow, I only get values from the first instance with unit id 1, which matches the default value of the "Default_Client". When changing the defualt Unit-Id of the "Default_Client" to 10, I only get values from the second device, which has unit id 10. It seems like the Modbus Flex Connector is not overwriting the default Unit-Id with the value it get's from the "Server Config" node but keeps the default value of the "Default_Client".

Server

Other/External server

How can this be reproduced?

  • Create two instances of the ReadModbus-Subflow
  • First instance with modbus_unitid = 1
  • Second instance with modbus_unitid ~= 1
  • Connect the outputs of both instances to debog nodes
  • Only the subflow with modbus_unitid = 1 delivers values
[
    {
        "id": "92094e3b8865d186",
        "type": "subflow",
        "name": "ReadModbus",
        "info": "",
        "category": "",
        "in": [],
        "out": [
            {
                "x": 1060,
                "y": 220,
                "wires": [
                    {
                        "id": "80ade53c487ebb68",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "modbus_host",
                "type": "str",
                "value": "10.119.60.1"
            },
            {
                "name": "modbus_port",
                "type": "num",
                "value": "502"
            },
            {
                "name": "modbus_unitid",
                "type": "num",
                "value": "1"
            },
            {
                "name": "modbus_address",
                "type": "num",
                "value": "1"
            }
        ],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "718ae18e4449ae5f",
        "type": "modbus-flex-connector",
        "z": "92094e3b8865d186",
        "name": "",
        "maxReconnectsPerMinute": 4,
        "emptyQueue": false,
        "showStatusActivities": true,
        "showErrors": true,
        "server": "e4e385c3f72e30b8",
        "x": 810,
        "y": 160,
        "wires": [
            []
        ]
    },
    {
        "id": "9d95b57e2e626f50",
        "type": "inject",
        "z": "92094e3b8865d186",
        "name": "Trigger Server Config",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "1",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 260,
        "y": 160,
        "wires": [
            [
                "19f30b7ff5ae4034"
            ]
        ]
    },
    {
        "id": "19f30b7ff5ae4034",
        "type": "function",
        "z": "92094e3b8865d186",
        "name": "Server Config",
        "func": "msg.payload = {\n    connectorType: 'TCP',\n    tcpHost: env.get(\"modbus_host\"),\n    tcpPort: env.get(\"modbus_port\"),\n    unit_id: env.get(\"modbus_unitid\"),\n};\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 160,
        "wires": [
            [
                "718ae18e4449ae5f"
            ]
        ]
    },
    {
        "id": "5182c2429eb8839e",
        "type": "function",
        "z": "92094e3b8865d186",
        "name": "Define Address",
        "func": "msg.payload = {\n    fc: 3,\n    address: env.get(\"modbus_address\"),\n    quantity: 1\n};\n\nreturn msg",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 220,
        "wires": [
            [
                "80ade53c487ebb68"
            ]
        ]
    },
    {
        "id": "80a25401fde32e1d",
        "type": "inject",
        "z": "92094e3b8865d186",
        "name": "Trigger Modbus Read",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": true,
        "onceDelay": "1",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 260,
        "y": 220,
        "wires": [
            [
                "5182c2429eb8839e"
            ]
        ]
    },
    {
        "id": "80ade53c487ebb68",
        "type": "modbus-flex-getter",
        "z": "92094e3b8865d186",
        "name": "",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "logIOActivities": false,
        "server": "e4e385c3f72e30b8",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 790,
        "y": 220,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "e4e385c3f72e30b8",
        "type": "modbus-client",
        "z": "92094e3b8865d186",
        "name": "Default_Client",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "127.0.0.1",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": true,
        "showErrors": false,
        "showWarnings": true,
        "showLogs": true
    },
    {
        "id": "33fbc91930097586",
        "type": "tab",
        "label": "Testflow",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "13f0e40848894d3c",
        "type": "subflow:92094e3b8865d186",
        "z": "33fbc91930097586",
        "name": "Connection1",
        "x": 390,
        "y": 180,
        "wires": [
            [
                "93424a2244468b9f"
            ]
        ]
    },
    {
        "id": "93424a2244468b9f",
        "type": "debug",
        "z": "33fbc91930097586",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 180,
        "wires": []
    },
    {
        "id": "b1a823a4f244f4c4",
        "type": "subflow:92094e3b8865d186",
        "z": "33fbc91930097586",
        "name": "Connection2",
        "env": [
            {
                "name": "modbus_host",
                "value": "10.119.20.10",
                "type": "str"
            },
            {
                "name": "modbus_unitid",
                "value": "10",
                "type": "num"
            },
            {
                "name": "modbus_address",
                "value": "1809",
                "type": "num"
            }
        ],
        "x": 390,
        "y": 240,
        "wires": [
            [
                "7a1701f7b31fd7ed"
            ]
        ]
    },
    {
        "id": "7a1701f7b31fd7ed",
        "type": "debug",
        "z": "33fbc91930097586",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 240,
        "wires": []
    }
]

What did you expect to happen?

The modbus clients in both instances are set with the right Unit-Id and both subflows deliver values to the debug nodes.

Other Information

Node-RED version: 4.0
NodeJS version: 20.15.0

it has to be msg.payload.unitId

node.unit_id is the property of the node itself later that will be set from the msg.payload.unitId if it exists in the payload and it is a number ready to be parsed as Integer

Thanks for your suggestion. The Unit-Id was misspelled in my approach, you are right. However, wheat really solved the issue for me was to ad the unitId to the payload that is sent to the Modbus-Flex-Getter node instead of the payload that is sent to the Modbus-Flex-Connector node.