Skip to content

Commit e17207c

Browse files
committed
fix 218
1 parent bd26d99 commit e17207c

3 files changed

Lines changed: 17 additions & 15 deletions

File tree

can/io/asc.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,19 @@ def log_event(self, message, timestamp=None):
120120
logger.debug("ASCWriter: ignoring empty message")
121121
return
122122

123-
timestamp = (timestamp or time.time())
123+
if timestamp is None:
124+
timestamp = time.time()
125+
124126
if timestamp >= self.started:
125127
timestamp -= self.started
126128

127129
line = self.EVENT_STRING.format(time=timestamp, message=message)
130+
128131
if not self.log_file.closed:
129132
self.log_file.write(line)
130133

131134
def on_message_received(self, msg):
135+
132136
if msg.is_error_frame:
133137
self.log_event("{} ErrorFrame".format(self.channel), msg.timestamp)
134138
return
@@ -139,18 +143,18 @@ def on_message_received(self, msg):
139143
else:
140144
dtype = "d {}".format(msg.dlc)
141145
data = ["{:02X}".format(byte) for byte in msg.data]
146+
142147
arb_id = "{:X}".format(msg.arbitration_id)
143-
if msg.id_type:
144-
arb_id = arb_id + "x"
145-
timestamp = msg.timestamp
146-
if timestamp >= self.started:
147-
timestamp -= self.started
148+
if msg.is_extended_id:
149+
arb_id += "x"
148150

149151
channel = msg.channel if isinstance(msg.channel, int) else self.channel
150-
line = self.LOG_STRING.format(time=timestamp,
152+
153+
line = self.LOG_STRING.format(time=msg.timestamp,
151154
channel=channel,
152155
id=arb_id,
153156
dtype=dtype,
154157
data=" ".join(data))
158+
155159
if not self.log_file.closed:
156160
self.log_file.write(line)

test/data/example_data.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from can import Message
1111

12-
1312
# make tests more reproducible
1413
random.seed(13339115)
1514

@@ -73,12 +72,12 @@
7372

7473
TEST_MESSAGES_REMOTE_FRAMES = [
7574
Message(
76-
arbitration_id=0xDADADA, extended_id=True, is_remote_frame=False,
75+
arbitration_id=0xDADADA, extended_id=True, is_remote_frame=True,
7776
timestamp=TEST_TIME + .165,
7877
data=[1, 2, 3, 4, 5, 6, 7, 8]
7978
),
8079
Message(
81-
arbitration_id=0x123, extended_id=False, is_remote_frame=False,
80+
arbitration_id=0x123, extended_id=False, is_remote_frame=True,
8281
timestamp=TEST_TIME + .365,
8382
data=[254, 255]
8483
),
@@ -124,6 +123,5 @@ def generate_message(arbitration_id):
124123
Generates a new message with the given ID, some random data
125124
and a non-extended ID.
126125
"""
127-
data = [random.randrange(0, 2 ** 8 - 1) for _ in range(8)]
128-
msg = Message(arbitration_id=arbitration_id, data=data, extended_id=False)
129-
return msg
126+
data = bytes([random.randrange(0, 2 ** 8 - 1) for _ in range(8)])
127+
return Message(arbitration_id=arbitration_id, data=data, extended_id=False)

test/logformats_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _test_writer_and_reader(test_case, writer_constructor, reader_constructor, s
112112
for i, (read, original) in enumerate(zip(read_messages, original_messages)):
113113
try:
114114
test_case.assertEqual(read, original)
115-
test_case.assertAlmostEqual(read.timestamp, original.timestamp)
115+
test_case.assertAlmostEqual(read.timestamp, original.timestamp, places=6)
116116
except Exception as exception:
117117
# attach the index
118118
exception.args += ("test failed at index #{}".format(i), )
@@ -142,7 +142,7 @@ class TestAscFileFormat(unittest.TestCase):
142142

143143
def test_writer_and_reader(self):
144144
_test_writer_and_reader(self, can.ASCWriter, can.ASCReader,
145-
check_error_frames=False, # TODO this should get fixed, see Issue #218
145+
check_error_frames=True,
146146
check_comments=True)
147147

148148

0 commit comments

Comments
 (0)