Modbus TCP - RS232 Gateway

175 views
Skip to first unread message

Scott Anderson

unread,
Nov 27, 2019, 6:19:06 AM11/27/19
to modbus-tk
I have devices that use an ASCII RS232 protocol. I am trying to build a gateway that will allow me to read and write to these devices using Modbus. I'm trying to understand how to build a robust, fault tolerant slave/server with modbus_tk. There seem to be some helper libraries like "hooks" and "utils", but I can't find any good examples or documentation that tell me how to use them or if I should use them. I'm having trouble wrapping my brain around using this library.

So, what I want to be able to do is to create a Modbus list that will allow me to read from and write to my devices. I could have more than one device connected to the gateway. It would be nice if I could make each of them a separate slave.I think I can do that with modbus_tk... I would have some process looking for writes to the Modbus registers to then write to the RS232 devices. Also, the Modbus master will be looking for updated values in the registers of the slave gateway. For instance, I want to have my gateway periodically read values from the RS232 devices and save these values to the modbus registers to be read by the master later. Also, I would like to have my master be able to send commands to the RS232 devices by writing to the gateway's modbus registers.

Another aspect is that I am dealing with 32 bit floating point values that I read from my RS232 devices. They come in as little endian encoded in hexadecimal. I've seen examples using struck.pack and unpack, I'm assuming I need to do this if I'm writing numbers that are not unsigned 16-bit integers.

I've used NModbus before in C#. For this project I really want to write Python and ultimately run the gateway on a Raspberry Pi (but I'm going to run the initial prototype on a Windows PC).

My thoughts are that I'll have a main loop that is reading and writing to the RS232 devices and querying and writing to the modbus registers. I think that when I "start" a server it automatically is running on its own thread in the background accepting requests from the master, is this correct?

Does anyone have any good sample code that they can point me to?

Luc JEAN

unread,
Dec 3, 2019, 4:30:43 AM12/3/19
to modb...@googlegroups.com
Hello Scott,

By default, modbus-tk just make possible to read/write values from memory. You can look at the system-monitor example https://github.com/ljean/modbus-tk/blob/master/examples/modbus_system_monitor.py which is doing something like this.

if you want to build a gateway and have synchronous ASCII RS232 queries to your device every tile you receive a modbus query, you probably need to look at the hooks of modbus_tk

For example : the modbus.Slave.handle_write_single_coil_request((slave, request_pdu)) will allow you to call your device on every modbus query.


from modbus_tk import modbus_tcp, hooks

def on_write_single_coil_request(data):
    slave, request_pdu= data
   # implement the ASCII RS232 things
   return response 
hooks.install_hook('modbus.Slave.handle_write_single_coil_request'on_write_single_coil_request)


For sure, more docs and examples have to be written. help is welcomed :)

I hope it helps
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/452725c3-3907-41a1-9099-be17b910182b%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages