-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscilloscope_2ch.py
executable file
·137 lines (112 loc) · 2.87 KB
/
oscilloscope_2ch.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /usr/bin/python
'''
37.6Hz@115200 100 1 1
'''
import serial,sys,time
from macrobull.misc import serialChecker
from macrobull.dynamicPlot import dynamicFigure,defaultProcFunc,average, timeDiv
BUFFLEN=16
plotArgs=dict(alpha=0.5, linewidth=3.2, marker='o', markersize=4.4)#,color="green")
procFunc=[]#[lambda x,y:average(0.2,x,y), ]
buff=''
baud=9600
intv=50
cnt='4x'
sl=1
BLKSIZE = 4
MAXBUFF = 400
VLO=int(12e3)
dev=serialChecker(True,'USB','AMA')
ser = serial.Serial(dev, baud)
ser.flush()
ser.flushInput()
ser.flushOutput()
ser.close()
if len(sys.argv)>1: baud = int(sys.argv[1])
if len(sys.argv)>2: intv = int(sys.argv[2])
#if len(sys.argv)>3: cnt = int(sys.argv[3]) % 10
if len(sys.argv)>3: cnt = sys.argv[3]+'x'
if len(sys.argv)>4: sl = float(sys.argv[4])
ser = serial.Serial(dev, baud, timeout=intv /1000)
print "baudrate={}".format(ser.getBaudrate())
ser.write(cnt);
flag=True
tStamp1=tStamp2=time.time()
to1=to2=0
def safeVal(s):
global flag
r=None
try:
r=int(s)
except ValueError,e:
flag=False
pass
return r
def adjustTS(t,to,ts):
ot=t
t+=(t<to)*VLO
t+=(t<to)*VLO
s=float(t-to)/VLO+ts
return s,ot,s
def pause(event):
global tStamp1, tStamp2
if event.key=='i':
df.pause=not(df.pause)
tStamp1, tStamp2 = time.time()
def getData(fs):
global buff, flag
global to1, to2, tStamp1, tStamp2
w=ser.inWaiting()
#print(w)
if w>0:
data=ser.read(w)
buff+=data
th1,th2=[],[]
ch1,ch2=[],[]
ts=te=0
print time.time(),':'
print len(buff)
#while len(buff)>MAXBUFF : buff=buff[MAXBUFF:]
while buff.find('S')>0:
buff=buff[buff.find('S')-1:]
pe=buff.find('E')
if pe==-1: break
if pe>len(buff)-6: break
flag=True
ts=safeVal(buff[2:8])
te=safeVal(buff[pe+1:pe+7])
if flag:
tmpch=[]
for i in range(8,pe-1,BLKSIZE):
v=safeVal(buff[i+1:i+4])
if v: tmpch.append(v *2.5/1024)
if buff[0]=='A':
ch1+=tmpch
#print time.time(), (ts,to1,tStamp1), adjustTS(ts, to1, tStamp1)
(ts,to1,tStamp1) = adjustTS(ts, to1, tStamp1)
(te,to1,tStamp1) = adjustTS(te, to1, tStamp1)
th1+=list(timeDiv(ts,te,len(tmpch)))
if buff[0]=='B':
ch2+=tmpch
(ts, to2, tStamp2) =adjustTS(ts, to2, tStamp2)
(te, to2, tStamp2) =adjustTS(te, to2, tStamp2)
th2+=list(timeDiv(ts,te,len(tmpch)))
buff=buff[pe+7:]
#print len(data),data,buff
#print th1, ch1
#df.appendData([1], 'Ch1', 111, x=[time.time()], procFunc=procFunc, plotArgs=plotArgs)
df.appendData(ch1, 'Ch1', 111, x=th1, procFunc=procFunc, plotArgs=plotArgs)
df.appendData(ch2, 'Ch2', 111, x=th2, procFunc=procFunc, plotArgs=plotArgs)
if df.check_subplot(111, "Channels"):
df.subplots[111].set_ylabel("Voltage\V")
ser.write(cnt);
df=dynamicFigure(updateInterval=intv,screenLen=sl)
df.keyHandler=pause
#df=dynamicFigure(updateInterval=1000,screenLen=60)
df.newData=getData
try:
df.run()
except BaseException,e:
print(e)
ser.close()
print('Exited')