Skip to content

Handle 5-byte Modbus exception frames without false “malformed packet” errors in state 7#18

Draft
Copilot wants to merge 6 commits into
mainfrom
copilot/fix-invalid-crc-error
Draft

Handle 5-byte Modbus exception frames without false “malformed packet” errors in state 7#18
Copilot wants to merge 6 commits into
mainfrom
copilot/fix-invalid-crc-error

Conversation

Copy link
Copy Markdown

Copilot AI commented May 19, 2026

A single-tower LVS setup can return a 5-byte Modbus exception response during state 7 polling, which was being reported as Invalid CRC or malformed packet ... (len=5). This PR treats that frame as a valid protocol exception instead of a malformed packet.

  • Protocol-aware exception frame handling

    • Added _is_modbus_exception_response(data, request) to recognize valid Modbus exception packets (address, function|0x80, exception_code, crc).
    • Added focused constants for exception parsing (MODBUS_EXCEPTION_PACKET_LENGTH, MIN_MODBUS_REQUEST_LENGTH, MODBUS_EXCEPTION_FLAG).
  • State read-path behavior update

    • In _send_and_receive(...), when a valid exception frame is detected:
      • log as a Modbus exception warning (including tower/state context),
      • stop the current polling cycle cleanly.
    • Keep existing malformed/invalid CRC logging for packets that do not match valid exception framing.
  • Guardrails for false positives

    • Exception detection now validates request/response address alignment before matching function-code exception flag.
if data and self._is_modbus_exception_response(data, request):
    exception_code = data[2]
    _LOGGER.warning(
        "Modbus exception response in %s for tower %d (exception=0x%02X, len=%d)",
        state_name, self.current_tower, exception_code, len(data),
    )
    self._state = 0
    return None

Copilot AI changed the title [WIP] Fix invalid CRC or malformed packet in state 7 error Handle 5-byte Modbus exception frames without false “malformed packet” errors in state 7 May 19, 2026
Copilot finished work on behalf of bbr111 May 19, 2026 19:59
Copilot AI requested a review from bbr111 May 19, 2026 19:59
Copilot finished work on behalf of bbr111 May 19, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid CRC or malformed packet in state 7 for tower 0 (len=5)

2 participants