Skip to content

Commit 2ca71e0

Browse files
committed
Convert Message.timestamp's seconds to the serial protocols milliseconds
1 parent 8617f92 commit 2ca71e0

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

can/interfaces/serial/serial_can.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ def send(self, msg, timeout=None):
7575
This parameter will be ignored. The timeout value of the channel is
7676
used.
7777
"""
78-
if isinstance(msg.timestamp, float):
79-
msg.timestamp = int(msg.timestamp)
78+
8079
try:
81-
timestamp = struct.pack('<I', msg.timestamp)
80+
timestamp = struct.pack('<I', self.convert_to_integer_milliseconds(msg.timestamp))
8281
except Exception:
8382
raise ValueError('Timestamp is out of range')
8483
try:
@@ -97,12 +96,15 @@ def send(self, msg, timeout=None):
9796
byte_msg.append(0xBB)
9897
self.ser.write(byte_msg)
9998

99+
@staticmethod
100+
def convert_to_integer_milliseconds(msg_timestamp):
101+
return int(msg_timestamp * 1000)
102+
100103
def recv(self, timeout=None):
101104
"""
102105
Read a message from the serial device.
103106
104-
:param timeout: # byte_msg = bytes([0xAA]) + timestamp + dlc + a_id + msg.data + \
105-
# bytes([0xBB])
107+
:param timeout:
106108
This parameter will be ignored. The timeout value of the channel is
107109
used.
108110
:returns:
@@ -135,10 +137,8 @@ def recv(self, timeout=None):
135137
rxd_byte = ord(self.ser.read())
136138
if rxd_byte == 0xBB:
137139
# received message data okay
138-
return Message(timestamp=timestamp, arbitration_id=arb_id,
139-
dlc=dlc, data=data)
140-
else:
141-
return None
140+
return Message(timestamp=timestamp/1000,
141+
arbitration_id=arb_id,
142+
dlc=dlc,
143+
data=data)
142144

143-
else:
144-
return None

examples/serial_com.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@
2222
import threading
2323

2424

25-
def time_millis():
26-
return int(round(time.time() * 1000))
27-
28-
2925
def send_cyclic(bus, msg, stop_event):
3026
print("Start to send a message every 1s")
31-
start_time = time_millis()
27+
start_time = time.time()
3228
while not stop_event.is_set():
33-
msg.timestamp = time_millis() - start_time
29+
msg.timestamp = time.time() - start_time
3430
bus.send(msg)
3531
print("tx: {}".format(tx_msg))
3632
time.sleep(1)

test/serial_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def reset(self):
5151

5252

5353
class SimpleSerialTest(unittest.TestCase):
54+
MAX_TIMESTAMP = 0xFFFFFFFF / 1000
5455

5556
def setUp(self):
5657
self.patcher = patch('serial.Serial')
@@ -117,16 +118,18 @@ def test_rx_tx_max_timestamp(self):
117118
"""
118119
Tests the transfer with the highest possible timestamp
119120
"""
120-
msg = can.Message(timestamp=0xFFFFFFFF)
121+
122+
msg = can.Message(timestamp=self.MAX_TIMESTAMP)
121123
self.bus.send(msg)
122124
msg_receive = self.bus.recv()
123125
self.assertEqual(msg, msg_receive)
126+
self.assertEqual(msg.timestamp, msg_receive.timestamp)
124127

125128
def test_rx_tx_max_timestamp_error(self):
126129
"""
127130
Tests for an exception with an out of range timestamp (max + 1)
128131
"""
129-
msg = can.Message(timestamp=0xFFFFFFFF+1)
132+
msg = can.Message(timestamp=self.MAX_TIMESTAMP+1)
130133
self.assertRaises(ValueError, self.bus.send, msg)
131134

132135
def test_rx_tx_min_timestamp(self):
@@ -137,6 +140,7 @@ def test_rx_tx_min_timestamp(self):
137140
self.bus.send(msg)
138141
msg_receive = self.bus.recv()
139142
self.assertEqual(msg, msg_receive)
143+
self.assertEqual(msg.timestamp, msg_receive.timestamp)
140144

141145
def test_rx_tx_min_timestamp_error(self):
142146
"""

0 commit comments

Comments
 (0)