Search code examples
pythonservermodbuspymodbus

Python Modbus Server using pymodbus module


I want to create a Modbus server (with IP address: 152.168.96.11 - same as the system) and the Modbus client running in a different system (IP address: 152.168.96.32). My client application is running successfully and I am creating the Modbus server application with the pymodbus server application. Data exchange of 32 bits (either read or write for testing purpose). I want to read and write the values of specific address to Modbus client.

How do I configure python pymodbus server with server able to read and write data into the client IP address

Here is the pymodbus server application -

# --------------------------------------------------------------------------- # 
# 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

# --------------------------------------------------------------------------- # 
# configure the service logging
# --------------------------------------------------------------------------- # 
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)


def run_server():

    # ----------------------------------------------------------------------- # 
    # initialize your data store
    # ----------------------------------------------------------------------- #
    block = ModbusSequentialDataBlock(0, [888]*32)
    store = ModbusSlaveContext(hr=block)

    slaves  = {
               0x01: store,
              }
    context = ModbusServerContext(slaves=slaves, 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=('0.0.0.0', 255))


if __name__ == "__main__":
    run_server()

Solution

  • Refer the issue raised in the Github and follow up from the contributor.