I am trying to use modbus-tk
to serially communicate with a device via Modbus RTU over a RS-485 network.
To understand how to use modbus-tk
, I am trying to query:
- Input register:
Year of Manufacture
- Register address:
0x7543 = 30019
- Device:
1
- Data type: 16-bit integer
- Value =
2019
(verified using a free master modbus simulator).
Here is the code I use, based primarily on rtumaster_example.py. Input:
#!/usr/bin/env python3
import time
from collections import namedtuple
from logging import Logger
from serial import Serial
from modbus_tk import modbus_rtu
import modbus_tk.defines as cst # cst = constants
from modbus_tk.utils import create_logger
PORT = "COM3"
SLAVE_NUM = 1
MODBUS_MASTER_TIMEOUT_SEC = 5.0
ModbusReg = namedtuple("ModbusInputRegister", ["name", "block_type", "address"])
year_of_manuf = ModbusReg(
"year of manufacture", cst.HOLDING_REGISTERS, 18
) # 0x7543 = 30019
logger = create_logger(name="console") # type: Logger
serial_ = Serial(PORT)
modbus_master = modbus_rtu.RtuMaster(serial_)
modbus_master.set_timeout(MODBUS_MASTER_TIMEOUT_SEC)
modbus_master.set_verbose(True)
time.sleep(2) # Per https://github.com/ljean/modbus-tk/issues/73#issuecomment-284800980
logger.info(
modbus_master.execute(
slave=SLAVE_NUM,
function_code=cst.READ_INPUT_REGISTERS,
starting_address=year_of_manuf.address,
)
)
Output:
2020-01-21 10:38:09,031 INFO modbus_rtu.__init__ MainThread RtuMaster COM3 is opened
2020-01-21 10:38:11,048 DEBUG modbus.execute MainThread -> 1-4-0-18-0-0-80-15
2020-01-21 10:38:11,077 DEBUG modbus.execute MainThread <- 1-132-3-3-1
---------------------------------------------------------------------------
ModbusError Traceback (most recent call last)
<ipython-input-2-9afaebcf3a35> in <module>
7 slave=SLAVE_NUM,
8 function_code=cst.READ_INPUT_REGISTERS,
----> 9 starting_address=year_of_manuf.address,
10 )
c:\path\to\venv\lib\site-packages\modbus_tk\utils.py in new(*args, **kwargs)
37 ret = fcn(*args, **kwargs)
38 except Exception as excpt:
---> 39 raise excpt
40 finally:
41 if threadsafe:
c:\path\to\venv\lib\site-packages\modbus_tk\utils.py in new(*args, **kwargs)
35 lock.acquire()
36 try:
---> 37 ret = fcn(*args, **kwargs)
38 except Exception as excpt:
39 raise excpt
c:\path\to\venv\lib\site-packages\modbus_tk\modbus.py in execute(self, slave, function_code, starting_address, quantity_of_x, output_value, data_format, expected_length)
312 # the slave has returned an error
313 exception_code = byte_2
--> 314 raise ModbusError(exception_code)
315 else:
316 if is_read_function:
ModbusError: Modbus Error: Exception code = 3
It looks like this exception is for if the slave has returned an error
What do you think I am doing wrong? I am new to this library.
What I Have Read
- Can't connect to slave with Python's modbus_tk, I am using
pyserial >= 3.1
- And every other question under the
modbus-tk
tag, some posts in the Google Groups, and the repo's examples +README.md
Device Specifics
- Device: SST Sensing's OXY-LC-485
- Modbus RTU, 9600/8-N-1
- User Guide (section 7.1.2.1 contains set of input registers)
- Device is plugged into Windows machine that I run this Python script
Packages
I am using Python 3.6 on Windows 10.
pyserial==3.4
modbus-tk==1.1.0
**EDIT 1**
Per @Brits comment, I updated my registry addresses to be correct function codes and data frame addresses.
**EDIT 2**
Updated question since I am getting a different error after more correct library usage.
ModbusInvalidResponseError
, any other ideas? – Intrastellar ExplorerModbusError
. See the traceback in question for more details – Intrastellar Explorerlogger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 18, 1))
(modified from the example code). I suspect an issue might be that the last param, quantity_of_x, defaults to 0. This param becomes the 'Quantity of Registers' in the modbus request packet; 0 is invalid for that (a request to get no registers). – Britsquantity_of_x
parameter should not be left as the default value of0
. I wonder why a default was specified in the library, if it won't ever work. – Intrastellar Explorer