Jump to content

How to use modbus_tk.py with unitronics


Recommended Posts

Im just a nube so it took me a while to figure it out.

on the unitronix side assign a slave to one of the ports like this


on the python side read data with something like this

import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus as modbus
import modbus_tk.modbus_tcp as modbus_tcp
import logging

logger = modbus_tk.utils.create_logger("console", level=logging.DEBUG)

master = modbus_tcp.TcpMaster(host='', port=20257, timeout_in_sec=5.0)
actual_bits = master.execute(slave=1, function_code=cst.READ_COILS, starting_address=1, quantity_of_x=2)
print("actual_bits =", actual_bits)


remember the slave number!

Hope this helps someone


Link to comment
Share on other sites

  • 3 weeks later...

some help from friends and this forum - here is the last version of the controller side

first added the close every 3 minutes - did not do the trick but helped a bit so i keep it there

init the card socket and port and set tcp keep, link lost and connection gave a stable read even when power or cable momentarily removed


conecting modbus stable version.jpg

Link to comment
Share on other sites

  • MVP 2023

Also look at SI 103 .. SI 108 which are internal timers to close socket after a period of silence.

Try disabling your timer networks and setting SI 105 to 600 to achieve the same results.  If your system is the Master use SI 109.  If you open the Help and search for "SI 105" you can navigate your way to a more thorough description.

Joe T.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...