Skip to content

Commit 82d52fc

Browse files
Add TRCReader RTR frames support (#1953)
* New trc test files with RTR frame * trc reader support for RTR frames parsing * black format
1 parent 958fc64 commit 82d52fc

File tree

7 files changed

+68
-35
lines changed

7 files changed

+68
-35
lines changed

can/io/trc.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ def _parse_msg_v1_0(self, cols: tuple[str, ...]) -> Optional[Message]:
153153
msg.is_extended_id = len(arbit_id) > 4
154154
msg.channel = 1
155155
msg.dlc = int(cols[3])
156-
msg.data = bytearray([int(cols[i + 4], 16) for i in range(msg.dlc)])
156+
if len(cols) > 4 and cols[4] == "RTR":
157+
msg.is_remote_frame = True
158+
else:
159+
msg.data = bytearray([int(cols[i + 4], 16) for i in range(msg.dlc)])
157160
return msg
158161

159162
def _parse_msg_v1_1(self, cols: tuple[str, ...]) -> Optional[Message]:
@@ -165,7 +168,10 @@ def _parse_msg_v1_1(self, cols: tuple[str, ...]) -> Optional[Message]:
165168
msg.is_extended_id = len(arbit_id) > 4
166169
msg.channel = 1
167170
msg.dlc = int(cols[4])
168-
msg.data = bytearray([int(cols[i + 5], 16) for i in range(msg.dlc)])
171+
if len(cols) > 5 and cols[5] == "RTR":
172+
msg.is_remote_frame = True
173+
else:
174+
msg.data = bytearray([int(cols[i + 5], 16) for i in range(msg.dlc)])
169175
msg.is_rx = cols[2] == "Rx"
170176
return msg
171177

@@ -178,7 +184,10 @@ def _parse_msg_v1_3(self, cols: tuple[str, ...]) -> Optional[Message]:
178184
msg.is_extended_id = len(arbit_id) > 4
179185
msg.channel = int(cols[2])
180186
msg.dlc = int(cols[6])
181-
msg.data = bytearray([int(cols[i + 7], 16) for i in range(msg.dlc)])
187+
if len(cols) > 7 and cols[7] == "RTR":
188+
msg.is_remote_frame = True
189+
else:
190+
msg.data = bytearray([int(cols[i + 7], 16) for i in range(msg.dlc)])
182191
msg.is_rx = cols[3] == "Rx"
183192
return msg
184193

@@ -200,7 +209,8 @@ def _parse_msg_v2_x(self, cols: tuple[str, ...]) -> Optional[Message]:
200209
msg.is_extended_id = len(cols[self.columns["I"]]) > 4
201210
msg.channel = int(cols[bus]) if bus is not None else 1
202211
msg.dlc = dlc
203-
if dlc:
212+
msg.is_remote_frame = type_ in {"RR"}
213+
if dlc and not msg.is_remote_frame:
204214
msg.data = bytearray.fromhex(cols[self.columns["D"]])
205215
msg.is_rx = cols[self.columns["d"]] == "Rx"
206216
msg.is_fd = type_ in {"FD", "FB", "FE", "BI"}
@@ -227,7 +237,7 @@ def _parse_cols_v1_3(self, cols: tuple[str, ...]) -> Optional[Message]:
227237

228238
def _parse_cols_v2_x(self, cols: tuple[str, ...]) -> Optional[Message]:
229239
dtype = cols[self.columns["T"]]
230-
if dtype in {"DT", "FD", "FB", "FE", "BI"}:
240+
if dtype in {"DT", "FD", "FB", "FE", "BI", "RR"}:
231241
return self._parse_msg_v2_x(cols)
232242
else:
233243
logger.info("TRCReader: Unsupported type '%s'", dtype)

test/data/test_CanMessage_V1_0_BUS1.trc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
;##########################################################################
2-
; C:\Users\User\Desktop\python-can\test\data\test_CanMessage_V1_0_BUS1.trc
2+
; C:\NewFileName_BUS1.trc
33
;
4-
; CAN activities imported from C:\Users\User\Desktop\python-can\test\data\test_CanMessage_V1_1.trc
4+
; CAN activities imported from C:\test_CanMessage_V1_1.trc
55
; Start time: 18.12.2021 14:28:07.062
66
; PCAN-Net: N/A
7-
; Generated by PEAK-Converter Version 2.2.4.136
7+
; Generated by PEAK-Converter Version 3.0.4.594
88
;
99
; Columns description:
1010
; ~~~~~~~~~~~~~~~~~~~~~
@@ -26,3 +26,4 @@
2626
9) 20798 00000100 8 00 00 00 00 00 00 00 00
2727
10) 20956 00000100 8 00 00 00 00 00 00 00 00
2828
11) 21097 00000100 8 00 00 00 00 00 00 00 00
29+
12) 48937 0704 1 RTR

test/data/test_CanMessage_V1_1.trc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
8) 20592.7 Tx 00000100 8 00 00 00 00 00 00 00 00
2323
9) 20798.6 Tx 00000100 8 00 00 00 00 00 00 00 00
2424
10) 20956.0 Tx 00000100 8 00 00 00 00 00 00 00 00
25-
11) 21097.1 Tx 00000100 8 00 00 00 00 00 00 00 00
25+
11) 21097.1 Tx 00000100 8 00 00 00 00 00 00 00 00
26+
12) 48937.6 Rx 0704 1 RTR

test/data/test_CanMessage_V1_3.trc

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
;$FILEVERSION=1.3
22
;$STARTTIME=44548.6028595139
3+
; C:\NewFileName_V1_3.trc
34
;
4-
; C:\test.trc
5-
; Start time: 18.12.2021 14:28:07.062.0
6-
; Generated by PCAN-Explorer v5.4.0
5+
; Start time: 18.12.2021 14:28:07.062.1
6+
;
7+
; Generated by PEAK-Converter Version 3.0.4.594
8+
; Data imported from C:\test_CanMessage_V1_1.trc
79
;-------------------------------------------------------------------------------
8-
; Bus Name Connection Protocol Bit rate
9-
; 1 PCAN Untitled@pcan_usb CAN 500 kbit/s
10-
; 2 PTCAN PCANLight_USB_16@pcan_usb CAN
10+
; Bus Name Connection Protocol
11+
; N/A N/A N/A CAN
1112
;-------------------------------------------------------------------------------
1213
; Message Number
1314
; | Time Offset (ms)
@@ -20,13 +21,15 @@
2021
; | | | | | | | |
2122
; | | | | | | | |
2223
;---+-- ------+------ +- --+-- ----+--- +- -+-- -+ -- -- -- -- -- -- --
23-
1) 17535.4 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
24-
2) 17700.3 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
25-
3) 17873.8 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
26-
4) 19295.4 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
27-
5) 19500.6 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
28-
6) 19705.2 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
29-
7) 20592.7 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
30-
8) 20798.6 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
31-
9) 20956.0 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
32-
10) 21097.1 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
24+
1) 17535.400 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
25+
2) 17540.300 1 Warng FFFFFFFF - 4 00 00 00 08 BUSHEAVY
26+
3) 17700.300 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
27+
4) 17873.800 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
28+
5) 19295.400 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
29+
6) 19500.600 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
30+
7) 19705.200 1 Tx 0000 - 8 00 00 00 00 00 00 00 00
31+
8) 20592.700 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
32+
9) 20798.600 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
33+
10) 20956.000 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
34+
11) 21097.100 1 Tx 00000100 - 8 00 00 00 00 00 00 00 00
35+
12) 48937.600 1 Rx 0704 - 1 RTR

test/data/test_CanMessage_V2_0_BUS1.trc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
;$STARTTIME=44548.6028595139
33
;$COLUMNS=N,O,T,I,d,l,D
44
;
5-
; C:\Users\User\Desktop\python-can\test\data\test_CanMessage_V2_0_BUS1.trc
5+
; C:\test_CanMessage_V2_0_BUS1.trc
66
; Start time: 18.12.2021 14:28:07.062.001
7-
; Generated by PEAK-Converter Version 2.2.4.136
8-
; Data imported from C:\Users\User\Desktop\python-can\test\data\test_CanMessage_V1_1.trc
7+
; Generated by PEAK-Converter Version 3.0.4.594
8+
; Data imported from C:\test_CanMessage_V1_1.trc
99
;-------------------------------------------------------------------------------
1010
; Connection Bit rate
11-
; N/A N/A
11+
; N/A N/A
1212
;-------------------------------------------------------------------------------
1313
; Message Time Type ID Rx/Tx
1414
; Number Offset | [hex] | Data Length
1515
; | [ms] | | | | Data [hex] ...
16-
; | | | | | | |
16+
; | | | | | | |
1717
;---+-- ------+------ +- --+----- +- +- +- -- -- -- -- -- -- --
1818
1 17535.400 DT 00000100 Tx 8 00 00 00 00 00 00 00 00
1919
2 17540.300 ST Rx 00 00 00 08
@@ -26,3 +26,4 @@
2626
9 20798.600 DT 00000100 Tx 8 00 00 00 00 00 00 00 00
2727
10 20956.000 DT 00000100 Tx 8 00 00 00 00 00 00 00 00
2828
11 21097.100 DT 00000100 Tx 8 00 00 00 00 00 00 00 00
29+
12 48937.600 RR 0704 Rx 1

test/data/test_CanMessage_V2_1.trc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
;$STARTTIME=44548.6028595139
33
;$COLUMNS=N,O,T,B,I,d,R,L,D
44
;
5-
; C:\Users\User\Desktop\python-can\test\data\test_CanMessage_V2_1.trc
5+
; C:\test_CanMessage_V2_1.trc
66
; Start time: 18.12.2021 14:28:07.062.001
7-
; Generated by PEAK-Converter Version 2.2.4.136
8-
; Data imported from C:\Users\User\Desktop\python-can\test\data\test_CanMessage_V1_1.trc
7+
; Generated by PEAK-Converter Version 3.0.4.594
8+
; Data imported from C:\test_CanMessage_V1_1.trc
99
;-------------------------------------------------------------------------------
1010
; Bus Name Connection Protocol
11-
; N/A N/A N/A N/A
11+
; N/A N/A N/A N/A
1212
;-------------------------------------------------------------------------------
1313
; Message Time Type ID Rx/Tx
1414
; Number Offset | Bus [hex] | Reserved
1515
; | [ms] | | | | | Data Length Code
1616
; | | | | | | | | Data [hex] ...
17-
; | | | | | | | | |
17+
; | | | | | | | | |
1818
;---+-- ------+------ +- +- --+----- +- +- +--- +- -- -- -- -- -- -- --
1919
1 17535.400 DT 1 00000100 Tx - 8 00 00 00 00 00 00 00 00
2020
2 17540.300 ST 1 - Rx - 4 00 00 00 08
@@ -27,3 +27,4 @@
2727
9 20798.600 DT 1 00000100 Tx - 8 00 00 00 00 00 00 00 00
2828
10 20956.000 DT 1 00000100 Tx - 8 00 00 00 00 00 00 00 00
2929
11 21097.100 DT 1 00000100 Tx - 8 00 00 00 00 00 00 00 00
30+
12 48937.600 RR 1 0704 Rx - 1

test/logformats_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ def test_can_message(self):
990990
("V1_0", "test_CanMessage_V1_0_BUS1.trc", False),
991991
("V1_1", "test_CanMessage_V1_1.trc", True),
992992
("V1_3", "test_CanMessage_V1_3.trc", True),
993+
("V2_0", "test_CanMessage_V2_0_BUS1.trc", True),
993994
("V2_1", "test_CanMessage_V2_1.trc", True),
994995
]
995996
)
@@ -1029,6 +1030,20 @@ def msg_ext(timestamp):
10291030
msg.is_rx = False
10301031
return msg
10311032

1033+
def msg_rtr(timestamp):
1034+
msg = can.Message(
1035+
timestamp=timestamp + start_time,
1036+
arbitration_id=0x704,
1037+
is_extended_id=False,
1038+
is_remote_frame=True,
1039+
channel=1,
1040+
dlc=1,
1041+
data=[],
1042+
)
1043+
if is_rx_support:
1044+
msg.is_rx = True
1045+
return msg
1046+
10321047
expected_messages = [
10331048
msg_ext(17.5354),
10341049
msg_ext(17.7003),
@@ -1040,6 +1055,7 @@ def msg_ext(timestamp):
10401055
msg_ext(20.7986),
10411056
msg_ext(20.9560),
10421057
msg_ext(21.0971),
1058+
msg_rtr(48.9376),
10431059
]
10441060
actual = self._read_log_file(filename)
10451061
self.assertMessagesEqual(actual, expected_messages)

0 commit comments

Comments
 (0)