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
13 changes: 6 additions & 7 deletions can/io/trc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from ..message import Message
from ..typechecking import StringPathLike
from ..util import channel2int, len2dlc
from ..util import channel2int, dlc2len
from .generic import TextIOMessageReader, TextIOMessageWriter

logger = logging.getLogger("can.io.trc")
Expand Down Expand Up @@ -190,25 +190,24 @@ def _parse_msg_v2_x(self, cols: tuple[str, ...]) -> Message | None:
type_ = cols[self.columns["T"]]
bus = self.columns.get("B", None)

msg = Message()
msg.is_fd = type_ in {"FD", "FB", "FE", "BI"}
if "l" in self.columns:
length = int(cols[self.columns["l"]])
dlc = len2dlc(length)
msg.dlc = int(cols[self.columns["l"]])
elif "L" in self.columns:
dlc = int(cols[self.columns["L"]])
msg.dlc = dlc2len(dlc) if msg.is_fd and 0 <= dlc <= 15 else dlc
else:
raise ValueError("No length/dlc columns present.")

msg = Message()
msg.timestamp = float(cols[self.columns["O"]]) / 1000 + self._start_time
msg.arbitration_id = int(cols[self.columns["I"]], 16)
msg.is_extended_id = len(cols[self.columns["I"]]) > 4
msg.channel = int(cols[bus]) if bus is not None else 1
msg.dlc = dlc
msg.is_remote_frame = type_ in {"RR"}
if dlc and not msg.is_remote_frame:
if msg.dlc and not msg.is_remote_frame:
msg.data = bytearray.fromhex(cols[self.columns["D"]])
msg.is_rx = cols[self.columns["d"]] == "Rx"
msg.is_fd = type_ in {"FD", "FB", "FE", "BI"}
msg.bitrate_switch = type_ in {"FB", "FE"}
msg.error_state_indicator = type_ in {"FE", "BI"}

Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/+trc-payload-length.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :class:`~can.TRCReader` reporting an incorrect payload length for CAN FD and long J1939 messages in TRC 2.x files.
4 changes: 3 additions & 1 deletion doc/file_io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ Implements basic support for the TRC file format.
:show-inheritance:
:members:

The following class can be used to read messages from TRC file:
When reading CAN FD frames from TRC 2.0 and 2.1 files, :class:`~can.TRCReader`
reports :attr:`~can.Message.dlc` in bytes for both the data length (``l``) and
data length code (``L``) columns.

.. autoclass:: can.TRCReader
:show-inheritance:
Expand Down
26 changes: 25 additions & 1 deletion test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from abc import ABCMeta, abstractmethod
from contextlib import contextmanager
from datetime import datetime, timedelta, timezone
from io import BytesIO
from io import BytesIO, StringIO
from itertools import zip_longest
from pathlib import Path
from unittest.mock import patch
Expand Down Expand Up @@ -1319,6 +1319,30 @@ def msg_rtr(timestamp):
actual = self._read_log_file(filename)
self.assertMessagesEqual(actual, expected_messages)

@parameterized.expand(
[
("2.0", "l", "FD 0123 Rx 12", 12),
("2.0", "L", "FB 0123 Rx 9", 12),
("2.1", "l", "FE 0123 Rx 64", 64),
("2.1", "L", "BI 0123 Rx 15", 64),
("2.1", "R,l", "DT 18FF0034 Rx FF 80", 80), # J1939
("2.1", "R,L", "DT 18FF0034 Rx FF 80", 80),
]
)
def test_v2_payload_length(self, version, columns, fields, length):
payload = bytes(range(length))
trace = StringIO(
f";$FILEVERSION={version}\n"
";$STARTTIME=44548.0\n"
f";$COLUMNS=N,O,T,I,d,{columns},D\n"
f"1 0.000 {fields} {payload.hex(' ')}\n"
)
with can.TRCReader(trace) as reader:
(msg,) = list(reader)

self.assertEqual(msg.dlc, length)
self.assertEqual(msg.data, payload)

def test_not_supported_version(self):
with tempfile.NamedTemporaryFile(mode="w") as f:
with self.assertRaises(NotImplementedError):
Expand Down
Loading