-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeithley_control.py
More file actions
executable file
·297 lines (264 loc) · 10.7 KB
/
keithley_control.py
File metadata and controls
executable file
·297 lines (264 loc) · 10.7 KB
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
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
import time
import keithley_serial as ks
import numpy as np
"""
The keithley object that will manage the transmission of data and commands.
NOTES:
- Timing of sweep steps appears to be accurate only to a couple of ms.
If you request a 0-10V sweep with 1V steps amd 1 ms delays, you get
increments of 1V, but the times vary up to a few ms. Max, 08/12/2025.
"""
class Keithley:
# It would be good to have a class variable that prevented the
# creation of more than one keithley object at a time!
# This is it, but currently it does nothing
keithleyExists = False
def __init__(self, port='/dev/tty.USA19H1462P1.1'):
self.ser = ks.start_serial(port=port)
if self.check_ready():
keithleyExists = True
self.ser.timeout = None
# Now run start up commands
self.run_start_up_commands()
else:
print("Couldn't connect to Keithley...")
def run_start_up_commands(self):
for com in start_up_commands:
self.send_command(com)
time.sleep(.01)
def check_ready(self):
ready = ks.write_and_read(self.ser,'*OPC?')
if ready == '1 \n':
return True
else:
return False
def open_serial(self):
ks.close_serial(self.ser)
def close_serial(self):
ks.close_serial(self.ser)
def send_command(self, command):
if self.ser.isOpen():
response = ks.write(self.ser, command)
time.sleep(.1)
if response != '':
return response
else:
return None
else:
print("Port is closed...")
def get_response(self, command, pause=.25):
if self.ser.isOpen():
response = ks.write_and_read(self.ser, command, pause=pause)
if response != '':
return response
else:
return None
else:
print("Port is closed...")
""" Here I'm turning all the useful commands into methods """
def reset(self):
self.send_command('*RST')
def set_output_on(self):
self.send_command(':OUTP ON')
def set_output_off(self):
self.send_command(':OUTP OFF')
def set_source_type(self, source_type):
source_type = source_type.lower()
if source_type == 'current' or source_type == 'curr' or source_type == 'c':
self.send_command(':SOUR:FUNC CURRENT')
elif source_type == 'voltage' or source_type == 'volt' or source_type == 'v':
self.send_command(':SOUR:FUNC VOLT')
else:
print("Unknown source setting!")
def set_source_voltage(self, voltage):
if type(voltage) == int or type(voltage) == float:
if voltage > 200:
voltage = 200
print("Voltage limits are +/- 200 V")
if voltage < -200:
voltage = -200
print("Voltage limits are +/- 200 V")
self.set_source_type('voltage')
self.send_command(':SOUR:VOLT %s' %voltage)
else:
print("Bad voltage sent")
def set_source_current(self, current):
if type(current) == int or type(current) == float:
if current > 1:
current = 1
print("Current limits are +/- 1 A")
if current < -1:
current = -1
print("Current limits are +/- 1 A")
self.set_source_type('current')
self.send_command(':SOUR:CURR %s' %current)
else:
print("Bad current sent")
def set_sensor_type(self, sensor_type):
sensor_type = sensor_type.lower()
if sensor_type == 'current' or sensor_type == 'curr' or sensor_type == 'c':
self.send_command(':SENS:FUNC "CURR"')
return 'current'
elif sensor_type == 'voltage' or sensor_type == 'volt' or sensor_type == 'v':
self.send_command(':SENS:FUNC "VOLT"')
return 'voltage'
else:
print("Unknown sensor setting!")
return None
def set_sensor_range(self, sensor_type, sensor_range):
sensor_type = self.set_sensor_type(sensor_type)
print("Sensor type set to %s" %sensor_type)
if type(sensor_range) == int or type(sensor_range) == float:
order = int(('%.2E' %sensor_range)[5:])
if sensor_type == 'voltage':
self.send_command(':SENS:VOLT:RANG 10E%s' %order)
elif sensor_type == 'current':
self.send_command(':SENS:CURR:RANG 10E%s' %order)
else:
print("Shouldn't get here!")
else:
print("Bad range sent.")
def set_voltage_compliance(self, limit):
if type(limit) == int or type(limit) == float:
coeff = float(('%.2E' %limit)[:3])
order = int(('%.2E' %limit)[5:])
self.send_command(':SENS:VOLT:PROT %sE%s' %(coeff, order))
else:
print("Bad compliance value sent.")
def set_current_compliance(self, limit):
if type(limit) == int or type(limit) == float:
coeff = float(('%.2E' %limit)[:3])
order = int(('%.2E' %limit)[5:])
self.send_command(':SENS:CURR:PROT %sE%s' %(coeff, order))
else:
print("Bad compliance value sent.")
def set_num_triggers(self, num):
if type(num) == int:
if num < 1:
print("Number of triggers must be between 1-2500")
print("Triggers set to 1")
num = 1
if num > 2500:
print("Number of triggers must be between 1-2500")
print("Triggers set to 2500")
num = 2500
self.send_command(':TRIG:COUN %s' %num)
else:
print("Bad trigger value sent.")
def get_num_triggers(self):
response = self.get_response(':TRIG:COUN?')
return int(response.replace(' ', ''))
def read(self, data_type=None):
if data_type == None:
self.send_command(':FORM:ELEM TIME, VOLT, CURR, RES')
elif data_type.lower() == 'v':
self.send_command(':FORM:ELEM TIME, VOLT')
elif data_type.lower() == 'c':
self.send_command(':FORM:ELEM TIME, CURR')
elif data_type.lower() == 'r':
self.send_command(':FORM:ELEM TIME, RES')
else:
self.send_command(':FORM:ELEM TIME, VOLT, CURR, RES')
self.send_command(':OUTP ON')
self.send_command(':INIT')
# FETCH? waits for INIT to complete
response = self.get_response(':FETCH?')
time.sleep(.25)
self.send_command(':OUTP OFF')
return parse_data(response, data_type=data_type)
def sweep(self, data_type=None, start=-10, stop=10, step=1, num_sweeps=1, delay=0.01):
sweep_points = (stop-start)/step + 1
num_trigs = num_sweeps*sweep_points
if data_type == None:
self.send_command(':FORM:ELEM TIME, VOLT, CURR, RES')
elif data_type.lower() == 'v':
self.send_command(':FORM:ELEM TIME, VOLT')
elif data_type.lower() == 'c':
self.send_command(':FORM:ELEM TIME, CURR')
elif data_type.lower() == 'r':
self.send_command(':FORM:ELEM TIME, RES')
else:
self.send_command(':FORM:ELEM TIME, VOLT, CURR, RES')
self.send_command(':SOUR:FUNC VOLT')
self.send_command(":SENS:FUNC 'CURR:DC'")
self.send_command(':SENS:CURR:PROT 0.5')
self.send_command(':SOUR:VOLT:START %s' %start)
self.send_command(':SOUR:VOLT:STOP %s' %stop)
self.send_command(':SOUR:VOLT:STEP %s' %step)
self.send_command(':SOUR:VOLT:MODE SWE')
self.send_command(':SOUR:SWE:RANG AUTO')
self.send_command(':SOUR:SWE:SPAC LIN')
self.send_command(':SOUR:SWE:POIN %s' %sweep_points)
self.send_command(':TRIG:COUN %s' %num_trigs)
self.send_command(':SOUR:DEL %s' %delay)
self.send_command(':OUTP ON')
self.send_command(':INIT')
# FETCH? waits for INIT to complete
response = self.get_response(':FETCH?')
time.sleep(.25)
self.send_command(':OUTP OFF')
if response != None:
return parse_data(response, data_type=data_type)
else:
print("No response from Keithley in Sweep!")
return response
"""
This function parses the data returned from the Keithley.
The data is stuctured as {Volts, Amps, Ohms, Timestamp, Status}.
This creates an array of {Time, Volts, Amps, Ohms}.
Each element is a column vector for easy plotting.
KEYWORDS:
data_type = 'v' returns only time and volts, 'c', only time and current,
'r' only time and ohms.
"""
def parse_data(data, data_type=None):
# Clean up the strings, splits into list
data = data.replace(' ', '').split(',')
# Reshape in sections
if data_type in ['v', 'c', 'r']:
cols = 2
else:
cols = 4
# NOTE If data is not returned in groups of "cols" this will drop elements!
data = zip(*[iter(data)]*cols)
# Use an array as they can be indexed, and for type conversion
try:
data = np.array(data).astype(np.float)
if data_type == None:
return np.array([data[:,3], data[:,0], data[:,1], data[:,2]])
elif data_type.lower() == 'v':
return np.array([data[:,1], data[:,0]])
elif data_type.lower() == 'c':
return np.array([data[:,2], data[:,0]])
elif data_type.lower() == 'r':
return np.array([data[:,3], data[:,0]])
else:
print("Bad data_type given, returning full data set")
return np.array([data[:,3], data[:,0], data[:,1], data[:,2]])
except ValueError:
print("Value Error caught from Keithley! Data returned as string array.")
data = np.array(data)
""" Fast Settings """
start_up_commands = ["*RST",
":SYST:TIME:RES:AUTO 1",
":SYST:BEEP:STAT 0",
":SOUR:FUNC CURR",
":SENS:FUNC:CONC OFF",
":SENS:AVER:STAT OFF",
":SENS:CURR:NPLC 10",
":SENS:VOLT:NPLC 10",
":SENS:RES:NPLC 10",
":SENS:FUNC 'VOLT'",
":SENS:VOLT:RANG 1e1",
":TRIG:DEL 0.0",
":SYST:AZER:STAT OFF",
":SOUR:DELAY 0.0",
":DISP:ENAB OFF",
":ROUT:TERM REAR"]
class FakeKeithley(object):
def __init__(self):
for method in Keithley.__dict__:
self.__dict__[method] = self.fake_method
def fake_method(self, *arg, **kwarg):
pass