I use the Raspberry Pi 3B(run Ubuntu Mate 16.04 operating system) with a USBtoRS232 connector and a RS232toRS485 converter to fetch realtime data on a electric energy meter which supports Modbus-RTU protocol.
I have tried to use luc's example script as below:
import sys import serial
#add logging capability import logging import modbus_tk import modbus_tk.defines as cst import modbus_tk.modbus_rtu as modbus_rtu
logger = modbus_tk.utils.create_logger("console") if __name__ == "__main__":
try:
#Connect to the slave
master = modbus_rtu.RtuMaster(serial.Serial(port="/dev/ttyUSB0", baudrate=9600, bytesize=8, parity='N', stopbits=1, xonxoff=0))
master.set_timeout(5.0) #Change the timeout value/Defines a timeout on the MAC layer
master.set_verbose(True) #print some more log prints for debug purpose
logger.info("connected")
logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 49))
except modbus_tk.modbus.ModbusError, e:
logger.error("%s- Code=%d" % (e, e.get_exception_code()))
But the Raspberry Pi never gets a response from the meter.It always returns this:
2017-08-10 19:24:34,282 INFO modbus_rtu.__init__ MainThread RtuMaster /dev/ttyUSB0 is opened
2017-08-10 19:24:34,283 INFO rtumaster_example.<module> MainThread connected
2017-08-10 19:24:34,284 DEBUG modbus.execute MainThread -> 1-3-0-0-0-49-132-30
2017-08-10 19:24:39,291 DEBUG modbus.execute MainThread <-
Traceback (most recent call last):
File "rtumaster_example.py", line 34, in <module>
logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 49))
File "build/bdist.linux-x86_64/egg/modbus_tk/utils.py", line 39, in new
modbus_tk.exceptions.ModbusInvalidResponseError: Response length is invalid 0
I am sure that the parameters such as port, baudrate, bytesize,parity,and stopbits were set correctly, and the serial transmission line runs fine with a Windows laptop using a Windows based debugging tool, which was developed by the manufacturer of the meter.

The debugging tool send the same Request (1-3-0-0-0-49-132-30)as before, but the debugging tool can get correct Respondes. (Maybe it's because it ignored some incorrect Respondes and keep on send Requests regularly) And it represents that the Request message is correct and the connection of serial transmission has no problem.
I hope to know if it is possible to ignore the exceptions and keep on send Requests regularly.
I am new to Modbus and serial communication, so any help would be appreciated.