Skip to content

Commit 4d9b32c

Browse files
jazi007Nadhmi JAZI
andauthored
[IO][canutils]: add direction support (#1244)
Co-authored-by: Nadhmi JAZI <[email protected]>
1 parent d177a82 commit 4d9b32c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

can/io/canutils.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ def __iter__(self) -> Generator[Message, None, None]:
5151
continue
5252

5353
channel_string: str
54-
timestamp_string, channel_string, frame = temp.split()
54+
if temp[-2:].lower() in (" r", " t"):
55+
timestamp_string, channel_string, frame, is_rx_string = temp.split()
56+
is_rx = is_rx_string.strip().lower() == "r"
57+
else:
58+
timestamp_string, channel_string, frame = temp.split()
59+
is_rx = True
5560
timestamp = float(timestamp_string[1:-1])
5661
can_id_string, data = frame.split("#", maxsplit=1)
5762

@@ -101,6 +106,7 @@ def __iter__(self) -> Generator[Message, None, None]:
101106
is_extended_id=is_extended,
102107
is_remote_frame=is_remote_frame,
103108
is_fd=is_fd,
109+
is_rx=is_rx,
104110
bitrate_switch=brs,
105111
error_state_indicator=esi,
106112
dlc=dlc,
@@ -164,8 +170,13 @@ def on_message_received(self, msg):
164170
else:
165171
framestr += " %03X#" % (msg.arbitration_id)
166172

173+
if msg.is_error_frame:
174+
eol = "\n"
175+
else:
176+
eol = " R\n" if msg.is_rx else " T\n"
177+
167178
if msg.is_remote_frame:
168-
framestr += "R\n"
179+
framestr += f"R{eol}"
169180
else:
170181
if msg.is_fd:
171182
fd_flags = 0
@@ -174,6 +185,6 @@ def on_message_received(self, msg):
174185
if msg.error_state_indicator:
175186
fd_flags |= CANFD_ESI
176187
framestr += "#%X" % fd_flags
177-
framestr += "%s\n" % (msg.data.hex().upper())
188+
framestr += f"{msg.data.hex().upper()}{eol}"
178189

179190
self.file.write(framestr)

test/test_logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_compressed_logfile(self):
132132
with gzip.open(self.testfile.name, "rt") as testlog:
133133
last_line = testlog.readlines()[-1]
134134

135-
self.assertEqual(last_line, "(0.000000) vcan0 00C0FFEE#0019000103010401\n")
135+
self.assertEqual(last_line, "(0.000000) vcan0 00C0FFEE#0019000103010401 R\n")
136136

137137
def tearDown(self) -> None:
138138
self.testfile.close()

0 commit comments

Comments
 (0)