-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialtest.py
48 lines (43 loc) · 1.17 KB
/
serialtest.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
# from time import sleep
# import serial
# ser = serial.Serial('COM3', 9600) # Establish the connection on a specific port
# res = ser.read()
# print(res)
import serial
import time
import csv
import matplotlib
matplotlib.use("tkAgg")
import matplotlib.pyplot as plt
import numpy as np
ser = serial.Serial('COM3')
ser.flushInput()
plot_window = 20
y_var = np.array(np.zeros([plot_window]))
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(y_var)
while True:
try:
ser_bytes = ser.readline()
try:
decoded_bytes = float(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
print(decoded_bytes)
except:
continue
with open("test_data.csv","a") as f:
writer = csv.writer(f,delimiter=",")
writer.writerow([time.time(),decoded_bytes])
if decoded_bytes < 0.0:
y_var = np.append(y_var,0)
else:
y_var = np.append(y_var,decoded_bytes)
y_var = y_var[1:plot_window+1]
line.set_ydata(y_var)
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
fig.canvas.flush_events()
except:
print("Keyboard Interrupt")
break