Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modbus Slave don`t respond

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.

like image 627
Peter Solomon Avatar asked Sep 05 '25 03:09

Peter Solomon


2 Answers

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!

like image 157
Peter Solomon Avatar answered Sep 07 '25 16:09

Peter Solomon


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.

like image 39
John Edward Avatar answered Sep 07 '25 15:09

John Edward