I have an issue with a simple pymodbus server implementation. From what I have read in the docs, this implementation should have unique slave contexts for each slave device, i.e. writing to device 0x01, register address 1, should be a different register from device 0x02, register 1.
In my case, writing to register 1 writes to register 1 for ALL slave addresses. Could someone go over my server code to see if I am missing something, or perhaps clarify if my understanding of how the pymodbus server is supposed to work with the single flag set to False.
Cheers. Code here:
#!/usr/bin/env python3
"""
Pymodbus Synchronous Server
--------------------------------------------------------------------------
This synced server is implemented using TCP, with multiple slave contexts
"""
# --------------------------------------------------------------------------- #
# import the various server implementations
# --------------------------------------------------------------------------- #
from pymodbus.server.sync import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
def run_server():
slaves = {
0x01: ModbusSlaveContext(),
0x02: ModbusSlaveContext()
}
context = ModbusServerContext(slaves=slaves, single=False)
# ----------------------------------------------------------------------- #
# 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 = '2.3.0'
# ----------------------------------------------------------------------- #
# run the server
# ----------------------------------------------------------------------- #
StartTcpServer(context, identity=identity, address=("0.0.0.0", 5020))
if __name__ == "__main__":
run_server()