Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions can/interfaces/pcan/pcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
PCAN_MESSAGE_FD,
PCAN_MESSAGE_RTR,
PCAN_MESSAGE_STANDARD,
PCAN_MESSAGE_STATUS,
PCAN_NONEBUS,
PCAN_PARAMETER_OFF,
PCAN_PARAMETER_ON,
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/+pcan-skip-status.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Skip PCAN controller status frames instead of returning them as CAN messages.
52 changes: 52 additions & 0 deletions test/test_pcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down