Lucas
Lucas

Reputation: 51

How do I fix the checksum error in modbus

I have a script which should collect the concentration data from a sensor connected with MODBUS. When i run the script, this error occures:

Traceback (most recent call last): File "C:\Users\AquaBattery.HP-LAPTOP\Desktop\H2-reading.py", line 33, in val = H2.read_long(ADRH2,functioncode=4,byteorder=0) File "C:\Users\AquaBattery.HP-LAPTOP\Desktop\minimalmodbus.py", line 560, in read_long payloadformat=_PAYLOADFORMAT_LONG, File "C:\Users\AquaBattery.HP-LAPTOP\Desktop\minimalmodbus.py", line 1170, in _generic_command payload_from_slave = self._perform_command(functioncode, payload_to_slave) File "C:\Users\AquaBattery.HP-LAPTOP\Desktop\minimalmodbus.py", line 1244, in _perform_command response, self.address, self.mode, functioncode File "C:\Users\AquaBattery.HP-LAPTOP\Desktop\minimalmodbus.py", line 1756, in _extract_payload raise InvalidResponseError(text) minimalmodbus.InvalidResponseError: Checksum error in rtu mode: '\x00\x00' instead of 'c\x85' . The response is: '\x01\x04\x0e\x00\x00\x00\x00\x00\x00' (plain response: '\x01\x04\x0e\x00\x00\x00\x00\x00\x00')

This is my code:

import time
import os
import serial
import minimalmodbus



delay = 1
pdata = 1


#-- H2 sensor read out  -----------------------------------
#def getH2():
res = 0      
ADRH1 = 1 # read out register for H2
ADRH2 = 0 # read out register for H2

try:
        H2 = minimalmodbus.Instrument('COM6',ADRH1)  # open serial port
        H2.serial.port          # this is the serial port name
        H2.serial.baudrate = 9600   # Baud
        H2.serial.bytesize = 8
        H2.serial.parity   = serial.PARITY_NONE
        H2.serial.stopbits = 1
        H2.serial.timeout  = 0.25   # seconds


        val = H2.read_long(ADRH2,functioncode=4,byteorder=0)
        print("test")
        res = float(val)/10
        print(val)
        print("---")
        print(res)

except ValueError:
        print("Failed to read H2")

How do I change the request to the sensor or the response from the sensor so that the checksum of the two data streams are the same?

Sincerely, Lucas

Upvotes: 1

Views: 6363

Answers (1)

Lucas
Lucas

Reputation: 51

I figured it out!

I needed to use read_registers instead of read_register, as the sensor returned 7 registers at a time.

Upvotes: 4

Related Questions