Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions can/io/canutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions doc/changelog.d/+canutils-error-frames.fixed.rst
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 47 additions & 0 deletions test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down