-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathk2636.py
277 lines (234 loc) · 10.6 KB
/
k2636.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
"""
Module for interacting with the Keithley 2636B SMU.
Author: Ross <peregrine dot warren at physics dot ox dot ac dot uk>
"""
import visa
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.style as style
import time
from serial import SerialException
class K2636():
"""Class for Keithley control."""
def __init__(self, address='ASRL/dev/ttyUSB0', read_term='\n',
baudrate=57600):
"""Make instrument connection instantly on calling class."""
rm = visa.ResourceManager('@py') # use py-visa backend
self.makeConnection(rm, address, read_term, baudrate)
def makeConnection(self, rm, address, 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.baud_rate = baudrate
if 'GPIB' in str(address):
# Connection via GPIB
print('No GPIB support. Please use serial')
except SerialException:
print("CONNECTION ERROR: Check instrument address.")
raise ConnectionError
def closeConnection(self):
"""Close connection to keithley."""
try:
self.inst.close()
except(NameError):
print('CONNECTION ERROR: No connection established.')
except(AttributeError):
print('CONNECTION ERROR: No connection established.')
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 _read(self):
"""Read instrument."""
r = self.inst.read()
return r
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 readBuffer(self):
"""Read buffer in memory and return an array."""
try:
vg = [float(x) for x in self._query('printbuffer' +
'(1, smub.nvbuffer1.n, smub.nvbuffer1.sourcevalues)').split(',')]
ig = [float(x) for x in self._query('printbuffer' +
'(1, smub.nvbuffer1.n, smub.nvbuffer1.readings)').split(',')]
vd = [float(x) for x in self._query('printbuffer' +
'(1, smua.nvbuffer1.n, smua.nvbuffer1.sourcevalues)').split(',')]
c = [float(x) for x in self._query('printbuffer' +
'(1, smua.nvbuffer1.n, smua.nvbuffer1.readings)').split(',')]
df = pd.DataFrame({'Gate Voltage [V]': vg,
'Channel Voltage [V]': vd,
'Channel Current [A]': c,
'Gate Leakage [A]': ig})
return df
except SerialException:
print('Cannot read buffer.')
return
def readBufferIV(self):
"""Read specified buffer in keithley memory and return an array."""
vd = [float(x) for x in self._query('printbuffer' +
'(1, smua.nvbuffer1.n, smua.nvbuffer1.sourcevalues)').split(',')]
c = [float(x) for x in self._query('printbuffer' +
'(1, smua.nvbuffer1.n, smua.nvbuffer1.readings)').split(',')]
df = pd.DataFrame({'Channel Voltage [V]': vd, 'Channel Current [A]': c})
return df
def readBufferInverter(self):
"""Read specified buffer for inverter measurement."""
SMUAsrc = [float(x) for x in self._query('printbuffer' +
'(1, smua.nvbuffer1.n, smua.nvbuffer1.sourcevalues)').split(',')]
SMUAread = [float(x) for x in self._query('printbuffer' +
'(1, smua.nvbuffer1.n, smua.nvbuffer1.readings)').split(',')]
SMUBsrc = [float(x) for x in self._query('printbuffer' +
'(1, smub.nvbuffer1.n, smub.nvbuffer1.sourcevalues)').split(',')]
SMUBread = [float(x) for x in self._query('printbuffer' +
'(1, smub.nvbuffer1.n, smub.nvbuffer1.readings)').split(',')]
df = pd.DataFrame({'Voltage In [V]': SMUAread, 'Voltage Out [V]': SMUBread, 'SMUA source': SMUAsrc, 'SMUB source': SMUBsrc})
return df
def DisplayMeasurement(self, sample):
"""Show graphs of measurements."""
try:
style.use('ggplot')
fig, ([ax1, ax2], [ax3, ax4]) = plt.subplots(2, 2, figsize=(20, 10),
dpi=80, facecolor='w',
edgecolor='k')
df1 = pd.read_csv(str(sample+'-iv-sweep.csv'), '\t')
ax1.plot(df1['Channel Voltage [V]'],
df1['Channel Current [A]'], '.')
ax1.set_title('I-V sweep')
ax1.set_xlabel('Channel Voltage [V]')
ax1.set_ylabel('Channel Current [A]')
df2 = pd.read_csv(str(sample+'-output.csv'), '\t')
ax2.plot(df2['Channel Voltage [V]'],
df2['Channel Current [A]'], '.')
ax2.set_title('Output curves')
ax2.set_xlabel('Channel Voltage [V]')
ax2.set_ylabel('Channel Current [A]')
df3 = pd.read_csv(str(sample+'-transfer.csv'), '\t')
ax3.plot(df3['Gate Voltage [V]'],
df3['Channel Current [A]'], '.')
ax3.set_title('Transfer Curves')
ax3.set_xlabel('Gate Voltage [V]')
ax3.set_ylabel('Channel Current [A]')
df4 = pd.read_csv(str(sample+'-transfer.csv'), '\t')
ax4.plot(df4['Gate Voltage [V]'],
df4['Gate Leakage [A]'], '.')
ax4.set_title('Gate leakage current')
ax4.set_xlabel('Gate Voltage [V]')
ax4.set_ylabel('Gate Leakage [A]')
fig.tight_layout()
fig.savefig(sample)
plt.show()
except(FileNotFoundError):
print('Sample name not found.')
def IVsweep(self, sample):
"""K2636 IV sweep."""
try:
begin_time = time.time()
self.loadTSP('iv-sweep.tsp')
self.runTSP()
df = self.readBufferIV()
output_name = str(sample + '-iv-sweep.csv')
df.to_csv(output_name, sep='\t', index=False)
finish_time = time.time()
print('IV sweep complete. Elapsed time %.2f mins.'
% ((finish_time - begin_time)/60))
except(AttributeError):
print('Cannot perform IV sweep: no keithley connected.')
def Output(self, sample):
"""K2636 Output sweeps."""
try:
begin_time = time.time()
self.loadTSP('output-charact.tsp')
self.runTSP()
df = self.readBuffer()
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 Transfer(self, sample):
"""K2636 Transfer sweeps."""
try:
begin_time = time.time()
self.loadTSP('transfer-charact.tsp')
self.runTSP()
df = self.readBuffer()
output_name = str(sample + '-neg-pos-transfer.csv')
df.to_csv(output_name, sep='\t', index=False)
# transfer reverse scan
self.loadTSP('transfer-charact-2.tsp')
self.runTSP()
df = self.readBuffer()
output_name = str(sample + '-pos-neg-transfer.csv')
df.to_csv(output_name, sep='\t', index=False)
finish_time = time.time()
print('Transfer curves measured. Elapsed time %.2f mins.'
% ((finish_time - begin_time) / 60))
except(AttributeError):
print('Cannot perform transfer sweep: no keithley connected.')
def Inverter(self, sample):
"""K2636 inverter measurement."""
try:
begin_time = time.time()
self.loadTSP('inverter.tsp')
self.runTSP()
df = self.readBufferInverter()
output_name = str(sample + '-neg-pos-inverter.csv')
df.to_csv(output_name, sep='\t', index=False)
# inverter reverse scan
self.loadTSP('inverter-reverse.tsp')
self.runTSP()
df = self.readBufferInverter()
output_name = str(sample + '-pos-neg-inverter.csv')
df.to_csv(output_name, sep='\t', index=False)
finish_time = time.time()
print('Inverter measurement complete. Elapsed time %.2f mins.'
% ((finish_time - begin_time) / 60))
except(AttributeError):
print('Cannot perform output sweep: no keithley connected.')
########################################################################
if __name__ == '__main__':
"""For testing methods in the K2636 class."""
keithley = K2636(address='ASRL/dev/ttyUSB0', read_term='\n', baudrate=57600)
sample = 'blank-20-1'
keithley.IVsweep(sample)
# keithley.Output(sample)
# keithley.Transfer(sample)
# keithley.DisplayMeasurement(sample)
keithley.closeConnection()