forked from girish946/plot-cat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_plot.py
117 lines (87 loc) · 3.13 KB
/
live_plot.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from plotcat import *
import sys
import serial
import argparse
import time
if sys.version_info[0] < 3:
import thread
else:
import _thread as thread
def read_from_serial(ser, data):
while True:
try:
temp = ser.readline()
try:
data.append(int(temp))
data.pop(0)
time.sleep(0.0001)
except ValueError:
pass
except AttributeError as Ae:
pass
except TypeError:
pass
except:
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='tool for plotting live\
serial data')
parser.add_argument('-d',
'--SerialDevice',
type=str,
default='/dev/ttyUSB0',
help='serial device from which the input is coming.')
parser.add_argument('-b',
'--Baudrate',
type=int,
default=9600,
help='the baudrate of serial device.')
parser.add_argument('-n',
'--Samples',
type=int,
default=1000,
help='number of samples to be plotted.')
parser.add_argument('-ymin',
'--Yminimum',
type=int, default=0,
help='lower limit of Y-Axis')
parser.add_argument('-ymax',
'--Ymaximum',
type=int,
default=1024,
help='upper limit of Y-Axis')
parser.add_argument('-p',
'--Plots',
type=int,
default=1,
help='total number of subplots to be created')
parser.add_argument('-r',
'--Rows',
type=int,
default=1,
help='number of rows on the figure.')
parser.add_argument('-c',
'--Cols',
type=int,
default=1,
help='number of columns on the figure.')
parser.add_argument('-t',
'--Titles',
nargs='+',
type=str,
default=['serial-graph'],
help='titles for plots.')
args = parser.parse_args()
data = [0 for i in range(0, args.Samples)]
p = plotter(number_of_samples=args.Samples, total_plots=args.Plots,
rows=args.Rows, cols=args.Cols, y_low_lim=args.Yminimum,
y_high_lim=args.Ymaximum, names=args.Titles)
@p.plot_self
def setval():
for i in range(len(p.lines)):
p.lines[i][0].set_data(p.currentAxis[i], data)
ser = serial.Serial(args.SerialDevice, args.Baudrate)
thread.start_new_thread(read_from_serial, (ser, data, ))
p.set_call_back(setval)