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?
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 use0.0.0.0
to specify ALL interfaces - that may be the ideal solution