Can't connect to Modbus TCP server (that is running 100%)

I have a running Modbus TCP server on a PLC with these parameters:

  • IP:
  • Port: 502
  • Device address: 1
  • Holding register on address 1 with heartbeat variable

I know for sure that the Modbus TCP server is running because I have tried connecting to it via Python script sucesfully:

from pyModbusTCP.client import ModbusClient

host = ''
port = 502

client = ModbusClient(host, port, unit_id = 1)
hr = client.read_holding_registers(0,10)


[310, 0, 0, 0, 0, 0, 0, 0, 0, 0]

My Node-red gets stuck in registered state always:

When I force it to connect it sends out this messeges:

Any suggestions guys? I don't know what to try at this point.

This is my flow I am using on my Node-Red:
[{"id":"683bce4b.da7ed","type":"modbus-read","z":"f0f61dbf.5d964","name":"Modbus TCP","topic":"","showStatusActivities":true,"logIOActivities":false,"showErrors":true,"unitid":"1","dataType":"HoldingRegister","adr":"0","quantity":"2","rate":"1","rateUnit":"s","delayOnStart":true,"startDelayTime":"1","server":"610770cd.6c00c","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":true,"x":330,"y":700,"wires":[["6361d552.0f5f2c"],["1fed322a.4e6bce"]]},{"id":"6361d552.0f5f2c","type":"debug","z":"f0f61dbf.5d964","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":680,"wires":[]},{"id":"1fed322a.4e6bce","type":"debug","z":"f0f61dbf.5d964","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":720,"wires":[]},{"id":"610770cd.6c00c","type":"modbus-client","name":"mark220LX - TCP","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"tcpHost":"","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}]

Have you checked the issues on the github repository for similar issues?

Is the node-red server on the same network (e.g. can the machine running node-red ping the PLC)?

Have you tried other modbus nodes?

1 Like

I can't go to the first link you posted unfortunately :c

Yes I have tried other Nodes - all fail. I have even tried to import modbus-stream third party library and it also fails (to my surprise with no error messages!) ...

Have you also tried with Modbus Flex Getter instead of Modbus Read node ?

[{"id":"1fed322a.4e6bce","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":810,"y":1640,"wires":[]},{"id":"c564e90ec1c46867","type":"modbus-flex-getter","z":"54efb553244c241f","name":"","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"610770cd.6c00c","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":600,"y":1660,"wires":[["1fed322a.4e6bce"],[]]},{"id":"cfda606228934e05","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":1660,"wires":[["63ba7d9945104484"]]},{"id":"63ba7d9945104484","type":"function","z":"54efb553244c241f","name":"","func":"msg.payload = {\n    value: msg.payload,\n    'fc': 3,\n    'unitid': 1,\n    'address': 0,\n    'quantity': 2\n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":1660,"wires":[["c564e90ec1c46867"]]},{"id":"610770cd.6c00c","type":"modbus-client","name":"mark220LX - TCP","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"","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":"30","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]
1 Like

I will try it tomorrow right away!

I have done some further testing and have strange results:

  • when I run NodeRed on my Windows 11 notebook I can connect with the same flow without any problem to the PLC.
  • but when I run the code on my Raspberry PI I can't. I have even checked the versions and they are the same.

I will try tomorrow your suggestions + logging to see what's really going under the hood.

@Sulomus Did you answer that question?

The devices can see each other and are on the same network:

So that suggests a problem with the PI.

What versions of NODE and Node-RED run on both these devices?

Using Modbus Flex Getter from your code results in same error:


The device that is trying to connect to the Modbus Server:

25 Feb 09:33:44 - [info] Node-RED version: v2.1.4
25 Feb 09:33:44 - [info] Node.js  version: v12.22.8

The device serving the Modbus Server is a PLC that is not running NodeRed (so I can't provide any info regarding NODE and NodeRed version).

No, when i said "So that suggests a problem with the PI." I meant compared to the Windows 11 PC that does connect

So again, What versions of NODE and Node-RED run on both these devices (the PI and the Win 11 machine)

Windows 11:

25 Feb 10:41:59 - [info] Node-RED version: v2.2.0
25 Feb 10:41:59 - [info] Node.js  version: v16.14.0

Raspberry PI:

25 Feb 09:33:44 - [info] Node-RED version: v2.1.4
25 Feb 09:33:44 - [info] Node.js  version: v12.22.8

I know what you're suggesting - get the same versions of SW. I have tried that with bash script for updating on Raspberry PI.

When I updated the NodeRed the same result occurred :confused:

Did you upgrade node red and nodejs? Why did you go back to the old version again?

All I can say it is that you have it working on one computer but not on the PI

It stands to reason there is an issue with the pi (leaving aside the architecture and OS differences)

I know there are plenty running modbus on PI so that leaves me one conclusion - that there is an issue with the PI (be that FW/network or borked node/nodejs)

Do you have a spare SD card to build a new clean image?

Have you made changes to the firewall settings on the pi? Perhaps you have blocked the port.

I have checked now (today the link is working, yesterday it wasn't idk what changed)

I have found this issue describing that this can occur because "it needs some time in the physical world to connect":

So I have set a delay on start:

Now everything is working ... !!! Such a simple think that didn't occur to me for the whole day.
Thank you ALL for the help and sorry for wasting your time ...

I am still curiouse why this doesn't need to be setup on my Win11 machine but it has to be on Raspberry PI ... :thinking:

1 Like

Not wasted at all, we have learnt from this and may remember to make the suggestion when another has the same problem.

1 Like

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