Skip to content
Merged
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
39 changes: 21 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,12 @@ 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}#"
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:
Expand Down
5 changes: 5 additions & 0 deletions doc/changelog.d/2146.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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. An error frame without error class bits is still
written with the bus error class.
64 changes: 64 additions & 0 deletions test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,13 +1002,77 @@ 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,
preserves_channel=False,
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}")

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"""
Expand Down
Loading