4

I am trying to use a Raspberry Pi 3B (run Ubuntu Mate 16.04 operating system)as a Master to read values from an electric energy meter which supports Modbus-RTU protocol.

I used a RS232/USB adapter and a RS485/RS232 adapter to link the meter and the USB port on the Raspberry Pi. I have tried the modbus_tk 0.5.7 and MinimalModbus to implement the communication under Modbus-RTU protocol.

The meter reading system based on the Modbus-RTU protocol

When I use modbus_tk 0.5.7 and run the following code:

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()))

The parameters such as port, baudrate, bytesize,parity,and stopbits were set correctly, but 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

When I use MinimalModbus and run the following code:

#!/usr/bin/env python
import minimalmodbus

instrument.serial.port='/dev/ttyUSB0'          # this is the serial port name
instrument.serial.baudrate = 9600   # Baud
instrument.serial.bytesize = 8
instrument.serial.parity   = serial.PARITY_NONE
instrument.serial.stopbits = 1
instrument.serial.timeout  = 0.05   # seconds

#instrument.address     # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU   # rtu or ascii mode
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1) # port name, slave address (in decimal)
energy = instrument.read_register(10, 1) # Registernumber, number of decimals
print energy

It always returns this:

    raise IOError('No communication with the instrument (no answer)')
IOError: No communication with the instrument (no answer)

And then I use the same serial transmission line to link the meter and the laptop, and use a debugging tool running on Windows XP, which was developed by the manufacturer of the meter. The debugging tool sends the same Request (1-3-0-0-0-49-132-30) as before, but the debugging tool can get correct Responses. (Maybe it's because it ignored some incorrect Responses and keep on send Requests regularly) And it can represent that the Request message is correct and the connection of serial transmission has no problem.

I also used the CuteCom(a graphical serial terminal) and the RS232/USB adapter to confirm that the USB port can send and receive correctly. It is also useless to add a resistor between two RS485 lines

I have tried many times, but the Raspberry Pi never gets a Response, and always returns the same error information. I also try to run the same code on a Ubuntu virtual machine, it returned the same message as above and never get a Respond.

I am new to Modbus and serial communication, so any help would be appreciated.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Peter Solomon
  • 73
  • 1
  • 5

4 Answers4

2

I have solved my problem by using a more expensive USBtoRS485 connector. The problem took me a lot of time to try different library and different code. It turns out that the adapter "QinHeng Electronics HL-340 usb-serial" that I bought works well on windows but does not work with Linux. It can achieve send and receive message on Linux, but it just can`t support Modbus communication.

So I suggest you buy a more expensive connector, and it may save your a lot of time and energy.

That`s all, thank you!

Peter Solomon
  • 73
  • 1
  • 5
1

I was having the same problem. After use mbpoll tool I have realized the parity was wrong. Just updated to parity.EVEN and it is ok now.

0
  • Try connecting RS232-end of a cable into PC with serial port (if you have it ofc) to ensure usb/rs232 works
  • If you have oscilloscope (like fluke), you can observe RS485 line (it is not difficult in fact) to understand where is the issue.
  • It happened to me to find out that serial parameters (like baud rate) can be configured it 2 places for Windows - when you open the port and in driver configuration.
  • Try equalize the potential for all devices. I know rs485 is supposed to be immune to it but rs232 not
grapes
  • 8,185
  • 1
  • 19
  • 31
0

You have a too short time out on your Pi, try increasing it to 100 ms and it should work.

EDIT: As requested in the comment below I'm giving some more details on my suggestion. Based on the details given on the question and subsequent answers and comments the hardware setup seemed to be correct. I've seen this happen from time to time: when you set a very short timeout on one side the link suddenly stops working (it makes sense too, a timeout error occurs when you have not received an answer within the timespan you determine with the timeout parameter), and it goes back alive as soon as you increase it again. I've also noticed this effect is hardware dependent, some devices are able to answer faster than others

Marcos G.
  • 3,371
  • 2
  • 8
  • 16
  • Can you add some explanation to your answer such that others can learn from it? The OP already answered that he bought another cable, so what makes you think that a "too short time out" is a problem? – Nico Haase May 13 '19 at 10:29