-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeithley.py
278 lines (222 loc) · 9.95 KB
/
keithley.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
import pyvisa as visa
import serial
import pandas as pd
from serial import SerialException
import time
class Keithley():
"""Class for Keithley control."""
def __init__(self, address, timeout, read_term='\n', baudrate=115200):
"""Make instrument connection instantly on calling class."""
print("hello")
rm = visa.ResourceManager('@py') # use py-visa backend
print(rm.list_resources())
self.makeConnection(rm, address, timeout, read_term, baudrate)
def __del__(self):
"""
Action to perform on class destruction.
Args:
- None.
Returns:
- None.
"""
self.closeConnection()
def makeConnection(self, rm, address, timeout, read_term, baudrate):
"""Make initial connection to instrument."""
try:
if 'ttyS' or 'ttyUSB' in str(address):
# Connection via SERIAL
self.inst = rm.open_resource(address)
self.inst.read_termination = str(read_term)
self.inst.term_chars = str(read_term)
self.inst.baud_rate = baudrate
self.inst.timeout = timeout
print(self._query('*IDN?'))
if 'GPIB' in str(address):
# Connection via GPIB
self.inst = rm.open_resource(address)
self.inst.read_termination = str(read_term)
self.inst.term_chars = str(read_term)
print(f"GPIB connection.")
if 'TCPIP' in str(address):
# Connection via ethernet
self.inst = rm.open_resource(address)
self.inst.write_termination = str(read_term)
self.inst.read_termination = str(read_term)
self.inst.term_chars = str(read_term)
# self.inst.timeout = timeout
# print(self._query('*IDN?'))
except SerialException:
print("CONNECTION ERROR: Check instrument address.")
raise ConnectionError
def closeConnection(self):
"""Close connection to keithley."""
try:
self.inst.close()
print(f"Keithley closed.")
except(NameError):
print('CONNECTION ERROR [NameError]: No connection established.')
except(AttributeError):
print('CONNECTION ERROR [AttributeError]: No connection established.')
def on(self):
"""Turn ON the output."""
self._write("smua.source.output = smua.OUTPUT_ON")
def off(self):
"""Turn OFF the output."""
self._write("smua.source.output = smua.OUTPUT_OFF")
def reset(self):
"""Reset the SMU."""
self._write("smua.reset()")
def format_ASCII(self):
"""Select ASCII data format."""
self._write("format.data = format.ASCII")
### Buffer configuration ###
def enable_app_buf(self):
"""Enable append buffer mode."""
self._write("smua.nvbuffer1.appendmode = 1")
def disable_app_buf(self):
"""Enable append buffer mode."""
self._write("smua.nvbuffer1.appendmode = 0")
def enable_src_val_store(self):
"""Enable source value storage."""
self._write("smua.nvbuffer1.collectsourcevalues = 1")
def disable_src_val_store(self):
"""Enable source value storage."""
self._write("smua.nvbuffer1.collectsourcevalues = 0")
### SMU configuration ###
def mode_2W(self):
"""Set 2-Wire mode."""
self._write("smua.sense = smua.SENSE_LOCAL")
def mode_4W(self):
"""Set 4-Wire mode."""
self._write("smua.sense = smua.SENSE_REMOTE")
def set_s_autorange(self, i):
"""Set source range to auto. i= 'v' , 'i' , 'r'."""
self._write("smua.source.autorange"+str(i)+" = smua.AUTORANGE_ON")
def set_m_autorange(self, i):
"""Set measure range to auto. i= 'v' , 'i' , 'r'."""
self._write("smua.measure.autorange"+str(i)+" = smua.AUTORANGE_ON")
def set_s_range(self, i, gauge):
"""Set source range. i= 'v' , 'i' , 'r'."""
self._write("smua.source.range"+str(i)+" = "+str(gauge))
def set_m_range(self, i, gauge):
"""Set measure range. i= 'v' , 'i' , 'r'."""
self._write("smua.measure.range"+str(i)+" = "+str(gauge))
def set_V_source(self):
"""Set smu in voltage source function."""
self._write("smua.source.func = smua.OUTPUT_DCVOLTS")
def set_I_source(self):
"""Set smu in current source function."""
self._write("smua.source.func = smua.OUTPUT_DCAMPS")
def set_s_level(self, i, val):
"""Set source value. i= 'v' , 'i' , 'r'."""
self._write("smua.source.level"+str(i)+" = "+str(val))
def set_s_limit(self, i, val):
"""Set source value. i= 'v' , 'i' , 'r'."""
self._write("smua.source.limit"+str(i)+" = "+str(val))
def set_delay(self, delay):
"""Set delay before the first measurement in seconds."""
self._write("smua.measure.delay = "+str(delay))
def set_period(self, period):
"""Set period between two measurements in seconds."""
self._write("smua.measure.interval = "+str(period))
def set_count(self, count):
"""Set the number of measurements."""
self._write("smua.measure.count = "+str(count))
def clear_buf(self, n):
"""Clear the SMU buffer."""
self._write("smua.nvbuffer"+str(n)+".clear()")
def _write(self, m):
"""Write to instrument."""
try:
assert type(m) == str
self.inst.write(m)
except AttributeError:
print('CONNECTION ERROR: No connection established.')
def _query(self, s):
"""Query instrument."""
try:
r = self.inst.query(s)
return r
except SerialException:
return ('Serial port busy, try again.')
except FileNotFoundError:
return ('CONNECTION ERROR: No connection established.')
except AttributeError:
print('CONNECTION ERROR: No connection established.')
return ('CONNECTION ERROR: No connection established.')
def loadTSP(self, tsp):
"""Load an anonymous TSP script into the K2636 nonvolatile memory."""
try:
tsp_dir = 'TSP-scripts/' # Put all tsp scripts in this folder
self._write('loadscript')
line_count = 1
for line in open(str(tsp_dir + tsp), mode='r'):
self._write(line)
line_count += 1
self._write('endscript')
print('----------------------------------------')
print('Uploaded TSP script: ', tsp)
except FileNotFoundError:
print('ERROR: Could not find tsp script. Check path.')
raise SystemExit
def runTSP(self):
"""Run the anonymous TSP script currently loaded in the K2636 memory."""
self._write('script.anonymous.run()')
print('Measurement in progress...')
def readBufferV(self):
"""Read buffer in memory and return an array."""
try:
s = [float(x) for x in self._query('printbuffer'+'(1, smua.nvbuffer1.n, smua.nvbuffer1.sourcevalues)').split(',')]
m = [float(x) for x in self._query('printbuffer'+'(1, smua.nvbuffer1.n, smua.nvbuffer1.readings)').split(',')]
df = pd.DataFrame({'Current [A]': s, 'Voltage [V]': m})
return df
except SerialException:
print('Cannot read buffer.')
return
### SMU functions ###
def measureV(self, sample):
"""Measure voltage with zero current source."""
try:
begin_time = time.time()
self.loadTSP('measureV.tsp')
self.runTSP()
df = self.readBufferV()
output_name = str(sample + '-output.csv')
df.to_csv(output_name, sep='\t', index=False)
finish_time = time.time()
print('Output sweeps complete. Elapsed time %.2f mins.'
% ((finish_time - begin_time) / 60))
except(AttributeError):
print('Cannot perform output sweep: no keithley connected.')
def sweepVlin_measureI(self, startv, stopv, stime, points):
"""Sweep voltage from startv to stopv with points steps
wait stime seconds at each step, and log current"""
try:
#begin_time = time.time()
self._write("SweepVLinMeasureI(smua," + str(startv) + "," + str(stopv) + "," + str(stime) + "," +str(points) + ")")
#df = self.readBufferV()
#output_name = str(sample + '-output.csv')
#df.to_csv(output_name, sep='\t', index=False)
#finish_time = time.time()
#print('Output sweeps complete. Elapsed time %.2f mins.'
# % ((finish_time - begin_time) / 60))
except(AttributeError):
print('Cannot perform output sweep: no keithley connected.')
def measure(self,i):
"""Record a measurement into the buffer."""
self._write("smua.measure."+str(i)+"(smua.nvbuffer1)")
def print_readings_buf(self):
"""Print the value of the readings buffer."""
result = self._query('printbuffer'+'(1, smua.nvbuffer1.n, smua.nvbuffer1.readings)').split(',')
# print(result)
return result
def print_srcvalues_buf(self):
"""Print the value of the source values buffer."""
result = self._query('printbuffer'+'(1, smua.nvbuffer1.n, smua.nvbuffer1.sourcevalues)').split(',')
# print(result)
return result
def print_timestamps_buf(self):
"""Print the value of the readings buffer."""
result = self._query('printbuffer'+'(1, smua.nvbuffer1.n, smua.nvbuffer1.timestamps)').split(',')
# print(result)
return result