-
Notifications
You must be signed in to change notification settings - Fork 0
/
keithleyCLI.py
286 lines (233 loc) · 7.97 KB
/
keithleyCLI.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
""" Keithley_CLI.py: Command line interface
"""
#######################################
# Imports
#######################################
import sys
import cmd
import time
import datetime
from threading import Thread
#######################################
# Class CLI
#######################################
class CLI(cmd.Cmd,Thread):
"""Command Line Interface"""
def __init__(self):
cmd.Cmd.__init__(self)
Thread.__init__(self)
self.running = True
#super(CLI,self).__init__()
self.keithleys={}
# Init logging file
value = datetime.datetime.fromtimestamp(time.time())
logfile_name = "keithleyLog_" + value.strftime('%Y_%m_%d_%H_%M') + ".txt"
self.logfile = open(logfile_name, "w", 1)
def run(self):
self.cmdloop()
def set_keithleys(self,keithleys):
""" set a map of keithley devices"""
self.keithleys= keithleys
print 'set keithleys'
for name in keithleys:
print name,keithleys[name].name
def do_exit(self,line):
"""Quit CLI"""
# Turn off the devices
for k in self.keithleys.keys():
self.keithleys[k].isKilled=True
self.logfile.close()
self.running = False
return True
def do_names(self,line):
"""Print connected Keithley devices"""
print 'There are %d Keithleys connected:'%len(self.keithleys)
k = 1
for i in self.keithleys:
print k, i
k+=1
#######################################
# do_ON / do_OFF
#######################################
def setOutput(self,name,status):
print 'Set Output %d: %s'%(status,name)
if name.upper() == 'ALL':
for k in self.keithleys:
self.setOutput(k,status)
return
if self.keithleys.has_key(name):
keithley = self.keithleys[name]
keithley.wait_for_device()
keithley.isBusy=True
try:
keithley.setOutput(status)
keithley.lastUChange = time.time()
keithley.powering_down = False
except Exception as inst:
print type(inst),inst
keithley.isBusy=False
else:
print 'cannot find %s'%name
def do_ON(self,line):
""" Set output of device to ON.
Usage: ON KeithleyName
(ON ALL to turn on all devices)
"""
self.setOutput(line,True)
def do_OFF_FAST(self,line):
""" Set output of device to OFF.
Usage: OFF_FAST KeithleyName
(OFF_FAST ALL to turn on all devices)
"""
self.setOutput(line,False)
def do_OFF(self,line):
""" Set output of device to OFF.
Usage: OFF KeithleyName
(OFF ALL to turn off all devices)
"""
try:
name = line.split()[0]
if name.upper() == 'ALL':
for k in self.keithleys:
self.do_OFF(k)
else:
self.keithleys[name].power_down()
except Exception as inst:
print type(inst),inst
#######################################
# do_FILTER
#######################################
def setFilter(self,name,status):
print 'Set Filter %d: %s'%(status,name)
if name.upper() == 'ALL':
for k in self.keithleys:
self.setFilter(k,status)
return
if self.keithleys.has_key(name):
keithley = self.keithleys[name]
keithley.wait_for_device()
keithley.isBusy=True
try:
keithley.setAverageFiltering(status)
except Exception as inst:
print type(inst),inst
keithley.isBusy=False
else:
print 'cannot find %s'%name
def do_FILTER(self,line):
""" Set filter of device.
FILTER KeithleyName status
status should be 0/1
('FILTER ALL 0/1' sets all devices)"""
try:
name = line.split()[0]
status = int(line.split()[1])
print 'do_FILTER',line,name,status
self.setFilter(name,status)
except Exception as inst:
print type(inst),inst
#######################################
# do_MANUAL
#######################################
def set_manual(self,name,status):
""" (De-)Activates manual control mode.
MANUAL KeithleyName status
status should be 0/1
('MANUAL ALL 0/1' sets all devices)"""
try:
keithley = self.keithleys[name]
except Exception as inst:
print 'cannot find keithley with name "%s"'%name
try:
keithley.set_manual(status)
except Exception as inst:
print type(inst),inst
def do_MANUAL(self,line):
try:
[name,status] = line.split()
status = int(status)
except:
print 'Wrong input for MANUAL',line
return
if name.upper() == 'ALL':
for k in self.keithleys:
self.set_manual(k,status)
else:
try:
keithley = self.keithleys[name]
keithley.set_manual(status)
except:
print 'cannot find keithley with name "%s"'%name
#######################################
# do_BIAS
#######################################
def setBias(self, name, target_bias ):
try:
if self.keithleys.has_key(name):
keithley = self.keithleys[name]
if ( (target_bias < keithley.minBias)
or (target_bias > keithley.maxBias)):
print "This bias voltage", target_bias, "is not allowed! Boundaries are: ", keithley.minBias, keithley.maxBias
return
keithley.set_target_bias(target_bias)
except Exception as inst:
print type(inst),inst
def do_BIAS(self,line):
""" Set target voltage of device.
Usage:
BIAS KeithleyName voltage"""
try:
name = line.split()[0]
target_bias = float(line.split()[1])
self.setBias( name, target_bias )
except Exception as inst:
print type(inst),inst
#######################################
# do_COMMAND
#######################################
def do_COMMAND(self,line):
"""performs any command on device"""
try:
command = line.split(None,1)
name = command [0]
if self.keithleys.has_key(name):
keithley = self.keithleys[name]
keithley.wait_for_device()
keithley.isBusy = True
try:
print 'Write to "%s" "%s"'%(name,command[1])
keithley.write(command[1])
except:
pass
keithley.isBusy = False
else:
print 'cannot find %s'%name
except Exception as inst:
print type(inst),inst
def do_read(self,line):
"""Call read for device"""
try:
name = line
keithley = self.keithleys[name]
keithley.wait_for_device()
keithley.isBusy = True
try:
print keithley.read()
except:
pass
keithley.isBusy = False
except Exception as inst:
print type(inst),inst
print Exception
#######################################
# do_NEWLOG
#######################################
def do_NEWLOG(self,line):
""" Closes the current logfile and opens a new one"""
# Close old and open new logging file
value = datetime.datetime.fromtimestamp(time.time())
logfile_name = "keithleyLog_" + value.strftime('%Y_%m_%d_%H_%M') + ".txt"
self.logfile.close()
self.logfile = open(logfile_name, "w",1)
# End of Class CLI