forked from marcelrsoub/keithley-visa-measurements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeithley-2602B.py
249 lines (216 loc) · 10.2 KB
/
keithley-2602B.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
import visa
import numpy as np
import matplotlib.pyplot as plt
from tkinter import Button, Tk, Frame, Entry, Label, StringVar #, Checkbutton, BooleanVar
import time
"""Keithley 2602B automatisation program"""
#--------------------------------------------------------------------------
def connection_choice(connection):
"""enables the user to choose the connection"""
global keithley
try:
rm = visa.ResourceManager() #import visa
rm.list_resources() #import visa
keithley = rm.open_resource(connection) #creat device connection
print("connection succeed")
except:
raise StopIteration ("Error. Verify the connection (GPIB,RS232,USB,Ethernet) and its identifier")
#--------------------------------------------------------------------------
def close_all():
"""Close the connection"""
reset() #reset smu
try:
keithley.write("beeper.enable=0") #deactivate beep
keithley.close() #close the connection
print("connection closed")
except:
raise StopIteration ("Closing error, connection still open")
#--------------------------------------------------------------------------
def reset():
"""reset smu"""
try:
keithley.write("smua.reset()")
keithley.write("smub.reset()")
print("instrument reset")
except:
raise StopIteration ("Reset error")
#--------------------------------------------------------------------------
def switchON(smux, onoff=False):
"""Turn on/off the smu"""
try:
keithley.write(("smu%s.source.output = smu%s.OUTPUT_ON" if onoff else "smu%s.source.output = smu%s.OUTPUT_OFF") % (smux, smux))
print("Source on" if onoff else "Source off")
except:
raise StopIteration ("Source can't be turn on/off")
#--------------------------------------------------------------------------
def measurement(smux,volts_min,volts_max,nb,delay=0,compliance=0.1,R=0):
"""Send voltage and measure current"""
global voltage_input, voltage_diode, measure #allow to save data even when error detected
measure=np.array([]) #creat array for futur measures
voltage_diode=np.array([]) # array for the real voltage across the diode
voltage_input=np.linspace(volts_min,volts_max,nb) #creat an array with all voltages needed for measurement
print('U(V)\tUd(V)\tI(A)\t\tpoints') #display titles
i=0
for x in voltage_input: #loop over voltage values
i+=1 #used to display measure number (could use enumerate instead)
try: #SOURCE
keithley.write("smu%s.source.levelv = %f" % (smux,x)) #send voltage values to the smux
except:
print("error from applied source")
break
time.sleep(delay) #delay not needed
try: #MEASURE
y=float(keithley.query("print(smu%s.measure.i())" % smux)) #read current value
except:
print("error from reading measurement")
break
voltage_diode=np.append(voltage_diode, x - (R*y)) #voltage correction due to safety resistance
measure=np.append(measure, y) #add the current value to a array regrouping all measurements
print ("%3.3f\t%3.3f\t%s\t%s/%s" % (x,voltage_diode[-1],y,i,nb)) #display voltage and current
if y>=compliance:
print("current too high")
break
try:
keithley.write("smu%s.source.levelv = 0" % smux) #send 0 volt after measurements complete
except:
print("error : can't apply 0 volt")
reset()
return (voltage_diode,measure) #return the voltage and current diode
#--------------------------------------------------------------------------
def complete_measure(smux,volts_min,volts_max,nb,delay=0,compliance=0.1,R=0):
"""Main fonction taking tkinter value as input. Plot measures and save them."""
global voltage_input, voltage_diode, measure
switchON(smux, True) #active smua
try:
keithley.write("smu%s.source.func = smu%s.OUTPUT_DCVOLTS" % (smux,smux)) #select smuX as voltage source
except:
raise StopIteration ("error from choosing the source type")
(voltage_diode,measure)=measurement(smux,volts_min,volts_max,nb,compliance=compliance,R=R) #send chosen voltage and measures the current associated
switchON(smux, False) #deactivate smu
np.savetxt('Diode %s R=%s.csv' % (smux,str(R)),np.transpose((voltage_diode,measure)),delimiter="\t")
print("data saved")
try:
plt.figure(num='Diode '+smux+" R="+str(R)) #plot differents figure according to a specific name
plt.clf() #clear the graph to avoir superposing data from the same set (can be deactivated if need to superpose)
plt.title('Diode '+str(smux)+' R='+str(R))
plt.ylabel('I(A)')
plt.xlabel('U(V)')
plt.semilogy(voltage_diode,measure, '+', label='Diode '+smux) #display current in fct of input_voltage
plt.legend() #add legend to the graph (take label from plot)
plt.savefig('Diode %s R=%s.pdf' % (smux,str(R)), format='pdf', dpi=1000, bbox_inches='tight') #save the graph in a vector file
plt.show() #plot data
print("plot save and display")
except:
print ("error from plotting data")
# =============================================================================
def compute():
"""Fonction use by tkinter to pilote the instrument"""
message1["text"] = ""
message2["text"] = ""
message3["text"] = ""
message4["text"] = ""
message5["text"] = ""
message6["text"] = ""
message7["text"] = ""
try:
smux=str(smux_entry.get())
if smux != 'a' and smux != 'b':
raise ValueError
try:
volts_min=float(volt_min_entry.get()) # min voltage
volts_max=float(volt_max_entry.get()) # max voltage
R=float(resistance_entry.get()) # protection resistance
if abs(volts_max)>20 or abs(volts_min)>20:
texte5="abs(volt) <=20"
message5["text"] = texte5
print(texte5)
else:
nb=int(point_number_entry.get()) #measures nb
if nb<1:
texte4="nb>0"
message4["text"] = texte4
print(texte4)
else:
# delay=float(delay_entry.get()) #temps entre mesures en secondes
# print(smux,volts_min,volts_max,nb,delay) #used to debug
try:
complete_measure(smux,volts_min,volts_max,nb,R=R,compliance=compliance) #start measurements
texte7="Measures done"
message7["text"] = texte7
print(texte7)
except Exception as ex:
print(ex) #display the error name
texte6="Error from measurement detected"
message6["text"] = texte6
print(texte6)
reset()
except:
texte2="floats"
message2["text"] = texte2
print(texte2)
except:
texte="a or b"
message1["text"] = texte
print(texte)
# =============================================================================
"""Program begin"""
volts_min=0 #min voltage
nb=101 #how many measures
volts_max=1 # max voltage
delay=0 #time between applying voltage and measuring current (not needed)
smux='a' #name of the smu (a or b)
R=100 # safe resistance value
compliance=0.2 # max current value
#connection='GPIB0::26::INSTR'
connection='COM1'
connection_choice(connection) #try to connect the devide using GPIB or RS232 (com1)
reset() #reset completement les smu
keithley.write("beeper.enable=0") #desactive le beep
# =============================================================================
"""Tkinter menu used to facilitate measurement from an user"""
root = Tk() #used to creat user interface
frame = Frame(root)
root.title("Keithley options")
frame.pack()
L0 = Label(frame, text="a or b")
L0.grid(row=0, column=0)
smux_entry = Entry(frame, textvariable=StringVar(frame, value=smux), bd =2, width=7)
smux_entry.grid(row=0, column=1)
message1 = Label(frame, text="") #allow to display message when activate [text]
message1.grid(row=0, column=2)
L1 = Label(frame, text="start volt(V)")
L1.grid(row=1, column=0)
volt_min_entry = Entry(frame, textvariable=StringVar(frame, value=volts_min), bd =2, width=7)
volt_min_entry.grid(row=1, column=1)
message3 = Label(frame, text="")
message3.grid(row=1, column=2)
L2 = Label(frame, text="end volt(V)")
L2.grid(row=2, column=0)
volt_max_entry = Entry(frame, textvariable=StringVar(frame, value=volts_max), bd =2, width=7)
volt_max_entry.grid(row=2, column=1)
message5 = Label(frame, text="")
message5.grid(row=2, column=2)
L3 = Label(frame, text="nbr pts")
L3.grid(row=3, column=0)
point_number_entry = Entry(frame, textvariable=StringVar(frame, value=nb), bd =2, width=7)
point_number_entry.grid(row=3, column=1)
message4 = Label(frame, text="")
message4.grid(row=3, column=2)
L4 = Label(frame, text="resistance(ohm)")
L4.grid(row=4, column=0)
resistance_entry = Entry(frame, textvariable=StringVar(frame, value=R), bd =2, width=7)
resistance_entry.grid(row=4, column=1)
#L4 = Label(frame, text="delay(s)")
#L4.grid(row=4, column=0)
#delay_entry = Entry(frame, textvariable=StringVar(frame, value=delay), bd =2, width=7)
#delay_entry.grid(row=4, column=1)
message2 = Label(frame, text="")
message2.grid(row=5, column=2)
message6 = Label(frame, text="")
message6.grid(row=6, column=2)
compute_button = Button(frame, text="Measure", width=14, command=compute) #button used to get all values and start measures
compute_button.grid(row=6, column=1)
message7 = Label(frame, text="")
message7.grid(row=6, column=2)
root.mainloop() #instance looping until closed
close_all() #reset and close connection with the instrument