forked from mjmottram/leCroyComms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.py
218 lines (188 loc) · 6.91 KB
/
scope.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#
from pyvisa.vpp43 import visa_library
from pyvisa.visa_exceptions import VisaIOError
visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")
import visa
import sys
import time
class ScopeException(Exception):
"""Just an exception class
"""
def __init__(self, error):
Exception.__init__(self, error)
def chstr(channel):
"""Function to convert channel to the appropriate string.
Only needed where a function can be used for either math/channel types.
"""
if type(channel) is int:
return "C%01d" % channel
elif type(channel) is str:
return "T%s" % channel
else:
raise ScopeException("Unknown channel type")
class LeCroy684(object):
"""LeCroy LC684DL Scope.
Requires connection via GPIB.
"""
def __init__(self, debug=False):
self._dbg = debug
self._conn = None
self._conn = self._find_connection()
self._measurements = {"area": "AREA",
"fall": "FALL",
"maximum": "MAX",
"minimum": "MIN",
"rise": "RISE",
"width": "WID"}
def __del__(self):
"""Print any debug messages and clear logs.
"""
if self._dbg and self._conn:
# self._write("GTL")
try:
print self._conn.ask("CHL? CLR")
except VisaIOError:
# timeout; fine.
pass
def _find_connection(self):
"""Find the appropriate GPIB connection
"""
instrument_list = []
print visa.get_instruments_list()
for instrument in visa.get_instruments_list():
if instrument.startswith("GPIB"):
temp_conn = visa.instrument(instrument)
try:
response = temp_conn.ask("*IDN?")
except VisaIOError:
# Need to release connection somehow!
print "Cannot connect with %s" % instrument
temp_conn.write("GTL")
raise
if response.startswith("*IDN LE"):
instrument_list.append(instrument)
if len(instrument_list)==0:
raise ScopeException("Cannot find LeCroy684 GPIB")
elif len(instrument_list)==1:
return visa.instrument(instrument_list[0])
else:
print "Select instrument:"
for i, instrument in enumerate(instrument_list):
temp_conn = visa.instrument(instrument)
print "\t%s:\t%s" % (i, temp_conn.ask("*IDN?"))
number = raw_input("Make selection [number]: ")
return visa_instrument(instrument_list[number])
def _debug(self, info):
"""Basic logging
"""
if self._dbg is True:
print info
def _ask(self, request):
"""A scope request. But with some logging.
"""
self._debug(request)
return self._conn.ask(request)
def _write(self, send):
"""A scope write. But with some logging.
"""
self._debug(send)
self._conn.write(send)
def reset_averaging(self, channel):
"""Reset averaging for the given (math) channel
"""
self._write("T%s:FRST" % channel)
def set_averaging(self, math_ch, scope_ch, nave):
"""Set channel T<math> to average T<scope>
"""
self._write("T%s:DEF EQN,'AVGS(C%01d)',SWEEPS,%d" % (math_ch, scope_ch, nave))
#def get_num_averages(self, math_ch):
# """Get the number of averages.
# """
# return self._ask("T%s?" % (math_ch))
def set_trigger_mode(self, mode):
"""Set the trigger mode, options are single, normal
"""
request = None
if mode=="normal":
request = "NORM"
elif mode=="single":
request = "SINGLE"
else:
raise scope.ScopeException("Unknown trigger mode %s" % mode)
self._write("TRMD %s" % request)
def set_trigger(self, channel, level, falling=False):
"""Set the trigger parameters for the scope.
Still need to arm (enable_trigger).
"""
request = ""
request += "C%01d:TRLV %.3fV; " % (channel, level)
if falling is False:
request += "C%01d:TRSL POS" % channel
else:
request += "C%01d:TRSL NEG" % channel
self._write(request)
def enable_trigger(self):
"""Just set the trigger
"""
return self._write("ARM;")
def wait_triggered(self, channel):
"""Set the trigger on the scope and wait before acquiring waveform.
"""
# TODO: add timeout
return self._ask("ARM;WAIT;C%01d:WF?" % (channel))
def get_waveform(self, channel):
"""Just get the waveform
"""
return self._ask("%s:WAVEFORM? ALL" % chstr(channel))
def save_waveform(self, channel, filename):
"""Get and save a waveform
"""
f = open(filename, "w")
f.write(self.get_waveform(channel))
f.close()
def set_x_scale(self, tdiv):
"""Set all channels to tdiv
"""
self._write("TDIV %E" % (tdiv))
def set_y_scale(self, channel, vdiv):
"""Set channel to vdiv
"""
self._write("%s:VDIV %E" % (chstr(channel), vdiv))
def set_y_position(self, channel, vpos):
"""Set the vertical position
*** Math function positions must be set with DIVS! ***
"""
if type(channel) is int:
self._write("%s:OFST %f" % (chstr(channel), vpos))
else:
self._write("%s:VPOS %f" % (chstr(channel), vpos))
def set_cursor(self, axis, channel, pos1, pos2):
"""Set the cursors, must use div references.
"""
if axis=="x" or axis=="X":
csr = "HABS"
elif axis=="y" or axis=="Y":
csr = "VABS"
else:
raise ScopeException("Unknown axis %s" % axis)
command = "%s:CRST %s,%f,%s,%f" % (chstr(channel), csr, pos1, csr, pos2)
self._write(command)
def measure(self, channel, parameter):
"""Make a measurement
"""
if parameter in self._measurements:
response = self._ask("%s:PAVA? %s" % (chstr(channel), self._measurements[parameter]))
bits = response.split(",") # returns measurement, value unit, OK/NOTOK
value = None
try:
value = float(bits[1].split()[0])
units = str(bits[1].split()[1])
except:
# test to see if it's undefined
if "UNDEF" in bits[1]:
value = -9999
else:
raise ScopeException("Unknown parameter type %s" % parameter)
return value
else:
raise ScopeException("Unknown parameter type %s" % parameter)