-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial-plot 1 copy.py
59 lines (44 loc) · 1.66 KB
/
serial-plot 1 copy.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
import numpy as np
import matplotlib.pyplot as plt
import serial
import serial.tools.list_ports
import re
# plt.show()
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
plots = []
counter = 0
serialDevices = serial.tools.list_ports.comports()
print("Devices: ")
for device in serialDevices:
print(device)
# print(device.device)
with serial.Serial(device.device, 9600, timeout=1) as ser:
while True:
# x = ser.read() # read one byte
s = ser.read(1000) # read up to ten bytes (timeout)
# line = ser.readlines() # read a '\n' terminated line
values = re.findall("\d+[.,]?\d+", s.decode())
floatVals = [float(value) for value in values]
print( floatVals )
# IF THERE ARE NOT ENOUGH PLOTS FOR EVERY VALUE: MAKE MORE PLOTS!
while( len(floatVals) > len( plots ) ):
newPlot, = ax.plot([], [])
plots.append( newPlot )
if( len(floatVals) > 0 ):
print( "plots", plots )
counter += 1
for i in range(len(floatVals)):
# newPlot.set_ydata( range( counter ) )
plots[i].set_ydata( np.append(plots[i].get_ydata(), floatVals[i]) )
plots[i].set_xdata( range( counter ) )
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
fig.canvas.flush_events()
# plt.draw()
# plt.pause(0.1)
print(s.decode())
# add this if you don't want the window to disappear at the end
# plt.show()