-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.py
More file actions
79 lines (68 loc) · 1.94 KB
/
Copy pathplotter.py
File metadata and controls
79 lines (68 loc) · 1.94 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
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from time import sleep
import serial
import sys
import glob
filename = input('Enter filename to save: ')
f = open(filename, 'w+')
f.close()
def serial_ports():
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*') + glob.glob('/dev/cu.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
ports = serial_ports()
for i, s in enumerate(ports):
print('{:d}. port: {:s}'.format(i + 1, s))
sp = int(input('Enter the number of the serial port: '))
ser = serial.Serial()
ser.port = ports[sp - 1]
ser.baudrate = 115200
ser.timeout = 10
ser.open()
while not ser.is_open:
print('Serial port could not open!\n')
sleep(1)
print('Serial port open! Configuration:\n')
print(ser, "\n")
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ys = [[], [], [], []]
xs = []
t = 0
def animate(i, xs, ys):
line = ser.readline().decode("utf-8").strip()
values = line.split(',')
f = open(filename, 'a+')
f.write(line + '\n')
print(line)
f.close()
xs.append(i)
global t
t += 1
ax.clear()
for i in range(4):
ys[i].append(int(values[i].strip()))
ax.plot(xs, ys[i], label='{:d}. Photodiode'.format(i + 1))
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('Group 2 RedStone')
plt.ylabel('RAW Values')
plt.xlabel('Time')
plt.legend()
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=10)
plt.show()