Skip to content

pcan: keep a received frame read with a bus-load warning - #2130

Open
adambodurka-lime wants to merge 2 commits into
hardbyte:mainfrom
adambodurka-lime:pcan-keep-frames-under-bus-load
Open

adambodurka-lime wants to merge 2 commits into
hardbyte:mainfrom
adambodurka-lime:pcan-keep-frames-under-bus-load

Conversation

@adambodurka-lime

Copy link
Copy Markdown

Summary of Changes

  • PcanBus drops a received frame when Read() also reports PCAN_ERROR_BUSLIGHT or PCAN_ERROR_BUSHEAVY. This keeps the frame when those are the only flags and the message is a data frame. Status frames, error frames and other errors are handled as before.

Type of Change

  • Bug fix

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox). Ran test/test_pcan.py and black --check only.

🤖 Generated with Claude Code

adambodurka-lime and others added 2 commits November 13, 2026 14:40
PCAN-Basic can report BUSLIGHT or BUSHEAVY together with a frame it
received. PcanBus dropped the frame in that case. Keep it when those
are the only flags and the message is not a status or error frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

elif result & (PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY):
log.warning(self._get_formatted_error(result))
if result & ~(PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY) == 0 and not (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd prefer intermediate variables for better readability like
no_other_errors = result & ~(PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY) == 0

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: pcan: keep a received frame read with a bus-load warning

The fix is correct: PCAN-Basic documents the message buffer as valid for these status codes (PEAK-System PCAN-Basic V5.1.0.1194 help, CAN_Read / CAN_ReadFD, "Returns": "MessageBuffer and TimestampBuffer are valid in this case"). Each new test turns red when the branch it guards is broken.

Findings

  • Error frames are still dropped (can/interfaces/pcan/pcan.py, new condition). PcanBus always enables PCAN_ALLOW_ERROR_FRAMES, so on PCAN_ERROR_OK an error frame is returned as is_error_frame=True. With this PR, the same error frame is discarded when BUSLIGHT/BUSHEAVY is set, which is when error frames are most likely to arrive. The PCAN-Basic docs make no exception for them. Exclude only PCAN_MESSAGE_STATUS; this also addresses the open readability comment:
    no_other_errors = (
        result & ~(PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY) == 0
    )
    is_status = pcan_msg.MSGTYPE & PCAN_MESSAGE_STATUS.value
    if no_other_errors and not is_status:
        # the bus state came with a received frame, keep the frame
        break
  • Tests (test/test_pcan.py): the error_frame case in test_recv_bus_load_flags_drop_other_reads asserts that error frames are dropped. That is the behaviour the previous finding asks to fix, so the case fails after the fix, and no other test checks that error frames are kept. Move it into test_recv_bus_load_flags_keep_the_frame: add a msgtype parameter there and assert recv_msg.is_error_frame == (msgtype == PCAN_MESSAGE_ERRFRAME.value). It then goes red if the error-frame exclusion is reintroduced.
  • Merge conflict: test/test_pcan.py conflicts with tests added on main since the branch point (test_recv_skips_status_frames, timestamp tests). Rebase onto main and keep both sets of tests.

Verdict

request changes

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.

2 participants