I am setting up a Modbus TCP server on my Linux PC. When i run the code below the port 502 is not open on my computer, when i check with nmap.
Anyone have experience with this?
https://pymodbus.readthedocs.io/en/latest/examples/synchronous-server.html
from pymodbus.server.async import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [17]*100),
co = ModbusSequentialDataBlock(0, [17]*100),
hr = ModbusSequentialDataBlock(0, [17]*100),
ir = ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)
#---------------------------------------------------------------------------#
# initialize the server information
#---------------------------------------------------------------------------#
# If you don't set this or any fields, they are defaulted to empty strings.
#---------------------------------------------------------------------------#
identity = ModbusDeviceIdentification()
identity.VendorName = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/riptideio/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '1.0'
#---------------------------------------------------------------------------#
# run the server you want
#---------------------------------------------------------------------------#
# Tcp:
StartTcpServer(context, identity=identity, address=('localhost', 502))
Edit: If i connect to the server with a client on the same computer, the server is working. If i close the server the client responds with port 502 closed.
The 'localhost'
string you give as the server's listening address in this call:
StartTcpServer(context, identity=identity, address=('localhost', 502))
tells your server to listen for connections only from clients running on the same system as the server. If you want your server to accept connections from other systems then pass an empty string ''
as the address instead of 'localhost'
. The empty string tells the server to listen on all of this system's interfaces.