pyhys / minimalmodbus

Easy-to-use Modbus RTU and Modbus ASCII implementation for Python.
Apache License 2.0
308 stars 145 forks source link

Response leading 0x00 thrown crc error #61

Closed buffedelic closed 3 years ago

buffedelic commented 3 years ago

Hi

Running into a problem during testing against a HVAC device from Heru.

import minimalmodbus
import serial

instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 4)
instrument.serial.baudrate = 19200
instrument.serial.bytesize = 8
instrument.serial.stopbits = 1
instrument.serial.parity = serial.PARITY_NONE
instrument.serial.timeout = 0.5
instrument.serial.dsrdtr = False
instrument.serial.rtscts = False
instrument.debug = True
#instrument.precalculate_read_size = False
instrument.handle_local_echo = False
instrument.mode = minimalmodbus.MODE_RTU
print(instrument.read_register(289, 1))

As you can see below the respone har a leading 0x00 witch I suspect is throwing everything off.

pi@heruclient:~ $ python rstest.py 
MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): '\x04\x03\x01!\x00\x01\xd5\xa9' (04 03 01 21 00 01 D5 A9)
MinimalModbus debug mode. Clearing serial buffers for port /dev/ttyUSB0
MinimalModbus debug mode. No sleep required before write. Time since previous read: 1607462799595.63 ms, minimum silent period: 2.01 ms.
MinimalModbus debug mode. Response from instrument: '\x00\x04\x83\x02\xd0\xf0\x00' (00 04 83 02 D0 F0 00) (7 bytes), roundtrip time: 30.1 ms. Timeout for reading: 500.0 ms.

Traceback (most recent call last):
  File "rstest.py", line 16, in <module>
    print(instrument.read_register(289, 1))
  File "/home/pi/.local/lib/python2.7/site-packages/minimalmodbus.py", line 447, in read_register
    payloadformat=_PAYLOADFORMAT_REGISTER,
  File "/home/pi/.local/lib/python2.7/site-packages/minimalmodbus.py", line 1170, in _generic_command
    payload_from_slave = self._perform_command(functioncode, payload_to_slave)
  File "/home/pi/.local/lib/python2.7/site-packages/minimalmodbus.py", line 1244, in _perform_command
    response, self.address, self.mode, functioncode
  File "/home/pi/.local/lib/python2.7/site-packages/minimalmodbus.py", line 1756, in _extract_payload
    raise InvalidResponseError(text)
minimalmodbus.InvalidResponseError: Checksum error in rtu mode: '\xf0\x00' instead of '\xd4$' . The response is: '\x00\x04\x83\x02\xd0\xf0\x00' (plain response: '\x00\x04\x83\x02\xd0\xf0\x00')

Thing is that I had a device reading the HVAC unit 6 years ago with minimal modbus and I recall having to modify the code for it to work. Since then the device is dead and also the memory of how to deal with the leading byte of terror :) Any help is much appriciated!

EDIT: I even bragged about fixing it but not sharing how i did! https://forum.m.nu/viewtopic.php?f=26&t=3545&start=15#p24749 what a dumbass :facepalm:

buffedelic commented 3 years ago

So I figured it out, not quite the best approach. I have terminated my transmission line to spec and have a, probably, good usb-rs485 adapter. But to me it seems like a hardware problem, in what end I am not shure.. Link to datasheet

I am closing this due to the reason it is not a problem with minimalmodbus. This is confirmed with other, not so great, modules out there :)

# Read response
        answer = self.serial.read(number_of_bytes_to_read)
        answer = answer[1:-1] #Strip leading and trailing byte
        _LATEST_READ_TIMES[self.serial.port] = time.time()