-
Notifications
You must be signed in to change notification settings - Fork 0
/
HR_Stripchart.py
69 lines (59 loc) · 1.54 KB
/
HR_Stripchart.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
# -*- coding: utf-8 -*-
"""
"""
import serial
import serial.tools.list_ports
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import sys, time, math
PORT = 'COM10'
xsize=100
try:
ser.close();
except:
print();
try:
ser = serial.Serial(PORT, 115200, timeout=100)
except:
print ('Serial port %s is not available' % PORT);
portlist=list(serial.tools.list_ports.comports())
print('Trying with port %s' % portlist[0][0]);
ser = serial.Serial(portlist[0][0], 115200, timeout=100)
ser.isOpen()
#while 1 :
# strin = ser.readline();
# print(strin.decode('ascii'));
def data_gen():
t = data_gen.t
while True:
t+=1
strin = ser.readline();
y= float (strin);
# y = strin.decode('ascii');
yield t, y
def run(data):
# update the data
t,y = data
if t>-1:
xdata.append(t)
ydata.append(y)
if t>xsize: # Scroll to the left.
ax.set_xlim(t-xsize, t)
line.set_data(xdata, ydata)
return line,
def on_close_figure(event):
sys.exit(0)
data_gen.t = -1
fig = plt.figure()
fig.canvas.mpl_connect('close_event', on_close_figure)
ax = fig.add_subplot(111)
line, = ax.plot([], [], lw=2)
ax.set_ylim(-5, 200)
ax.set_xlim(0, xsize)
ax.grid()
xdata, ydata = [], []
# Important: Although blit=True makes graphing faster, we need blit=False to prevent
# spurious lines to appear when resizing the stripchart.
ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=100, repeat=False)
plt.show()