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
50 changes: 50 additions & 0 deletions can/io/asc.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,41 @@ def _process_classic_can_frame(
return Message(**msg_kwargs)

def _process_fd_can_frame(self, line: str, msg_kwargs: dict[str, Any]) -> Message:
fields = line.split()
if len(fields) >= 4 and fields[1] not in ("Rx", "Tx"):
# CANoe 7.x uses <channel> <id> <direction> instead of
# <channel> <direction> <id>. CANoe also writes FD DLC codes in
# decimal even for hex-base logs, so the data-length field decides
# between decimal and hexadecimal interpretation.
channel, can_id_str, direction = fields[:3]
msg_kwargs["channel"] = int(channel) - 1
msg_kwargs["is_rx"] = direction == "Rx"
self._extract_can_id(can_id_str, msg_kwargs)

rest = fields[3:]
if rest[:1] and rest[0].lower() == "errorframe":
msg_kwargs["is_error_frame"] = True
return Message(**msg_kwargs)
if len(rest) < 4:
return Message(**msg_kwargs)

brs, esi = rest[0], rest[1]
index = 2
if index < len(rest) and not rest[index].isdigit():
index += 1
if index + 1 >= len(rest):
return Message(**msg_kwargs)

dlc_str = rest[index]
data_length = int(rest[index + 1])
dlc = self._parse_fd_dlc(dlc_str, data_length)
msg_kwargs["is_fd"] = True
msg_kwargs["bitrate_switch"] = brs == "1"
msg_kwargs["error_state_indicator"] = esi == "1"
msg_kwargs["dlc"] = data_length
msg_kwargs["data"] = self._parse_data(rest[index + 2 :], data_length)
return Message(**msg_kwargs)

channel, direction, rest_of_message = line.split(None, 2)
# See ASCWriter
msg_kwargs["channel"] = int(channel) - 1
Expand Down Expand Up @@ -294,6 +329,21 @@ def _process_fd_can_frame(self, line: str, msg_kwargs: dict[str, Any]) -> Messag

return Message(**msg_kwargs)

def _parse_fd_dlc(self, dlc_str: str, data_length: int) -> int:
"""Resolve CANoe decimal and python-can hexadecimal DLC spellings."""
candidates: list[int] = []
for base in (self._converted_base, BASE_DEC):
try:
code = int(dlc_str, base)
except ValueError:
continue
if code not in candidates:
candidates.append(code)
for code in candidates:
if dlc2len(code) == data_length:
return code
return candidates[0] if candidates else 0

def __iter__(self) -> Generator[Message, None, None]:
first_line = self._extract_header()
lines = (
Expand Down
3 changes: 3 additions & 0 deletions doc/changelog.d/canoe7.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed ``ASCReader`` support for CANoe 7.x CAN FD records that place the CAN
identifier before the ``Rx``/``Tx`` direction. CANoe decimal DLC codes are now
resolved against the record's authoritative data-length field.
32 changes: 32 additions & 0 deletions test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,38 @@ def test_roundtrip_empty_can_fd_data(self):
(actual,) = reader
self.assertTrue(msg.equals(actual, timestamp_delta=None))

def test_read_canoe_7_can_fd_layout(self):
stream = StringIO(
"date Mon Oct 12 06:42:09 PM 2026\n"
"base hex timestamps absolute\n"
"// version 7.0.0\n"
"0.000000 CANFD 1 1d3 Rx 1 0 d 8 8 "
"78 88 00 00 00 00 0c e6\n"
)
with can.ASCReader(stream) as reader:
(actual,) = reader

self.assertTrue(actual.is_fd)
self.assertEqual(actual.arbitration_id, 0x1D3)
self.assertEqual(actual.channel, 0)
self.assertTrue(actual.is_rx)
self.assertTrue(actual.bitrate_switch)
self.assertEqual(actual.dlc, 8)
self.assertEqual(actual.data, b"\x78\x88\x00\x00\x00\x00\x0c\xe6")

def test_read_canoe_7_decimal_fd_dlc(self):
stream = StringIO(
"base hex timestamps absolute\n"
"0.004238 CANFD 1 1e3 Rx 1 0 d 13 32 " + " ".join(["00"] * 32) + "\n"
)
with can.ASCReader(stream) as reader:
(actual,) = reader

self.assertTrue(actual.is_fd)
self.assertEqual(actual.arbitration_id, 0x1E3)
self.assertEqual(actual.dlc, 32)
self.assertEqual(len(actual.data), 32)

@parameterized.expand(
[
("empty_data", "0 0 0 0 0", False, 0, b""),
Expand Down
Loading