-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice_manager.py
401 lines (311 loc) · 11.9 KB
/
device_manager.py
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
This file is responsible for handling the COM ports
and serial devices.
It uses pySerial and has a loop running on a thread.
"""
import threading
import time
import numpy as np
import serial
import serial.tools.list_ports
from globals import GRAPH_BEGINNING, START_REC, END_REC
class DeviceManager():
"""
Handles all of the serial communications:
Uploads
Compiles
Reads
Writes
Connect
Disconnect
Attributes:
parent (MainGUI) : the main gui the app is running off of
device (Serial) : the serial device the app connects to
port (str) : the port the device is currently connected to
last_port (str) : the port the device was connected to
error (str) : the error if there was an error connecting
target (str) : the device to auto connect to
connected (bool) : wether or not a device is connected
auto_connect (bool) : wether the GUI should try to auto connect if there is a port
raw_cummulative_data (str) : the raw data from the gui that is continually added
raw_data (list) : the data split into lines
Methods:
send:
Sends the message given from the gui to the
connected device
device_parse_data:
Parses the data buffer into self.raw_data
device_data:
Gets the raw data from either the emulated device or the actual
arduino device that's connected
debug_device:
Prints out the raw data and the data on terminal
threaded_get_raw_data:
Raw input data from the serial device
Connects device and then while loops with no time.sleep
Checks for "(" in the data as all sidekick messages will have one
terminate_device:
Terminates the while True loop to disconnect the device
then sets the variables back to the initial states so
that there is no data
connect_serial:
Connects the device in order to read the data coming
from the SideKick/Teensy/Arduino
also stars the thread to get the data from the newly connected
device
scan_avaliable_ports:
Gets all avaliable com ports and auto connects to devices
reset_difference:
Sets self.change_in_data to 0 after the value has been used.
device_emulator:
Gives dummy inputs to dev the GUI without physical hardware!
"""
def __init__(self, parent=None):
self.parent = parent
self.device = None
self.port = None
self.last_port = None
self.error = None
self.target = None
self.connected = False
self.auto_connect = False
self.raw_cummulative_data = ""
self.raw_data = []
self.failed_recv = 0
self.change_in_data = []
self.emulated_input = b""
self.emulating_counter = 0
self.emulating = False
self.start_rec = False
self.end_rec = False
def __call__(self):
self.device = None
self.port = None
self.error = None
self.target = None
self.connected = False
self.auto_connect = True
self.raw_cummulative_data = ""
self.raw_data = []
self.failed_recv = 0
self.change_in_data = []
self.emulated_input = b""
self.emulating_counter = 0
self.emulating = False
def send(self, message:str):
"""
Sends the message given from the gui to the
connected device
Args:
message (str): the message to send to the device
"""
if self.device:
print("Sending...")
self.device.write(f"{message}\n".encode("UTF-8"))
print("Sent")
def device_parse_data(self, buffer:str) -> str:
"""
Parses the data buffer into self.raw_data
Args:
buffer (str): the collective data
Returns:
str: the buffer of incomplete mesages
"""
try:
decoded_buffer = buffer.decode("UTF-8")
except UnicodeDecodeError:
decoded_buffer = "ERROR"
return buffer
split_data = decoded_buffer.split("\r\n")
if decoded_buffer.endswith("\r\n"):
for item in split_data[1:]:
self.raw_data.append(item)
buffer = (split_data[0] + "\r\n").encode("UTF-8")
else:
for item in split_data[1:-1]:
self.raw_data.append(item)
buffer = (split_data[0] + "\r\n" + split_data[-1]).encode("UTF-8")
if START_REC.encode("UTF-8") in buffer:
self.start_rec = True
elif END_REC.encode("UTF-8") in buffer:
self.end_rec = True
buffer = buffer.replace(START_REC.encode("UTF-8"), b"")
buffer = buffer.replace(END_REC.encode("UTF-8"), b"")
self.raw_data = list(filter(None, self.raw_data))
return buffer
def parse_buffer(self, raw_data):
"""
Processes raw data to ensure that graph names that are the same are
not on the same line and so inserts new lines where they should be
in case they are skipped or the user forgot.
Args:
raw_data (str): the raw data that may be missing newlines.
Returns:
str: the raw data with new lines
"""
marker = GRAPH_BEGINNING.encode("UTF-8")
output_list = []
new_line_data = raw_data.split(b"\r\n")
for line in new_line_data:
split_data = line.split(marker)
graph_keys = []
for i, data in enumerate(split_data):
if not line.startswith(marker) and i == 0:
continue
if data.count(b",") > 1:
split = data.split(b",")
if split[0]+b","+split[1] not in graph_keys:
graph_keys.append(split[0]+b","+split[1])
else:
split_data[i-1] = split_data[i-1]+b"\r\n"
graph_keys = []
output_list.append(marker.join(split_data))
return b"\r\n".join(output_list)
def device_data(self) -> str:
"""
Gets the raw data from either the emulated device or the actual
arduino device that's connected
Returns:
str: raw data from device
"""
try:
if not self.emulating:
raw_data = self.device.read_all()
else:
raw_data = self.emulated_input
self.emulated_input = b""
self.failed_recv = 0
except serial.SerialException:
raw_data = ""
self.failed_recv += 1
return raw_data
def debug_device(self, raw_data:str):
"""
Prints out the raw data and the data on terminal
Args:
raw_data (str): the raw data through the serial port
"""
self.raw_cummulative_data += raw_data.replace(b"\r\n", b"/n").decode("UTF-8")
print("Raw data cummulative >>> " + self.raw_cummulative_data)
string = ""
for item in self.raw_data:
string += item.replace("\r\n", "")
print("Data on terminal >>> " + string)
def threaded_get_raw_data(self, port:str, baud:int, dev=False):
"""
Raw input data from the serial device
Connects device and then while loops with no time.sleep
Checks for "(" in the data as all sidekick messages will have one
Args:
port (string): the port to connect to
baud (int): the baud rate of the connected device
dev (bool): whether the app is emulating or not
"""
self.port = port
self.raw_cummulative_data = ""
if not dev:
try:
self.device = serial.Serial(port, baud)
#initial_data = b""
#while not initial_data:
# initial_data = self.device_data()
except serial.SerialException as error:
self.error = str(error).replace("(","\n").replace(")","\n")
print("<<< ERROR >>> " + self.error)
return
self.connected = True
buffer = b""
while self.connected:
raw_data = self.device_data()
if self.failed_recv > 10:
break
if not (raw_data != b"" and isinstance(raw_data, bytes)):
continue
buffer += raw_data
buffer = buffer.replace(b"\r\n\r\n", b"\r\n")
buffer = self.parse_buffer(buffer)
if buffer.startswith(b"\r\n"):
buffer = buffer[2:]
buffer = self.device_parse_data(buffer)
if self.device:
self.device.close()
self()
self.target = self.last_port
self.auto_connect = False
def terminate_device(self):
"""
Terminates the while True loop to disconnect the device
then sets the variables back to the initial states so
that there is no data.
"""
while self.connected:
self.connected = False
self.last_port = self.port
time.sleep(0.2)
def connect_serial(self, port="COM1", baud=115200, dev=False):
"""
Connects the device in order to read the data coming
from the SideKick/Teensy/Arduino
also stars the thread to get the data from the newly connected
device
Args:
port (string): the com port the device is connected to e.g. "COM1"
baud (int): the baud rate of the connected board
dev (bool): emulates a device if the user wants
"""
self()
if dev:
self.emulating = True
print("<<< WARNING >>> EMULATING DEVICE")
emulator = threading.Thread(target=self.device_emulator)
emulator.start()
else:
self.auto_connect = True
self.target = port
get_data = threading.Thread(
target=self.threaded_get_raw_data, args=(port, baud, dev),)
get_data.start()
def scan_avaliable_ports(self, dev=False) -> list:
"""
Gets all avaliable com ports and auto connects to devices.
Args:
dev (bool): whether or not to show the dev option of emulate
Returns:
list : avaliable com ports for pyserial
"""
available_ports = ["emulate"] if dev else []
for port in serial.tools.list_ports.comports():
available_ports.append(port.device)
# Auto connection
if not self.connected and self.auto_connect:
if self.target is not None and (self.target in available_ports):
self.connect_serial(self.target)
time.sleep(1)
return available_ports
def reset_difference(self, data:list):
"""
Sets self.change_in_data to 0 after the value
has been used.
Args:
data (list): the data that is saved
"""
self.change_in_data = self.change_in_data[len(data):]
def device_emulator(self):
"""
Gives dummy inputs to dev the GUI without physical
hardware!
Designed to be run on a thread to emulate the device
being disconnected from the whole system.
"""
while self.emulating:
if self.emulating_counter % 100 > 49:
self.emulated_input += bytes("l058~Step Func,1,1zC43_","UTF-8")
else:
self.emulated_input += bytes("l058~Step Func,1,-1zC43_","UTF-8")
self.emulated_input += bytes(
f"l058~Sin(x),1,{np.sin(self.emulating_counter*3.1415/180)}zC43_",
"UTF-8")
self.emulated_input += bytes(
f"{self.emulating_counter}\r\n", "UTF-8")
self.emulating_counter += 1
time.sleep(0.005)