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/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. 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