Skip to content

Commit f2cb4cb

Browse files
authored
ASCWriter: use correct channel for error frame (#1583)
* use correct channel for error frame * add test
1 parent 6c820a1 commit f2cb4cb

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

can/io/asc.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,15 @@ def log_event(self, message: str, timestamp: Optional[float] = None) -> None:
420420
self.file.write(line)
421421

422422
def on_message_received(self, msg: Message) -> None:
423+
channel = channel2int(msg.channel)
424+
if channel is None:
425+
channel = self.channel
426+
else:
427+
# Many interfaces start channel numbering at 0 which is invalid
428+
channel += 1
429+
423430
if msg.is_error_frame:
424-
self.log_event(f"{self.channel} ErrorFrame", msg.timestamp)
431+
self.log_event(f"{channel} ErrorFrame", msg.timestamp)
425432
return
426433
if msg.is_remote_frame:
427434
dtype = f"r {msg.dlc:x}" # New after v8.5
@@ -432,12 +439,6 @@ def on_message_received(self, msg: Message) -> None:
432439
arb_id = f"{msg.arbitration_id:X}"
433440
if msg.is_extended_id:
434441
arb_id += "x"
435-
channel = channel2int(msg.channel)
436-
if channel is None:
437-
channel = self.channel
438-
else:
439-
# Many interfaces start channel numbering at 0 which is invalid
440-
channel += 1
441442
if msg.is_fd:
442443
flags = 0
443444
flags |= 1 << 12

can/message.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ def equals(
291291
self,
292292
other: "Message",
293293
timestamp_delta: Optional[float] = 1.0e-6,
294+
check_channel: bool = True,
294295
check_direction: bool = True,
295296
) -> bool:
296297
"""
@@ -299,6 +300,7 @@ def equals(
299300
:param other: the message to compare with
300301
:param timestamp_delta: the maximum difference in seconds at which two timestamps are
301302
still considered equal or `None` to not compare timestamps
303+
:param check_channel: whether to compare the message channel
302304
:param check_direction: whether to compare the messages' directions (Tx/Rx)
303305
304306
:return: True if and only if the given message equals this one
@@ -322,7 +324,7 @@ def equals(
322324
and self.data == other.data
323325
and self.is_remote_frame == other.is_remote_frame
324326
and self.is_error_frame == other.is_error_frame
325-
and self.channel == other.channel
327+
and (self.channel == other.channel or not check_channel)
326328
and self.is_fd == other.is_fd
327329
and self.bitrate_switch == other.bitrate_switch
328330
and self.error_state_indicator == other.error_state_indicator

test/logformats_test.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,26 @@ def test_no_triggerblock(self):
591591
def test_can_dlc_greater_than_8(self):
592592
_msg_list = self._read_log_file("issue_1299.asc")
593593

594+
def test_error_frame_channel(self):
595+
# gh-issue 1578
596+
err_frame = can.Message(is_error_frame=True, channel=4)
597+
598+
temp_file = tempfile.NamedTemporaryFile("w", delete=False)
599+
temp_file.close()
600+
601+
try:
602+
with can.ASCWriter(temp_file.name) as writer:
603+
writer.on_message_received(err_frame)
604+
605+
with can.ASCReader(temp_file.name) as reader:
606+
msg_list = list(reader)
607+
assert len(msg_list) == 1
608+
assert err_frame.equals(
609+
msg_list[0], check_channel=True
610+
), f"{err_frame!r}!={msg_list[0]!r}"
611+
finally:
612+
os.unlink(temp_file.name)
613+
594614

595615
class TestBlfFileFormat(ReaderWriterTest):
596616
"""Tests can.BLFWriter and can.BLFReader.
@@ -814,7 +834,7 @@ def test_not_crashes_with_stdout(self):
814834
printer(message)
815835

816836
def test_not_crashes_with_file(self):
817-
with tempfile.NamedTemporaryFile("w", delete=False) as temp_file:
837+
with tempfile.NamedTemporaryFile("w") as temp_file:
818838
with can.Printer(temp_file) as printer:
819839
for message in self.messages:
820840
printer(message)

test/message_helper.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
This module contains a helper for writing test cases that need to compare messages.
55
"""
66

7-
from copy import copy
8-
97

108
class ComparingMessagesTestCase:
119
"""
@@ -28,31 +26,14 @@ def assertMessageEqual(self, message_1, message_2):
2826
Checks that two messages are equal, according to the given rules.
2927
"""
3028

31-
if message_1.equals(message_2, timestamp_delta=self.allowed_timestamp_delta):
32-
return
33-
elif self.preserves_channel:
29+
if not message_1.equals(
30+
message_2,
31+
check_channel=self.preserves_channel,
32+
timestamp_delta=self.allowed_timestamp_delta,
33+
):
3434
print(f"Comparing: message 1: {message_1!r}")
3535
print(f" message 2: {message_2!r}")
36-
self.fail(
37-
"messages are unequal with allowed timestamp delta {}".format(
38-
self.allowed_timestamp_delta
39-
)
40-
)
41-
else:
42-
message_2 = copy(message_2) # make sure this method is pure
43-
message_2.channel = message_1.channel
44-
if message_1.equals(
45-
message_2, timestamp_delta=self.allowed_timestamp_delta
46-
):
47-
return
48-
else:
49-
print(f"Comparing: message 1: {message_1!r}")
50-
print(f" message 2: {message_2!r}")
51-
self.fail(
52-
"messages are unequal with allowed timestamp delta {} even when ignoring channels".format(
53-
self.allowed_timestamp_delta
54-
)
55-
)
36+
self.fail(f"messages are unequal: \n{message_1}\n{message_2}")
5637

5738
def assertMessagesEqual(self, messages_1, messages_2):
5839
"""

0 commit comments

Comments
 (0)