diff --git a/can/io/canutils.py b/can/io/canutils.py index e3e329a77..9d5c1b931 100644 --- a/can/io/canutils.py +++ b/can/io/canutils.py @@ -19,6 +19,8 @@ CAN_ERR_FLAG = 0x20000000 CAN_ERR_BUSERROR = 0x00000080 CAN_ERR_DLC = 8 +# separates an 8-byte Classical CAN frame from its raw DLC (9 to 15) +CC_DLC_DELIM = "_" CANFD_BRS = 0x01 CANFD_ESI = 0x02 @@ -86,15 +88,20 @@ def __iter__(self) -> Generator[Message, None, None]: if data and data[0].lower() == "r": is_remote_frame = True - if len(data) > 1: - dlc = int(data[1:]) - else: - dlc = 0 + length = data[1:] + if not is_fd and CC_DLC_DELIM in length: + # like can-utils: an optional length digit 0 to 8, then the raw DLC + digit = length[:1] + length = digit if digit in "012345678" else "" + dlc = int(length) if length else 0 data_bin = None else: is_remote_frame = False + # the raw DLC is only valid right after 8 data bytes; dlc stays 8 + if not is_fd and data[16:17] == CC_DLC_DELIM: + data = data[:16] dlc = len(data) // 2 data_bin = bytearray() for i in range(0, len(data), 2): diff --git a/doc/changelog.d/2163.fixed.rst b/doc/changelog.d/2163.fixed.rst new file mode 100644 index 000000000..d58e403fa --- /dev/null +++ b/doc/changelog.d/2163.fixed.rst @@ -0,0 +1,3 @@ +Fixed ``CanutilsLogReader`` stopping with a ``ValueError`` on candump log lines that carry a +Classical CAN raw DLC (``123#1122334455667788_C``, ``123#R8_F``) and reading ``R8_0`` to +``R8_9`` as DLC 80 to 89. Such frames are now read with ``dlc=8``. diff --git a/test/logformats_test.py b/test/logformats_test.py index 71056a8da..5b5ea63c7 100644 --- a/test/logformats_test.py +++ b/test/logformats_test.py @@ -1272,6 +1272,69 @@ def test_write_error_frame_without_class(self): read_back, ) + def test_read_raw_dlc(self): + # candump -L writes a Classical CAN DLC of 9 to 15 after an 8-byte + # payload as "_"; the frame still carries 8 bytes, so dlc is 8 + with open(self.test_file_name, "w", encoding="ascii") as file: + file.write("(1.000000) can0 123#1122334455667788_C R\n") + file.write("(2.000000) can0 123#R8_F R\n") + # can-utils reads a one-digit remote length of 0 to 8 before the delimiter + file.write("(3.000000) can0 123#R08_C R\n") + file.write("(4.000000) can0 123#R9_C R\n") + + with can.CanutilsLogReader(self.test_file_name) as reader: + messages = list(reader) + + expected = [ + can.Message( + timestamp=1.0, + arbitration_id=0x123, + is_extended_id=False, + dlc=8, + data=bytes.fromhex("1122334455667788"), + channel="can0", + ), + can.Message( + timestamp=2.0, + arbitration_id=0x123, + is_extended_id=False, + is_remote_frame=True, + dlc=8, + channel="can0", + ), + can.Message( + timestamp=3.0, + arbitration_id=0x123, + is_extended_id=False, + is_remote_frame=True, + dlc=0, + channel="can0", + ), + can.Message( + timestamp=4.0, + arbitration_id=0x123, + is_extended_id=False, + is_remote_frame=True, + dlc=0, + channel="can0", + ), + ] + self.assertEqual(len(messages), len(expected)) + for actual, wanted in zip(messages, expected): + self.assertTrue(wanted.equals(actual), f"{actual!r} != {wanted!r}") + + @parameterized.expand( + [("123#1122_C",), ("123##0R8_C",), ("123##01122334455667788_C",)] + ) + def test_read_raw_dlc_invalid(self, frame): + # can-utils accepts the suffix only after 8 bytes of a Classical CAN frame + with open(self.test_file_name, "w", encoding="ascii") as file: + file.write(f"(1.000000) can0 {frame} R\n") + + with can.CanutilsLogReader(self.test_file_name) as reader: + with self.assertRaises(ValueError): + list(reader) + class TestCsvFileFormat(ReaderWriterTest): """Tests can.CSVWriter and can.CSVReader"""