Using a hook to trap holding registers write on a slave

132 views
Skip to first unread message

Jean-Simon LaRochelle

unread,
Mar 20, 2020, 1:14:05 PM3/20/20
to modbus-tk
I have to write a proxy for a Modbus device to remap the I/O to another device. One thing I would like to do more specifically is trap the holding registers write request on a slave to remap the I/O and use a master to write the remapped i/o to another device.
How could I modify the modbus slave example to do this ?

Jean-Simon LaRochelle

unread,
Mar 24, 2020, 2:51:43 PM3/24/20
to modbus-tk
Ok I found the information I needed by looking at the master example that was using some hooks.
Looking at the comments in the hooks class after a few test I was able tofind the recipe I needed.
For my tests I modified the tcpslave_example like this:

    def on_write_request(data):
        slave, pdu = data
        print(f"PDU={pdu}")
       

    try:
        #Create the server
        server = modbus_tcp.TcpServer()
        logger.info("running...")
        logger.info("enter 'quit' for closing the server")


        slave_1 = server.add_slave(1)
        slave_1.add_block('0', cst.HOLDING_REGISTERS, 0, 100)
       
        hooks.install_hook("modbus.Slave.handle_write_multiple_registers_request", on_write_request)
        hooks.install_hook("modbus.Slave.handle_write_single_register_request", on_write_request)


Bingo!

Luc JEAN

unread,
Mar 26, 2020, 4:17:46 AM3/26/20
to modb...@googlegroups.com
Hello Jean-Simon,

Well done and thanks for sharing.

Best
luc

--
You received this message because you are subscribed to the Google Groups "modbus-tk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modbus-tk+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modbus-tk/3f026c0b-f8d7-4ef0-bfcf-4746b81a833b%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages