pyhys / minimalmodbus

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

Modbus RTU minimalmodbus.InvalidResponseError #75

Closed Bogaerts-Gaetan closed 3 years ago

Bogaerts-Gaetan commented 3 years ago

Explication Hello everyone, I am currently in a modbus rtu communication test phase with minimalmodbus to control a Mitsubishi Heavy industries air conditioning unit via an intesis converter. When I modify the 16 bit registers, the change in the air conditioning registers is done fine but this error appears. So I would like to use minimalmodbus and understand this error. Can you help me?

thank you very much in advance code:

!/usr/bin/env python3

import minimalmodbus import serial minimalmodbus.BYTEORDER_BIG = 0 minimalmodbus.MODE_RTU = 'rtu' instrument = minimalmodbus.Instrument('COM3', 1) instrument.serial.port = 'COM3' instrument.serial.baudrate = 9600 # Baud instrument.serial.bytesize = 8 instrument.serial.parity = serial.PARITY_NONE instrument.serial.stopbits = 2 instrument.serial.timeout = 100 instrument.address = 1 instrument.mode = minimalmodbus.MODE_RTU instrument.clear_buffers_before_each_transaction = True instrument.write_register(2, 4)

temperature = instrument.read_register(2, 1)

instrument.serial.close()

error :

"C:\Program Files\Python39\python.exe" C:/Users/bogae/Desktop/test_modbus/test_com_clim_modbu/clim.py Traceback (most recent call last): File "C:\Users\bogae\Desktop\test_modbus\test_com_clim_modbu\clim.py", line 19, in instrument.write_register(2, 4) File "C:\Users\bogae\AppData\Roaming\Python\Python39\site-packages\minimalmodbus.py", line 550, in write_register self._generic_command( File "C:\Users\bogae\AppData\Roaming\Python\Python39\site-packages\minimalmodbus.py", line 1245, in _generic_command payload_from_slave = self._perform_command(functioncode, payload_to_slave) File "C:\Users\bogae\AppData\Roaming\Python\Python39\site-packages\minimalmodbus.py", line 1329, in _perform_command payload_from_slave = _extract_payload( File "C:\Users\bogae\AppData\Roaming\Python\Python39\site-packages\minimalmodbus.py", line 1867, in _extract_payload raise InvalidResponseError(text) minimalmodbus.InvalidResponseError: Checksum error in rtu mode: '\x01\xa0' instead of '8{' . The response is: '\x00\x01\x10\x00\x02\x00\x01\xa0' (plain response: '\x00\x01\x10\x00\x02\x00\x01\xa0')

j123b567 commented 3 years ago

Accroding to your code example, you have lot of difficulties to communicate with the instrument and you already try something without sucess.

minimalmodbus.BYTEORDER_BIG = 0
minimalmodbus.MODE_RTU = 'rtu'

these are completely useless and you can do more harm trying to rewrite constants.

instrument.serial.timeout = 100 means timeout 100 seconds, which is too big for common instruments instrument.clear_buffers_before_each_transaction = True it seems that the instrument is sending much longer response then expected. '\x01\xa0' seems like a continuation of some data and not a CRC. These configurations are just hiding real problem.

Did you have any success to comunicate with this instrument with other tool then minimalmodbus? Does the instrument really support all common modbus commands? I have seen some crippeled instruments supporting only a subset of commands - eg write registers but not write single register. There are also strange instruments "supporting" modbus, where you must ask with exactly only one supported number of registers and from exactly only one supported register address.

It would be interesting to see complete response with script like this

import minimalmodbus
import serial

instrument = minimalmodbus.Instrument('COM3', 1, debug=True)
instrument.serial.baudrate = 9600
instrument.serial.parity = serial.PARITY_NONE
instrument.serial.stopbits = 2

instrument.serial.timeout = 1
instrument.precalculate_read_size = False

instrument.write_register(2, 4)
#temperature = instrument.read_register(2, 1)
Bogaerts-Gaetan commented 3 years ago

user-manual-inmbsmhi001r000.pdf Thank you for your reply. Here is the error I am getting with your test code. This code modifies the value of the registers because on my control screen I have the confirmation of the changes of the air conditioning controls. But this error persists

reponse

"C:\Program Files\Python39\python.exe" C:/Users/bogae/Desktop/test_modbus/test_com_clim_modbu/test_clim.py MinimalModbus debug mode. Create serial port COM3 MinimalModbus debug mode. Will write to instrument (expecting 1000 bytes back): 01 10 00 02 00 01 02 00 04 A6 71 (11 bytes) MinimalModbus debug mode. Clearing serial buffers for port COM3 MinimalModbus debug mode. No sleep required before write. Time since previous read: 2011921.00 ms, minimum silent period: 4.01 ms. MinimalModbus debug mode. Response from instrument: 00 01 10 00 02 00 01 A0 09 00 (10 bytes), roundtrip time: 1.0 ms. Timeout for reading: 0.0 ms.

Traceback (most recent call last): File "C:\Users\bogae\Desktop\test_modbus\test_com_clim_modbu\test_clim.py", line 12, in instrument.write_register(2, 4) File "C:\Users\bogae\AppData\Roaming\Python\Python39\site-packages\minimalmodbus.py", line 550, in write_register self._generic_command( File "C:\Users\bogae\AppData\Roaming\Python\Python39\site-packages\minimalmodbus.py", line 1245, in _generic_command payload_from_slave = self._perform_command(functioncode, payload_to_slave) File "C:\Users\bogae\AppData\Roaming\Python\Python39\site-packages\minimalmodbus.py", line 1329, in _perform_command payload_from_slave = _extract_payload( File "C:\Users\bogae\AppData\Roaming\Python\Python39\site-packages\minimalmodbus.py", line 1867, in _extract_payload raise InvalidResponseError(text) minimalmodbus.InvalidResponseError: Checksum error in rtu mode: '\t\x00' instead of 'R\x0b' . The response is: '\x00\x01\x10\x00\x02\x00\x01\xa0\t\x00' (plain response: '\x00\x01\x10\x00\x02\x00\x01\xa0\t\x00')

Process finished with exit code 1

This is the first time that I try to establish a communication with the air conditioning thanks to minimalmodbus. For a test, I tried to read a register with the read function and I received a response of 9 bytes compared to 10 when I write. I am enclosing the documentation of the device I want to control to give you all the information I have. thanks a lot for your help

j123b567 commented 3 years ago

Correct response to your command (write multiple registers) should be 01 10 00 02 00 01 A0 09 but response of your device has additional 00 at the begining and at the end of the message. But the rest is correct.

So it seems you have problems with DE on your RS485 port from the PC. This is common problem of some ultra cheep USB/RS485 converters, so please try some better RS485 on your PC.

Bogaerts-Gaetan commented 3 years ago

I have tested through another better quality converter and everything works fine. Thank you very much for your time and help