Search code examples
pythonmodbusminimalmodbus

minimalmodbus checksum error in rtu mode with 2 devices


I have a RPI with a USB CH340 dongle connected to a EM340 energy meter. It works fine with the code below.

When I connect 2 x EM340 energy meters I get the following error:

pi@raspberrypi:~ $ python3 modbus_test.py
Traceback (most recent call last):
  File "modbus_test.py", line 21, in <module>
    freq2 = instrument.read_register(0x0033,1)  # Registernumber, number of decimals
  File "/home/pi/.local/lib/python3.7/site-packages/minimalmodbus.py", line 486, in read_register
    payloadformat=_Payloadformat.REGISTER,
  File "/home/pi/.local/lib/python3.7/site-packages/minimalmodbus.py", line 1245, in _generic_command
    payload_from_slave = self._perform_command(functioncode, payload_to_slave)
  File "/home/pi/.local/lib/python3.7/site-packages/minimalmodbus.py", line 1330, in _perform_command
    response, self.address, self.mode, functioncode
  File "/home/pi/.local/lib/python3.7/site-packages/minimalmodbus.py", line 1867, in _extract_payload
    raise InvalidResponseError(text)
minimalmodbus.InvalidResponseError: Checksum error in rtu mode: '6ý' instead of '\x99ò' . The response is: '\x01\x01\x00\x00\x166ý' (plain response: '\x01\x01\x00\x00\x166ý')

My code:

import minimalmodbus
import serial

instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1)  # port name, slave address (in decimal)

#instrument.serial.port                     # 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.2          # seconds

instrument.address = 1                         # this is the slave address number
#instrument.mode = minimalmodbus.MODE_ASCII   # rtu or ascii mode
instrument.mode = minimalmodbus.MODE_RTU
instrument.clear_buffers_before_each_transaction = True
instrument.close_port_after_each_call = True


## Read temperature (PV = ProcessValue) ##
freq2 = instrument.read_register(0x0033,1)  # Registernumber, number of decimals
txt = "Frekvens: {} Hz".format(freq2)

#0x0034
kwh_total  = instrument.read_register(0x0400)
kwh_total_2  = instrument.read_register(0x0402)
txt2 = "Total forbrug: {0}.{1} kWh".format(kwh_total,kwh_total_2)

w_l1 = instrument.read_register(0x0012,1)  # Registernumber, number of decimals
txt3 = "Nuværende forbrug: {} W".format(w_l1)

print(txt)
print(txt2)
print(txt3)

I have wired the 2xEM340 according to the https://web.evishine.dk/wp-content/uploads/2019/11/EM340-ENG.pdf page 3.

Any idea why I get Checksum error in rtu mode ?


Solution

  • As per the comments if you attach two Modbus RTU devices with the same ID in parallel then they will both respond to any request addressed to that Slave ID. The responses will probably collide which means your code will receive a garbled response (detected via the CRC).

    The solution is to change the ID of one of the devices.