tiagocoutinho / async_modbus

Async python modbus client library
Other
9 stars 7 forks source link

KeyError decoding PDU #4

Closed kellerza closed 2 years ago

kellerza commented 2 years ago

Description

A user reported the following exception when reading a USR-W630 Wifi to RS485 converter (connected via tcp://)

I have no way to simulate these unreliable conditions.

This might be more related to umodbus than async_modbus?

Traceback (most recent call last):
  File "/usr/src/app/./run.py", line 155, in read
    await SUNSYNK.read(sensors)
  File "/usr/src/app/usunsynk.py", line 32, in read
    r_r = await self.client.read_holding_registers(
  File "/usr/src/app/async_modbus.py", line 180, in read_holding_registers
    return await self._send_message(request)
  File "/usr/src/app/async_modbus.py", line 141, in _send_message
    return await self.protocol._async_send_message(
  File "/usr/src/app/async_modbus.py", line 42, in send_message_tcp
    tcp.raise_for_exception_adu(response_error_adu)
  File "/usr/local/lib/python3.9/site-packages/umodbus/client/tcp.py", line 247, in raise_for_exception_adu
    pdu_to_function_code_or_raise_error(resp_pdu)
  File "/usr/local/lib/python3.9/site-packages/umodbus/functions.py", line 118, in pdu_to_function_code_or_raise_error
    raise error_code_to_exception_map[error_code]
KeyError: 0
tiagocoutinho commented 2 years ago

Looks like the modbus package is arriving with an error code 0 which is unspecified in the modbus protocol (seems only 1-11 are valid error codes)

kellerza commented 2 years ago

The USR-W630 seems to misbehave when reading an undefined register. Reading it through mbusb/serial:// handles the error correctly.

Not sure this can be fixed, the only minor improvement could be to raise a Unknown error code error.... but then one could also catch the KeyError, so not sure it adds that much value

Going to close it for now, thanks