0

I'm trying to make my Raspberry Pi 3B+ as ModbusTCP server and for testing purposes my PC as a client. I'm using pyModbusTCP module for this.

My server script (RPi) is:

from pyModbusTCP.server import ModbusServer, DataBank
from time import sleep
from random import uniform

server = ModbusServer("localhost", 502, no_block=True)

try:
    print("Start server...")
    server.start()
    print("Server is online...")
    while True:
        DataBank.set_words(51, [int(uniform(0, 100))])
        sleep(1)
except:
    print("Shutdown server...")
    server.stop()
    print("Server is offline...")

Client side script (PC) is:

from pyModbusTCP.client import ModbusClient

client = ModbusClient(host="192.168.1.122", port=502)
client.open()

192.168.1.122 - is my Raspberry Pi (Server) IP address.

As you can see - I put localhost as IP address in the server script. I tried 127.0.0.1 as well. However Clilent were not able to connect to the server and returned False after 30 seconds of waiting.

If I specify server's IP address (192.168.1.144 in my case) - server script imediately go to "Sever is offline..." part.

I suspect that I was wrong with that IP address.

Any suggestion?

2
  • 1
    localhost or 127.0.0.1 (same thing) are only accessible form the local host, i.e .the pi itself in this case - so using either of those two values is definitely not going to work - you need to specify the IP address of the interface you want to listen on ... 192.168.1.122 in your case - if that's a DHCP assigned address you can't rely on that - many networking type things allow you to use 0.0.0.0 to specify ALL interfaces - that may be the ideal solution Jun 30, 2020 at 22:05
  • @JaromandaX that was completely true! I put 0.0.0.0 into server's side (RPi) and 192.168.1.144 (RPi's IP address) into clients side (on a PC). And everything worked! Could you post this as a answer? I will accept this. Jun 30, 2020 at 22:25

2 Answers 2

1

localhost or 127.0.0.1 (same thing) are only accessible form the local host, i.e .the pi itself in this case - so using either of those two values is definitely not going to work

you need to specify the IP address of the interface you want to listen on ... 192.168.1.122 in your case -

However, if that's a DHCP assigned address you can't rely on that

Many networking type things allow you to use 0.0.0.0 to specify ALL interfaces - that may be the ideal solution

4
  • What if I have several slaves in the system? Your solution pretends that client points to the master. Could you comment on this? Jun 30, 2020 at 22:30
  • Your solution pretends that client points to the master ... I'm not pretending anything ... if you have several slaves (clients?), they can all connect to the master (server?) Jun 30, 2020 at 23:58
  • You're right - saying "slaves" I meant "clients". In case of Modbus RTU master (server?) addresses to a slave (client?). Now in case of Modbus TCP I see that client addresses to the master. So for me it looks like wrong way round. Like Client aquals to Maser and Server equals to Slave. Could you explain this for me? Jul 1, 2020 at 6:01
  • No, because I have no knowledge of Modbus TCP Jul 1, 2020 at 6:22
0

In my case port value needed to be equal to or more than 1024. From the Modbus Specifications and Implementations in some cases, you need a port higher than 1024.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.