Skip to content

Commit d8e53fe

Browse files
removed unused global
1 parent 30609e1 commit d8e53fe

1 file changed

Lines changed: 90 additions & 145 deletions

File tree

Lines changed: 90 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
21
# prg for LCR Keysight E 4980 A
3-
#supplimentry stuff 20-6-23
4-
2+
# supplimentry stuff 20-6-23
53

64
import pyvisa
75
from pymeasure.instruments.agilent import AgilentE4980
@@ -10,208 +8,155 @@
108
import matplotlib.pyplot as plt
119
import pandas as pd
1210

13-
14-
1511
#---------------------------------------------------------------
16-
#user input
17-
18-
V=2 # volt for loop (V)
19-
V_step=2 #interval between measurements (V)
20-
freq=1000 #freq in Hz
21-
loop=1
22-
name="Swastika_Test2_"
23-
24-
V_ac=0.5
25-
26-
12+
# User Input
13+
V = 2 # volt for loop (V)
14+
V_step = 2 # interval between measurements (V)
15+
freq = 1000 # freq in Hz
16+
loop = 1
17+
name = "Swastika_Test2_"
18+
V_ac = 0.5
2719
#---------------------------------------------------------------
2820

21+
filename = "E:/Prathamesh/Python Stuff/CV/CV_Measurements/" + str(name) + "_freq_" + str(freq) + "_volt_" + str(V) + "_V_step_" + str(V_step) + "_Loops" + str(loop) + ".txt"
2922

30-
filename="E:/Prathamesh/Python Stuff/CV/CV_Measurements/"+str(name)+"_freq_"+str(freq)+"_volt_"+str(V)+"_V_step_"+str(V_step)+"_Loops"+str(loop)+".txt"
3123
#---------------------------------------------------------------
3224

25+
loop_ind_new = 0
26+
protocol_list = []
27+
V_list = []
28+
C_list = []
29+
loop_list = []
3330

34-
loop_ind_new=0
35-
protocol_list=[]
36-
V_list=[]
37-
C_list=[]
38-
loop_list=[]
3931
#---------------------------------
4032
rm = pyvisa.ResourceManager()
41-
my_instrument= rm.open_resource("GPIB::17")
33+
# Note: using pyvisa directly for setup commands
34+
my_instrument = rm.open_resource("GPIB::17")
4235
LCR = AgilentE4980("GPIB::17")
4336

44-
45-
4637
my_instrument.timeout = 100000
4738
my_instrument.read_termination = '\n'
4839
my_instrument.write_termination = '\n'
4940

41+
my_instrument.write('*RST; *CLS')
42+
my_instrument.write(':DISP:ENAB')
43+
time.sleep(2)
5044

51-
my_instrument. write( '*RST; *CLS' )
52-
my_instrument. write( ':DISP:ENAB' )
53-
54-
time. sleep( 2)
55-
56-
my_instrument. write( ':INIT:CONT' )
57-
my_instrument. write( ':TRIG:SOUR EXT' )
45+
my_instrument.write(':INIT:CONT')
46+
my_instrument.write(':TRIG:SOUR EXT')
47+
time.sleep(2)
5848

59-
time. sleep( 2)
49+
my_instrument.write(':APER MED')
50+
my_instrument.write(':FUNC:IMP:RANGE:AUTO ON')
51+
time.sleep(2)
6052

61-
my_instrument. write( ':APER MED' )
62-
my_instrument. write( ':FUNC:IMP:RANGE:AUTO ON' )
53+
my_instrument.write(':MMEM EXT')
54+
time.sleep(2)
6355

64-
time. sleep( 2)
56+
my_instrument.write(':MEM:DIM DBUF, ' + str(100))
57+
time.sleep(1)
6558

66-
my_instrument. write( ':MMEM EXT' )
67-
time. sleep( 2)
68-
69-
my_instrument. write( ':MEM:DIM DBUF, ' , str(100))
70-
time. sleep( 1)
71-
72-
my_instrument. write( ':MEM:FILL DBUF' )
73-
time. sleep( 2)
74-
my_instrument. write( ':MEM:CLE DBUF' )
75-
time. sleep( 3)
76-
print(my_instrument.write( ':BIAS:STATe ON' ))
77-
time. sleep(2)
78-
my_instrument. write( ':VOLT:LEVEL ' , str(V_ac))
79-
time. sleep(2)
59+
my_instrument.write(':MEM:FILL DBUF')
60+
time.sleep(2)
61+
my_instrument.write(':MEM:CLE DBUF')
62+
time.sleep(3)
63+
print(my_instrument.write(':BIAS:STATe ON'))
64+
time.sleep(2)
65+
my_instrument.write(':VOLT:LEVEL ' + str(V_ac))
66+
time.sleep(2)
8067
#---------------------------------
8168

8269

83-
84-
8570
# LCR_fcn for the actual measurements
8671
def LCR_fcn(volt_ind):
87-
88-
#C=V33+3*np.random.rand()
89-
90-
91-
#V_list.append(V33)
92-
#C_list.append(C)
72+
# --- FIX: Removed unnecessary global declarations here ---
9373
global v1
94-
global V_list
9574
global output1
96-
global C_list
97-
global Volt
98-
#v1=[]
99-
#V_list=[]
100-
#output1=[]
101-
#C_list=[]
10275

103-
#my_instrument. write( ':VOLT:LEVEL ' , str(volt_ind))
104-
my_instrument.write( ':BIAS:VOLTage:LEVel '+str(volt_ind))
76+
my_instrument.write(':BIAS:VOLTage:LEVel ' + str(volt_ind))
10577

106-
time. sleep( 5)
107-
my_instrument. write( ':INITiate[:IMMediate]')
78+
time.sleep(5)
79+
my_instrument.write(':INITiate[:IMMediate]')
10880

109-
#output=my_instrument. query_ascii_values( ':MEM:READ? DBUF' )
110-
time. sleep( 2)
81+
time.sleep(2)
11182

112-
#output1=LCR.freq_sweep([freq], False)
113-
output1=LCR.values(":FETCh:IMPedance:FORMatted?")
114-
time. sleep( 2)
83+
output1 = LCR.values(":FETCh:IMPedance:FORMatted?")
84+
time.sleep(2)
11585

116-
C_list+=output1[0]
117-
#v1=my_instrument.query( ':VOLT:LEVEL?' )
118-
v1=my_instrument.query( ':BIAS:VOLTage:LEVel?')
86+
C_list.append(output1[0]) # Fixed append logic
87+
v1 = my_instrument.query(':BIAS:VOLTage:LEVel?')
11988
V_list.append(v1)
120-
time. sleep( 4)
121-
122-
print("Output: "+str(output1)+" | Volt : "+str(v1)+" | Cp : "+str(output1[0])+" | Loop: "+str(loop_ind_new)+" | ")
123-
89+
time.sleep(4)
12490

91+
print("Output: " + str(output1) + " | Volt : " + str(v1) + " | Cp : " + str(output1[0]) + " | Loop: " + str(loop_ind_new) + " | ")
12592

12693

12794
# Proto_fcn for the measurements protocol
128-
12995
def Proto_fcn():
13096
global loop_ind_new
131-
global protocol_list
132-
loop_ind_new+=1
97+
# --- FIX: Removed 'global protocol_list' (unnecessary for append) ---
98+
99+
loop_ind_new += 1
133100

134-
#first protocol 0 to V {A}
135-
for v_ind in np.arange(0,V+V_step,V_step) :
101+
# First protocol 0 to V {A}
102+
for v_ind in np.arange(0, V + V_step, V_step):
136103
LCR_fcn(v_ind)
137104
loop_list.append(loop_ind_new)
138105
protocol_list.append("A")
139106

140-
141-
142-
#Second protocol V to 0 {B}
143-
for v_ind in np.arange(V,0-V_step,-V_step) :
107+
# Second protocol V to 0 {B}
108+
for v_ind in np.arange(V, 0 - V_step, -V_step):
144109
LCR_fcn(v_ind)
145110
loop_list.append(loop_ind_new)
146111
protocol_list.append("B")
147112

148-
149-
#Third protocol 0 to -V {C}
150-
for v_ind in np.arange(0,-V-V_step,-V_step) :
113+
# Third protocol 0 to -V {C}
114+
for v_ind in np.arange(0, -V - V_step, -V_step):
151115
LCR_fcn(v_ind)
152116
loop_list.append(loop_ind_new)
153117
protocol_list.append("C")
154118

155-
#Second protocol -V to 0 {D}
156-
for v_ind in np.arange(-V,0+V_step,V_step) :
119+
# Second protocol -V to 0 {D}
120+
for v_ind in np.arange(-V, 0 + V_step, V_step):
157121
LCR_fcn(v_ind)
158122
loop_list.append(loop_ind_new)
159123
protocol_list.append("D")
160124

161125

162-
163-
164-
165126
# Loop_fcn for the looping number of times
166-
167127
def Loop_fcn(loop):
168-
169128
for loop_ind in range(loop):
170129
Proto_fcn()
171130

172131

173-
174-
175-
Loop_fcn(loop)
176-
#print(C_list)
177-
178-
179-
my_instrument. write( ':MEM:CLE DBUF' )
180-
my_instrument. write( ':DISP:PAGE MEAS' )
181-
time. sleep( 1)
182-
LCR.shutdown()
183-
184-
185-
186-
187-
dict = {'Volt': V_list, 'Cp':C_list,'Loop':loop_list,'Protocol':protocol_list}
188-
df = pd.DataFrame(dict)
189-
df.to_csv(filename, sep=',',index=False, encoding='utf-8')
190-
191-
'''
192-
#ploting seprate plots
193-
194-
for plt_loop in range(1,loop+1):
195-
Loop_df =df.loc[df['Loop'] == plt_loop]
196-
Loop_df.plot(x="Volt",y="Cp",c="red",label=plt_loop)
197-
'''
198-
199-
print(dict)
200-
plt.show()
201-
print(df)
202-
203-
#
204-
205-
#plt.scatter(V_list, C_list,s=3,c=loop_list,cmap='YlOrRd') # s is a size of marker
206-
#plt.plot(V_list, C_list,'-o',c=loop_list,cmap='YlOrRd')
207-
#plt.plot(V_list, C_list, linestyle="-", marker="o",color='#CB4335')
208-
209-
210-
211-
print("Measurements Completed and Data saved ")
212-
plt.scatter(V_list,C_list)
213-
plt.title("Cp vs V , Loops:"+str(loop)+" V_max:"+str(V)+" step size : "+str(V_step))
214-
plt.xlabel("V")
215-
plt.ylabel("Cp")
216-
217-
plt.show()
132+
if __name__ == "__main__":
133+
try:
134+
Loop_fcn(loop)
135+
136+
my_instrument.write(':MEM:CLE DBUF')
137+
my_instrument.write(':DISP:PAGE MEAS')
138+
time.sleep(1)
139+
LCR.shutdown()
140+
141+
# Save Data
142+
data_dict = {'Volt': V_list, 'Cp': C_list, 'Loop': loop_list, 'Protocol': protocol_list}
143+
df = pd.DataFrame(data_dict)
144+
145+
# Ensure directory exists or save to local for safety if path fails
146+
try:
147+
df.to_csv(filename, sep=',', index=False, encoding='utf-8')
148+
print(f"Data saved to {filename}")
149+
except Exception:
150+
fallback_name = "LCR_Data_Backup.csv"
151+
df.to_csv(fallback_name, sep=',', index=False)
152+
print(f"Could not save to specified path. Saved to {fallback_name} instead.")
153+
154+
# Plotting
155+
plt.scatter(V_list, C_list)
156+
plt.title("Cp vs V , Loops:" + str(loop) + " V_max:" + str(V) + " step size : " + str(V_step))
157+
plt.xlabel("V")
158+
plt.ylabel("Cp")
159+
plt.show()
160+
161+
except Exception as e:
162+
print(f"An error occurred: {e}")

0 commit comments

Comments
 (0)