From e98173bdbe25a39f97a5d1bf9989ca4970b2e379 Mon Sep 17 00:00:00 2001 From: TIM ANDERSON Date: Wed, 18 Nov 2026 12:02:36 +1000 Subject: [PATCH] Fix payload lengths when reading CAN FD and J1939 TRC logs --- can/io/trc.py | 13 +++++----- doc/changelog.d/+trc-payload-length.fixed.rst | 1 + doc/file_io.rst | 4 ++- test/logformats_test.py | 26 ++++++++++++++++++- 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 doc/changelog.d/+trc-payload-length.fixed.rst diff --git a/can/io/trc.py b/can/io/trc.py index 1f59b5787..d40cf8c0a 100644 --- a/can/io/trc.py +++ b/can/io/trc.py @@ -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") @@ -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"} diff --git a/doc/changelog.d/+trc-payload-length.fixed.rst b/doc/changelog.d/+trc-payload-length.fixed.rst new file mode 100644 index 000000000..5af585928 --- /dev/null +++ b/doc/changelog.d/+trc-payload-length.fixed.rst @@ -0,0 +1 @@ +Fix :class:`~can.TRCReader` reporting an incorrect payload length for CAN FD and long J1939 messages in TRC 2.x files. diff --git a/doc/file_io.rst b/doc/file_io.rst index 329ccac53..3f8a7eba4 100644 --- a/doc/file_io.rst +++ b/doc/file_io.rst @@ -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: diff --git a/test/logformats_test.py b/test/logformats_test.py index beb5b7b9f..0bad23918 100644 --- a/test/logformats_test.py +++ b/test/logformats_test.py @@ -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 @@ -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):