I currently have a local modbus-TCP network on my computer, consisting of a modbus server and two modbus clients. I want it to be a network of one server and one client, but currently have not been able to write and read holding registers from my server hence the need for a redundant extra client.
My code is as follows, I have simplified it considerably for illustrative purposes
SERVER.PY
from pymodbus.server import StartTcpServer
from pymodbus.datastore import ModbusSparseDataBlock, ModbusSlaveContext, ModbusServerContext
from pymodbus.version import version
from pymodbus.device import ModbusDeviceIdentification
store = ModbusSlaveContext(hr=ModbusSparseDataBlock({0x00: [0]*0xff}))
context = ModbusServerContext(slaves=store, single=True)
identity = ModbusDeviceIdentification()
identity.VendorName = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = version.short()
StartTcpServer(context=context, identity=identity, address=("0.0.0.0", 502))
CLIENT1.PY
from pymodbus.client import ModbusTcpClient
while True:
client.write_registers(0, [power, current, energy]) # variables defined earlier
feedback = client.read_registers(4, 3, unit=1)
# operations now commence on feedback variable
CLIENT2.PY
import numpy as np
from pymodbus.client import ModbusTcpClient
while True:
client.write_registers(4, [analytics, analytics2, analytics3]) # variables defined earlier
system = client.read_registers(0, 3, unit=1)
# operations now commence on system variable
I want the CLIENT2.PY to become the new server, i.e. something like the following
NEW_SERVER.PY
from pymodbus.server import StartTcpServer
from pymodbus.datastore import ModbusSparseDataBlock, ModbusSlaveContext, ModbusServerContext
from pymodbus.version import version
from pymodbus.device import ModbusDeviceIdentification
store = ModbusSlaveContext(hr=ModbusSparseDataBlock({0x00: [0]*0xff}))
context = ModbusServerContext(slaves=store, single=True)
identity = ModbusDeviceIdentification()
identity.VendorName = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = version.short()
server = StartTcpServer(context=context, identity=identity, address=("0.0.0.0", 502))
while True:
server.write_registers(4, [analytics, analytics2, analytics3]) # variables defined earlier
system = server.read_registers(0, 3, unit=1)
# operations now commence on system variable
CLIENT1.PY should remain the same as before in this example.
The issue is that I have not yet found a way to write to holding registers directly from the server, and instead have to create a client to do so, whilst the real server just sits there doing nothing but running. Help would be much appreciated