Repository navigation
Expand file tree
/
Copy pathinterface.py
More file actions
executable file
·285 lines (239 loc) · 9.46 KB
/
Copy pathinterface.py
File metadata and controls
executable file
·285 lines (239 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
# The MIT License (MIT)
#
# Copyright (c) 2016 Ivor Wanders
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
from collections import namedtuple
import json
import logging
import serial
import sys
import threading
import time
import queue
from . import message
logger = logging.getLogger(__name__)
class SerialInterface(threading.Thread): # Also known as 'SerialMan!'.
"""
Class to handle communication with the serial port. It uses a separate
thread to handle the communication with the serial port. Internally the
object uses two queues, one to be sent on the serial port and one that
contains the messages received from the serial port.
Placing messages on the queue to be sent is done with the `put_message`
method. Reading received messages is done with the `get_message`
method.
:param packet_size: The size of all messages in bytes.
:type packet_size: int
"""
def __init__(self, packet_size=64):
super().__init__()
self.ser = None
self.running = False
self.packet_size = packet_size
self.rx = queue.Queue()
self.tx = queue.Queue()
def connect(self, serial_port, baudrate=9600, **kwargs):
"""
Connects the object to a serial port.
:param serial_port: The path to the serial port to connect to.
:type serial_port: str
:param baudrate: The baudrate to use.
:type baudrate: int
"""
packet_read_timeout = self.packet_size * (1.1 / baudrate)
try:
self.ser = serial.Serial(serial_port,
baudrate=baudrate,
timeout=packet_read_timeout,
**kwargs)
logger.debug("Succesfully connected to {}.".format(serial_port))
return True
except serial.SerialException as e:
logger.warn("Failed to connect to {}".format(serial_port))
return False
def stop(self):
"""
Cleanly shuts down the running thread and closes the serial port.
"""
self.running = False
time.sleep(0.001)
if (self.ser):
self.ser.close()
def _process_rx(self):
# try to read message from serial port
try:
if (self.ser.inWaiting()):
buffer = bytearray(self.packet_size)
d = self.ser.readinto(buffer)
# Did we get the correct number of bytes? If so queue it.
if (d == self.packet_size):
msg = message.Msg.read(buffer)
self.rx.put_nowait(msg)
else:
logging.warn("Received incomplete packet "
" discarded ({}).".format(buffer))
except (serial.SerialException, OSError, IOError) as e:
self.ser.close()
self.ser = None
def _process_tx(self):
# try to put a message on the serial port from the queue
try:
msg = self.tx.get_nowait()
except queue.Empty:
pass # there was no data there.
return
if (self.ser is None):
logging.warn("Trying to send on a closed serial port.")
return
try:
logger.debug("Processing {}".format(msg))
self.ser.write(bytes(msg))
except serial.SerialException:
self.ser.close()
self.ser = None
def run(self):
# this method is called when the thread is started.
self.running = True
while (self.running):
# small sleep to prevent this loop from running too fast.
time.sleep(0.001)
self._process_tx() # read from the tx queue
if (self.ser is None):
continue
self._process_rx() # read from serial port
def is_serial_connected(self):
"""
Returns whether this object is connected to a serial port.
:returns: boolean
"""
return True if (self.ser is not None) and (
self.ser.isOpen()) else False
def get_serial_parameters(self):
"""
Returns a dictionary holding the device and baudrate of the current
serial connection.
:returns: dict containing a "device" and "baudrate" field.
"""
return {"device": self.ser.port, "baudrate": self.ser.baudrate}
def put_message(self, message):
"""
Places a message on the queue that is to be sent to the serial
port.
:param message: The message to be transmitted on the serial port.
:type message: Some object which is a valid input argument to
the `bytes` function.
"""
self.tx.put_nowait(message)
def get_message(self):
"""
Gets a message from the queue that is received on the serial port.
:returns: A `bytes` instance.
"""
try:
msg = self.rx.get_nowait()
return msg
except queue.Empty:
return None
# for command line tool
def wait_for_message(self):
while(True):
try:
time.sleep(0.01)
m = self.get_message()
if (m):
break
except KeyboardInterrupt:
break
return m
if __name__ == "__main__":
# Create a simple commandline interface.
parser = argparse.ArgumentParser(description="Control MCU at serial port.")
parser.add_argument('--port', '-p', help="The serial port to connect to.",
default="/dev/ttyACM0")
parser.add_argument('--verbose', '-v', help="Print all communication.",
action="store_true", default=False)
parser.add_argument('--listen', '-l',
help="Continue listening to communication",
action="store_true", default=False)
subparsers = parser.add_subparsers(dest="command")
# add subparsers for each command.
for i in range(0, len(message.msg_type_name)):
command = message.msg_type_name[i]
command_parser = subparsers.add_parser(command)
if (command.startswith("set_") or command.startswith("action_")):
if (i in message.msg_type_field):
fieldname = message.msg_type_field[i]
tmp = message.Msg()
tmp.msg_type = i
config = str(getattr(tmp, fieldname)).replace("'", '"')
helpstr = "Json representing configuration {}".format(config)
command_parser.add_argument('config', help=helpstr)
# parse the arguments.
args = parser.parse_args()
# no command
if (args.command is None):
parser.print_help()
parser.exit()
sys.exit(1)
# create a message of the right type to be sent.
msg = message.Msg()
msg.msg_type = getattr(msg.type, args.command)
# create the interface and connect to a serial port
a = SerialInterface(packet_size=message.PACKET_SIZE)
a.connect(args.port)
a.start() # start the interface
time.sleep(1)
# we are retrieving something.
if (args.command.startswith("get_")):
if (args.verbose):
print("Sending: {}".format(bytes(msg)))
a.put_message(msg)
print("Waiting for message.")
m = a.wait_for_message()
if (args.verbose):
print("Retrieved: {}".format(bytes(m)))
print(m)
# sending something
if (args.command.startswith("set_") or args.command.startswith("action_")):
command_id = getattr(message.msg_type, args.command)
if (command_id in message.msg_type_field):
fieldname = message.msg_type_field[command_id]
d = {fieldname: json.loads(args.config)}
msg.from_dict(d)
print("Sending {}".format(msg))
if (args.verbose):
print("Sending: {}".format(bytes(msg)))
# send the message and wait until it is really gone.
a.put_message(msg)
while(not a.tx.empty()):
time.sleep(0.1)
if (args.listen):
while(True):
try:
m = a.wait_for_message()
if (args.verbose):
print("Retrieved: {}".format(bytes(m)))
print(m)
except KeyboardInterrupt:
break
a.stop()
a.join()
sys.exit(0)