From 74161e5acd01bfcdedde4f0970efe9a4ff4643d4 Mon Sep 17 00:00:00 2001 From: Lizaola Hoaglund <348184934+lizaolahoaglund@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:35:02 +0800 Subject: [PATCH] pcan: skip controller status messages on receive PCAN-Basic can return PCAN_ERROR_OK with the PCAN_MESSAGE_STATUS bit set, for example when the CAN controller is activated. The ID and length fields in a status message do not describe a CAN frame. Skip status messages before constructing a Message. Continue reading queued frames while respecting the receive timeout. --- can/interfaces/pcan/pcan.py | 6 +++ doc/changelog.d/+pcan-skip-status.fixed.rst | 1 + test/test_pcan.py | 52 +++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 doc/changelog.d/+pcan-skip-status.fixed.rst diff --git a/can/interfaces/pcan/pcan.py b/can/interfaces/pcan/pcan.py index a11a7fa6c..9f78b58ae 100644 --- a/can/interfaces/pcan/pcan.py +++ b/can/interfaces/pcan/pcan.py @@ -57,6 +57,7 @@ PCAN_MESSAGE_FD, PCAN_MESSAGE_RTR, PCAN_MESSAGE_STANDARD, + PCAN_MESSAGE_STATUS, PCAN_NONEBUS, PCAN_PARAMETER_OFF, PCAN_PARAMETER_ON, @@ -514,6 +515,11 @@ def _recv_internal(self, timeout: float | None) -> tuple[Message | None, bool]: ) if result == PCAN_ERROR_OK: + if pcan_msg.MSGTYPE & PCAN_MESSAGE_STATUS.value: + # Keep draining queued messages on a nonblocking read. + if timeout == 0 or end_time is None or time.time() < end_time: + continue + return None, False # message received break diff --git a/doc/changelog.d/+pcan-skip-status.fixed.rst b/doc/changelog.d/+pcan-skip-status.fixed.rst new file mode 100644 index 000000000..21c92966e --- /dev/null +++ b/doc/changelog.d/+pcan-skip-status.fixed.rst @@ -0,0 +1 @@ +Skip PCAN controller status frames instead of returning them as CAN messages. diff --git a/test/test_pcan.py b/test/test_pcan.py index a9c6ea922..e4a45d66e 100644 --- a/test/test_pcan.py +++ b/test/test_pcan.py @@ -258,6 +258,58 @@ def test_recv_fd(self): self.assertEqual(recv_msg.timestamp, 0) self.assertEqual(recv_msg.channel, "PCAN_USBBUS1") + @parameterized.expand([("classic", False), ("fd", True)]) + def test_recv_skips_status_frames(self, name, fd): + if fd: + status = TPCANMsgFD(ID=1, DLC=4, MSGTYPE=PCAN_MESSAGE_STATUS.value) + data = TPCANMsgFD(ID=1, DLC=1, MSGTYPE=PCAN_MESSAGE_FD.value) + timestamp = TPCANTimestampFD() + read = self.mock_pcan.ReadFD + else: + status = TPCANMsg(ID=1, LEN=4, MSGTYPE=PCAN_MESSAGE_STATUS.value) + data = TPCANMsg(ID=1, LEN=1, MSGTYPE=PCAN_MESSAGE_STANDARD.value) + timestamp = TPCANTimestamp() + read = self.mock_pcan.Read + data.DATA[0] = 0xAB + read.side_effect = [ + (PCAN_ERROR_OK, status, timestamp), + (PCAN_ERROR_OK, data, timestamp), + (PCAN_ERROR_OK, status, timestamp), + (PCAN_ERROR_QRCVEMPTY, type(status)(), timestamp), + ] + self.bus = can.Bus(interface="pcan", fd=fd) + + recv_msg = self.bus.recv(timeout=0) + self.assertIsNotNone(recv_msg) + self.assertEqual(recv_msg.arbitration_id, 1) + self.assertEqual(recv_msg.is_fd, fd) + self.assertSequenceEqual(recv_msg.data, [0xAB]) + self.assertIsNone(self.bus.recv(timeout=0)) + + def test_recv_status_frames_respect_timeout(self): + self.bus = can.Bus(interface="pcan") + status = TPCANMsg(LEN=4, MSGTYPE=PCAN_MESSAGE_STATUS.value) + timestamp = TPCANTimestamp() + replies = iter( + [(PCAN_ERROR_OK, status, timestamp)] * 3 + + [(PCAN_ERROR_QRCVEMPTY, TPCANMsg(), timestamp)] + ) + now = 0.0 + + def read(_channel): + nonlocal now + now += 1.0 + return next(replies) + + self.mock_pcan.Read.side_effect = read + with ( + patch("can.bus.time", side_effect=lambda: now), + patch("can.interfaces.pcan.pcan.time") as pcan_time, + ): + pcan_time.time.side_effect = lambda: now + self.assertIsNone(self.bus.recv(timeout=2.0)) + self.assertEqual(self.mock_pcan.Read.call_count, 2) + @pytest.mark.timeout(3.0) @patch("select.select", return_value=([], [], [])) def test_recv_no_message(self, mock_select):