Hi,
I'm trying to control a micro-wave generator using Modbus protocol. I used this code down below from pymodbus and i get the device connected. Now i need to read and write some spécific single Coil and register. But i tried all the examples from de the documentation but there is always an errors. Can u please help me resolve the issue with reading and writing in server context.
Thank you in advance,
Here the code for connection :
#---------------------------------------------------------------------------------------------------
import logging
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.server.sync import ModbusSerialServer
from pymodbus.transaction import ModbusRtuFramer
from pymodbus.pdu import ModbusRequest
from pymodbus.bit_read_message import ModbusResponse
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
# Initialise slave and its components
context = ModbusServerContext(slaves={
unitId: ModbusSlaveContext(
di=ModbusSequentialDataBlock(0x00, [False]*0x10),
co=ModbusSequentialDataBlock(0x00, [False]*0x10),
hr=ModbusSequentialDataBlock(0x00, [0]*0x10),
ir=ModbusSequentialDataBlock(0x00, [0]*0x10),
zero_mode=True
) for unitId in [0x01, 0x02]
}, single=False)
slave = ModbusSerialServer(
context,
ModbusRtuFramer,
ignore_missing_slaves=True,
port='COM3',
baudrate=9600,
timeout=0.5
)
#----------------------------------------------------------------------------------
the device get connected

And when i add the read/ write coil fonction, here is the code and the error:
log.debug("Write to a Coil and read back")
rq = client.write_coil(7, True, unit=UNIT)
rr = client.read_coils(7, 1, unit=UNIT)
assert (not rq.isError()) # test that we are not an error
assert (rr.bits[0] == True) # test the expected value
