From 1b331cbbbaa375b16505505ae0cfd783afe77943 Mon Sep 17 00:00:00 2001 From: Dan Shinde <112338485+DanShinde@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:38:11 +0000 Subject: [PATCH 1/2] serial: do not read a payload for remote frames send() writes a remote frame with its DLC (the requested length) but no payload. _recv_internal() then read DLC payload bytes, so a remote frame with DLC > 0 swallowed the end-of-frame byte and the following frame, and recv() failed with TypeError. Read no payload for remote frames and document the encoding. --- can/interfaces/serial/serial_can.py | 4 +++- doc/interfaces/serial.rst | 2 ++ test/serial_test.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/can/interfaces/serial/serial_can.py b/can/interfaces/serial/serial_can.py index efef6cf59..a718f30d3 100644 --- a/can/interfaces/serial/serial_can.py +++ b/can/interfaces/serial/serial_can.py @@ -193,7 +193,9 @@ def _recv_internal(self, timeout: float | None) -> tuple[Message | None, bool]: else: arbitration_id = arbitration_id & CAN_ID_MASK_STD - data = self._ser.read(dlc) + # a remote frame carries the requested length in the DLC but + # no payload, see send() + data = b"" if is_remote_frame else self._ser.read(dlc) delimiter_byte = ord(self._ser.read()) if delimiter_byte == 0xBB: diff --git a/doc/interfaces/serial.rst b/doc/interfaces/serial.rst index 566ec7755..c81e2d9a2 100644 --- a/doc/interfaces/serial.rst +++ b/doc/interfaces/serial.rst @@ -33,6 +33,8 @@ arbitration ID will be interpreted as 4 byte unsigned integers. The DLC is also an unsigned integer with a length of 1 byte. Extended (29-bit) identifiers are encoded by adding 0x80000000 to the ID. For example, a 29-bit CAN ID of 0x123 is encoded with an arbitration ID of 0x80000123. +Remote frames set bit 30 (0x40000000) of the arbitration ID. They have no +payload; their DLC holds the requested data length. Serial frame format ^^^^^^^^^^^^^^^^^^^ diff --git a/test/serial_test.py b/test/serial_test.py index 409485112..a7100be4f 100644 --- a/test/serial_test.py +++ b/test/serial_test.py @@ -177,6 +177,21 @@ def test_rx_tx_rtr_frame(self): msg_receive = self.bus.recv() self.assertMessageEqual(msg, msg_receive) + def test_rx_tx_rtr_frame_with_dlc(self): + """ + Test the transfer of remote frames that request data. + """ + for dlc in (1, 8): + msg = can.Message( + arbitration_id=0x123, + is_extended_id=False, + is_remote_frame=True, + dlc=dlc, + ) + self.bus.send(msg) + msg_receive = self.bus.recv() + self.assertMessageEqual(msg, msg_receive) + def test_when_no_fileno(self): """ Tests for the fileno method catching the missing pyserial implementeation on the Windows platform From 6cf9cd0e91552b5c48b865392b9905437a49199f Mon Sep 17 00:00:00 2001 From: Dan Shinde <112338485+DanShinde@users.noreply.github.com> Date: Tue, 24 Nov 2026 06:02:36 +0000 Subject: [PATCH 2/2] Add news fragment --- doc/changelog.d/2151.fixed.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/2151.fixed.rst diff --git a/doc/changelog.d/2151.fixed.rst b/doc/changelog.d/2151.fixed.rst new file mode 100644 index 000000000..dbf35f5d5 --- /dev/null +++ b/doc/changelog.d/2151.fixed.rst @@ -0,0 +1 @@ +Fixed the serial interface reading payload bytes for received remote frames, which broke the stream for remote frames with a DLC above 0.