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"""