From 4c172a74d02039858bbdcf1252b7a30d0a574b8b Mon Sep 17 00:00:00 2001 From: JR Date: Sat, 21 Nov 2026 20:20:48 +0100 Subject: [PATCH 1/2] canutils: read every candump error frame as an error frame candump writes an error frame as CAN_ERR_FLAG plus the error class bits, followed by the error data bytes. The reader only recognised the bus error class, so a controller problem (20000004#...) or bus off frame was read as an extended data frame, and a bus error frame lost its channel, direction and data bytes. Test CAN_ERR_FLAG alone and keep the class bits in arbitration_id, as the SocketCAN interface does, together with the data bytes, channel and direction. The writer now writes the message's error class instead of always writing the bus error class, so an error frame round-trips. --- can/io/canutils.py | 34 +++++++------- .../+canutils-error-frames.fixed.rst | 4 ++ test/logformats_test.py | 47 +++++++++++++++++++ 3 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 doc/changelog.d/+canutils-error-frames.fixed.rst diff --git a/can/io/canutils.py b/can/io/canutils.py index 800125b73..994b7155f 100644 --- a/can/io/canutils.py +++ b/can/io/canutils.py @@ -100,23 +100,21 @@ def __iter__(self) -> Generator[Message, None, None]: for i in range(0, len(data), 2): data_bin.append(int(data[i : (i + 2)], 16)) - if can_id & CAN_ERR_FLAG and can_id & CAN_ERR_BUSERROR: - msg = Message(timestamp=timestamp, is_error_frame=True) - else: - msg = Message( - timestamp=timestamp, - arbitration_id=can_id & 0x1FFFFFFF, - is_extended_id=is_extended, - is_remote_frame=is_remote_frame, - is_fd=is_fd, - is_rx=is_rx, - bitrate_switch=brs, - error_state_indicator=esi, - dlc=dlc, - data=data_bin, - channel=channel, - ) - yield msg + # error frames carry their error class bits in the identifier + yield Message( + timestamp=timestamp, + arbitration_id=can_id & 0x1FFFFFFF, + is_extended_id=is_extended, + is_remote_frame=is_remote_frame, + is_error_frame=bool(can_id & CAN_ERR_FLAG), + is_fd=is_fd, + is_rx=is_rx, + bitrate_switch=brs, + error_state_indicator=esi, + dlc=dlc, + data=data_bin, + channel=channel, + ) self.stop() @@ -169,7 +167,7 @@ def on_message_received(self, msg: Message) -> None: framestr = f"({timestamp:f}) {channel}" if msg.is_error_frame: - framestr += f" {CAN_ERR_FLAG | CAN_ERR_BUSERROR:08X}#" + framestr += f" {CAN_ERR_FLAG | (msg.arbitration_id & 0x1FFFFFFF):08X}#" elif msg.is_extended_id: framestr += f" {msg.arbitration_id:08X}#" else: diff --git a/doc/changelog.d/+canutils-error-frames.fixed.rst b/doc/changelog.d/+canutils-error-frames.fixed.rst new file mode 100644 index 000000000..ca1a7dcd1 --- /dev/null +++ b/doc/changelog.d/+canutils-error-frames.fixed.rst @@ -0,0 +1,4 @@ +Fixed ``CanutilsLogReader`` reading candump error frames of any class other than bus error +as data frames. Error frames now keep their error class in ``arbitration_id``, their data +bytes, channel and direction, and ``CanutilsLogWriter`` writes that error class instead of +always writing the bus error class. diff --git a/test/logformats_test.py b/test/logformats_test.py index beb5b7b9f..5563389ec 100644 --- a/test/logformats_test.py +++ b/test/logformats_test.py @@ -1009,6 +1009,53 @@ def _setup_instance(self): adds_default_channel="vcan0", ) + def test_read_error_frames(self): + # error frames as candump -L writes them: CAN_ERR_FLAG plus the error + # class in the identifier, followed by the eight error data bytes + with open(self.test_file_name, "w", encoding="ascii") as file: + file.write("(1.000000) can0 20000004#0004000000000000\n") + file.write("(2.000000) can1 20000080#0000000000000000\n") + + with can.CanutilsLogReader(self.test_file_name) as reader: + messages = list(reader) + + expected = [ + can.Message( + timestamp=1.0, + arbitration_id=0x004, + is_error_frame=True, + data=bytes.fromhex("0004000000000000"), + channel="can0", + ), + can.Message( + timestamp=2.0, + arbitration_id=0x080, + is_error_frame=True, + data=bytes(8), + channel="can1", + ), + ] + self.assertEqual(len(messages), len(expected)) + for actual, wanted in zip(messages, expected): + self.assertTrue(wanted.equals(actual), f"{actual!r} != {wanted!r}") + + def test_write_error_frame_class(self): + message = can.Message( + timestamp=1.0, + arbitration_id=0x204, + is_error_frame=True, + data=bytes.fromhex("0004000000000102"), + channel="can1", + ) + with can.CanutilsLogWriter(self.test_file_name) as writer: + writer(message) + + with open(self.test_file_name, encoding="ascii") as file: + self.assertEqual(file.read(), "(1.000000) can1 20000204#0004000000000102\n") + with can.CanutilsLogReader(self.test_file_name) as reader: + (read_back,) = reader + self.assertTrue(message.equals(read_back), f"{read_back!r}") + class TestCsvFileFormat(ReaderWriterTest): """Tests can.CSVWriter and can.CSVReader""" From 0a360c5a0b642bbd388deca579f6dc95789db7ac Mon Sep 17 00:00:00 2001 From: JR Date: Sun, 22 Nov 2026 20:17:23 +0100 Subject: [PATCH 2/2] canutils: write an error frame without class bits as a bus error Older python-can readers recognise a candump error frame only by CAN_ERR_BUSERROR, so an error frame without class bits (as read from ASC or BLF) keeps the bus error class on write. The shared error frame round trip no longer holds for such frames; dedicated tests cover it. Rename the news fragment to the PR number. --- can/io/canutils.py | 7 ++++++- ...ls-error-frames.fixed.rst => 2146.fixed.rst} | 3 ++- test/logformats_test.py | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) rename doc/changelog.d/{+canutils-error-frames.fixed.rst => 2146.fixed.rst} (69%) diff --git a/can/io/canutils.py b/can/io/canutils.py index 994b7155f..452d99742 100644 --- a/can/io/canutils.py +++ b/can/io/canutils.py @@ -167,7 +167,12 @@ def on_message_received(self, msg: Message) -> None: framestr = f"({timestamp:f}) {channel}" if msg.is_error_frame: - framestr += f" {CAN_ERR_FLAG | (msg.arbitration_id & 0x1FFFFFFF):08X}#" + error_class = msg.arbitration_id & 0x1FFFFFFF + if not error_class: + # no SocketCAN error class bits: write CAN_ERR_BUSERROR, + # so older readers still recognise the line as an error frame + error_class = CAN_ERR_BUSERROR + framestr += f" {CAN_ERR_FLAG | error_class:08X}#" elif msg.is_extended_id: framestr += f" {msg.arbitration_id:08X}#" else: diff --git a/doc/changelog.d/+canutils-error-frames.fixed.rst b/doc/changelog.d/2146.fixed.rst similarity index 69% rename from doc/changelog.d/+canutils-error-frames.fixed.rst rename to doc/changelog.d/2146.fixed.rst index ca1a7dcd1..de2f7b220 100644 --- a/doc/changelog.d/+canutils-error-frames.fixed.rst +++ b/doc/changelog.d/2146.fixed.rst @@ -1,4 +1,5 @@ Fixed ``CanutilsLogReader`` reading candump error frames of any class other than bus error as data frames. Error frames now keep their error class in ``arbitration_id``, their data bytes, channel and direction, and ``CanutilsLogWriter`` writes that error class instead of -always writing the bus error class. +always writing the bus error class. An error frame without error class bits is still +written with the bus error class. diff --git a/test/logformats_test.py b/test/logformats_test.py index 5563389ec..5a11bbb1e 100644 --- a/test/logformats_test.py +++ b/test/logformats_test.py @@ -1002,6 +1002,9 @@ def _setup_instance(self): super()._setup_instance_helper( can.CanutilsLogWriter, can.CanutilsLogReader, + # error frames without class bits are written as bus errors, + # see test_write_error_frame_without_class + check_error_frames=False, check_fd=True, test_append=True, check_comments=False, @@ -1056,6 +1059,20 @@ def test_write_error_frame_class(self): (read_back,) = reader self.assertTrue(message.equals(read_back), f"{read_back!r}") + def test_write_error_frame_without_class(self): + message = can.Message(timestamp=1.0, is_error_frame=True, channel="can0") + with can.CanutilsLogWriter(self.test_file_name) as writer: + writer(message) + + with open(self.test_file_name, encoding="ascii") as file: + self.assertEqual(file.read(), "(1.000000) can0 20000080#\n") + with can.CanutilsLogReader(self.test_file_name) as reader: + (read_back,) = reader + self.assertMessageEqual( + can.Message(timestamp=1.0, arbitration_id=0x080, is_error_frame=True), + read_back, + ) + class TestCsvFileFormat(ReaderWriterTest): """Tests can.CSVWriter and can.CSVReader"""