AdvancedClimateSystems / uModbus

Python implementation of the Modbus protocol.
Mozilla Public License 2.0
211 stars 81 forks source link

RTU: Check CRC in exception ADU #118

Open acolomb opened 3 years ago

acolomb commented 3 years ago

The first five response ADU bytes received are checked for a valid function code, assuming an error if it is unknown. The received ADU part is then treated as an exception frame. But even those must pass the CRC validation, otherwise the supposed "wrong" function code cannot even be trusted at all.

This change adds the CRC check for the first five bytes, falling through to further processing when it fails. The overall CRC and function code is checked later in parse_response_adu(), so an invalid code will be detected again at that point.

This is mainly relevant if the responses are received partially from the serial input buffer, which will cause many CRC and parsing failures. In those cases, the reported exception will likely be a CRCError or ValueError for the whole ADU, instead of an invalid function code based on only the first 5 bytes.

Note that it works best in conjunction with #117, failing early if the response function code does not match the request.

coveralls commented 3 years ago

Coverage Status

Coverage decreased (-0.5%) to 95.726% when pulling 964d09f5bb0adb073e59a5d039f83f98c51c4853 on acolomb:check-crc-in-exception-adu into f1128a73e43f565bacedd1ae99d077d7c9c831f3 on AdvancedClimateSystems:master.

coveralls commented 3 years ago

Coverage Status

Coverage decreased (-0.5%) to 95.726% when pulling 964d09f5bb0adb073e59a5d039f83f98c51c4853 on acolomb:check-crc-in-exception-adu into f1128a73e43f565bacedd1ae99d077d7c9c831f3 on AdvancedClimateSystems:master.