Skip to content

Commit

Permalink
Add trc 1.3 read support (#1753)
Browse files Browse the repository at this point in the history
  • Loading branch information
1atabey1 committed Mar 12, 2024
1 parent d40915c commit ffb14a3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
30 changes: 30 additions & 0 deletions can/io/trc.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def _extract_header(self):
file_version = line.split("=")[1]
if file_version == "1.1":
self.file_version = TRCFileVersion.V1_1
elif file_version == "1.3":
self.file_version = TRCFileVersion.V1_3
elif file_version == "2.0":
self.file_version = TRCFileVersion.V2_0
elif file_version == "2.1":
Expand Down Expand Up @@ -121,6 +123,8 @@ def _extract_header(self):
self._parse_cols = self._parse_msg_v1_0
elif self.file_version == TRCFileVersion.V1_1:
self._parse_cols = self._parse_cols_v1_1
elif self.file_version == TRCFileVersion.V1_3:
self._parse_cols = self._parse_cols_v1_3
elif self.file_version in [TRCFileVersion.V2_0, TRCFileVersion.V2_1]:
self._parse_cols = self._parse_cols_v2_x
else:
Expand Down Expand Up @@ -161,6 +165,24 @@ def _parse_msg_v1_1(self, cols: List[str]) -> Optional[Message]:
msg.is_rx = cols[2] == "Rx"
return msg

def _parse_msg_v1_3(self, cols: List[str]) -> Optional[Message]:
arbit_id = cols[4]

msg = Message()
if isinstance(self.start_time, datetime):
msg.timestamp = (
self.start_time + timedelta(milliseconds=float(cols[1]))
).timestamp()
else:
msg.timestamp = float(cols[1]) / 1000
msg.arbitration_id = int(arbit_id, 16)
msg.is_extended_id = len(arbit_id) > 4
msg.channel = int(cols[2])
msg.dlc = int(cols[6])
msg.data = bytearray([int(cols[i + 7], 16) for i in range(msg.dlc)])
msg.is_rx = cols[3] == "Rx"
return msg

def _parse_msg_v2_x(self, cols: List[str]) -> Optional[Message]:
type_ = cols[self.columns["T"]]
bus = self.columns.get("B", None)
Expand Down Expand Up @@ -203,6 +225,14 @@ def _parse_cols_v1_1(self, cols: List[str]) -> Optional[Message]:
logger.info("TRCReader: Unsupported type '%s'", dtype)
return None

def _parse_cols_v1_3(self, cols: List[str]) -> Optional[Message]:
dtype = cols[3]
if dtype in ("Tx", "Rx"):
return self._parse_msg_v1_3(cols)
else:
logger.info("TRCReader: Unsupported type '%s'", dtype)
return None

def _parse_cols_v2_x(self, cols: List[str]) -> Optional[Message]:
dtype = cols[self.columns["T"]]
if dtype in ["DT", "FD", "FB"]:
Expand Down
32 changes: 32 additions & 0 deletions test/data/test_CanMessage_V1_3.trc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
;$FILEVERSION=1.3
;$STARTTIME=44548.6028595139
;
; C:\test.trc
; Start time: 18.12.2021 14:28:07.062.0
; Generated by PCAN-Explorer v5.4.0
;-------------------------------------------------------------------------------
; Bus Name Connection Protocol Bit rate
; 1 PCAN Untitled@pcan_usb CAN 500 kbit/s
; 2 PTCAN PCANLight_USB_16@pcan_usb CAN
;-------------------------------------------------------------------------------
; Message Number
; | Time Offset (ms)
; | | Bus
; | | | Type
; | | | | ID (hex)
; | | | | | Reserved
; | | | | | | Data Length Code
; | | | | | | | Data Bytes (hex) ...
; | | | | | | | |
; | | | | | | | |
;---+-- ------+------ +- --+-- ----+--- +- -+-- -+ -- -- -- -- -- -- --
1) 17535.4 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
2) 17700.3 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
3) 17873.8 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
4) 19295.4 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
5) 19500.6 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
6) 19705.2 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
7) 20592.7 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
8) 20798.6 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
9) 20956.0 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
10) 21097.1 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
1 change: 1 addition & 0 deletions test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ def test_can_message(self):
[
("V1_0", "test_CanMessage_V1_0_BUS1.trc", False),
("V1_1", "test_CanMessage_V1_1.trc", True),
("V1_3", "test_CanMessage_V1_3.trc", True),
("V2_1", "test_CanMessage_V2_1.trc", True),
]
)
Expand Down

0 comments on commit ffb14a3

Please sign in to comment.