Send data using modbus tcp protocol from node-red to a PLC

Hello,

I would like to send data using modbus tcp protocol from node-red to a PLC.
it is possible to create a modbus TCP server on the node-red?

Yes.

node-red-contrib-modbus has a built in example...

in fact I'm new to node-red, the goal is to send data on the photo to an automaton. I couldn't do that with the example.


node-red2
can you please help me to do this

I know nothing about modbus - is it a bus that is modded?

the screenshot above is the result from this discussion. (may help others getting this answered)

What do you mean by sending it to an automation? If you mean that you want to write it to a PLC using modbus then the modbus node is the one to use. Do you understand the fundamentals of modbus?

I managed to create the modbus server but when I set the port to 502, the node-red stops and it shows me this error message.

The modbus server is the PLC. Node red is the client. Unless I misunderstand exactly what you are trying to do.

The reason for the error is that you are trying to listen on port 502 and ports below 1000 can only be listened to with root privileges. Why are you creating a server?

yes I was wrong in fact the server is the PLC and node-red is the client.
I created the server to upload data to the PLC.
on the PLC I cannot change the port 502 tcp because it is the only port that it uses port the modbus TCP.
what can i do for root privileges?

Search the forum and internet (e.g. here )- asked and answered many times.

Sorry, I still don't understand. Node red is the client, which can write to the (server) PLC using modbus. Why do you want to create a server?

You don't need root privileges to connect to the PLC with node-red as a client, only if you want node-red to be the server.

here is the idea :

Sorry, that tells me nothing about why you want node-red to be a modbus server. Also I don't see a PLC in there.

how am I going to upload the modbus data to the PLC if I am not using modbus server. the PLC is in the cloud

Perhaps I am completely misunderstanding what you are trying to do. What is the data that you are trying to upload to the PLC in the cloud? Are you attempting to write to modbus registers just like a normal PLC? If so then the PLC is the server and node red is the client.

in fact I want to upload data from a router (data consumption) I retrieve them in HTTP POST, I use Modbus because there is no other way to upload this data to the PLC

[
    {
        "id": "be09724a5e928145",
        "type": "tab",
        "label": "HTTP POST ",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "8b03ba66b044c3d7",
        "type": "exec",
        "z": "be09724a5e928145",
        "command": "/usr/bin/curl ",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "2",
        "winHide": false,
        "oldrc": false,
        "name": "Curl",
        "x": 550,
        "y": 360,
        "wires": [
            [
                "570c1643b0bde49f"
            ],
            [],
            []
        ]
    },
    {
        "id": "f454c6183b821c86",
        "type": "inject",
        "z": "be09724a5e928145",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 360,
        "wires": [
            [
                "8fa43d6b7c972b98"
            ]
        ]
    },
    {
        "id": "8fa43d6b7c972b98",
        "type": "function",
        "z": "be09724a5e928145",
        "name": "Pass Args",
        "func": "msg.payload = '-H \"Content-Type:application/x-www-form-urlencoded\" -c ./cookie.txt -X POST --data \\'{\"id\":\"1\",\"execute\":1,\"core\" :\"user\",\"function\":\"login\",\"values\":[{\"username\":\"admin\",\"password\":\"ALtQvoiXntM7Cd3FcSa38g==\"}]}\\' http://10.90.9.37/cgi'\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 360,
        "wires": [
            [
                "8b03ba66b044c3d7"
            ]
        ]
    },
    {
        "id": "570c1643b0bde49f",
        "type": "debug",
        "z": "be09724a5e928145",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 750,
        "y": 360,
        "wires": []
    },
    {
        "id": "4b8b50db5297dac2",
        "type": "exec",
        "z": "be09724a5e928145",
        "command": "/usr/bin/curl ",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "2",
        "winHide": false,
        "oldrc": false,
        "name": "Curl",
        "x": 550,
        "y": 460,
        "wires": [
            [
                "3ba7a051ff679cf5"
            ],
            [],
            []
        ]
    },
    {
        "id": "1a6e2d34cb682e97",
        "type": "inject",
        "z": "be09724a5e928145",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 460,
        "wires": [
            [
                "fb72ee2237ade2bd"
            ]
        ]
    },
    {
        "id": "fb72ee2237ade2bd",
        "type": "function",
        "z": "be09724a5e928145",
        "name": "Pass Args",
        "func": "msg.payload = '-H \"Content-Type:application/x-www-form-urlencoded\" -b ./cookie.txt -X POST --data \\'{\"id\":7,\"execute\":1,\"core\":\"yruo_status\",\"function\":\"get\",\"values\":[{\"base\":\"yruo_celluar\"}]}\\' http://10.90.9.37/cgi'\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 460,
        "wires": [
            [
                "4b8b50db5297dac2"
            ]
        ]
    },
    {
        "id": "3ba7a051ff679cf5",
        "type": "json",
        "z": "be09724a5e928145",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 690,
        "y": 460,
        "wires": [
            [
                "1949b76c1cd2a229"
            ]
        ]
    },
    {
        "id": "1949b76c1cd2a229",
        "type": "function",
        "z": "be09724a5e928145",
        "name": "",
        "func": "const Value = msg.payload.result[0].get[0].value.statistics.sim2\nmsg.payload = Value.split(\":\")[3].trim()\nmsg.payload = msg.payload.split(\" \")[0]\nmsg.payload = Number(msg.payload);\nmsg.payload = {\n    'value': msg.payload, \n    'register': 'holding', \n    'address': 1 , \n    'disableMsgOutput' : 0 \n}; \n\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 840,
        "y": 460,
        "wires": [
            [
                "1c36643f8310e384"
            ]
        ]
    },
    {
        "id": "1c36643f8310e384",
        "type": "modbus-server",
        "z": "be09724a5e928145",
        "name": "",
        "logEnabled": false,
        "hostname": "192.168.1.101",
        "serverPort": "502",
        "responseDelay": 100,
        "delayUnit": "ms",
        "coilsBufferSize": "10",
        "holdingBufferSize": "10",
        "inputBufferSize": "10",
        "discreteBufferSize": "10",
        "showErrors": false,
        "x": 1060,
        "y": 460,
        "wires": [
            [
                "7663234554a528d0"
            ],
            [],
            [],
            [],
            []
        ]
    },
    {
        "id": "7663234554a528d0",
        "type": "debug",
        "z": "be09724a5e928145",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1250,
        "y": 480,
        "wires": []
    }
]

You didn't answer the question. You say you are using modbus. Are you using, for example, modbus write register to write data? If so you need to send a modbus write register command with the data. Where is the data being written to (so where is the register). If the register is in the cloud then the cloud is the server and node red is the client, writing the data to the cloud.

yes i use modbus write register to write data

Where is the data being written to and by what? If it is being written to the PLC in the cloud by node-red then node red is the client, and you should be using the modbus write node to do that, and not configuring a modbus server in node red.

The modbus server in node red is for when you want node-red to behave like a PLC, so other clients can write to registers in node-red.

I have a lot more experience with PLCs and Modbus than with Node-Red, although I do have a Modbus TCP connection from a Node-Red device (Raspberry Pi) to a PLC/HMI so...

The older Modbus documentation, especially for PLCs, refers to systems comprising "Modbus Masters" and "Modbus Slaves". A PLC can be Master or Slave, and actually can be both at the same time. If you PLC refers to data mapped as Registers 40000-40100 for example, your PLC is acting as Modbus Slave.

A Modbus Master sends a request/command to a Slave, and the Slave responds, either returning the requested data or executing a Write command.
In line with modern definitions:
Modbus Master = Client and
Modbus Slave = Server.

The Server (Slave) is passive, creating a space comprising multiple Modbus "Registers" which can be Bits or Words (16 bit). Since Modbus is an open standard there are hundreds of variations to include double Words (32 bit) and Floats (in 32 bit).

The Clients (Masters) actively poll the Server, either to retrieve data (Registers) or to write data in the Server.

To do any and all of this in Node-Red, just install the node-red-contrib-modbus and you're ready to go. You shouldn't have to worry about permissions etc. The Host Server you specify in your Client-Nodes will almost certainly to Port 502, but that is the port the Server is listening on, NOT the port on your client.

Anything else you want to know about Modbus can be found here... https://modbus.org/
Hope this helps!

1 Like

I dont think you can send (photos) over modbus as the format of modbus is 16 / 32 bit intergers. if you want to write a register to the plc, as long as its setup in the tag list (in the plc) you just do a write with the flow and the value you want to change IE %mw0 = 6789 and it will change that register in the PLC.

Tommy