2929loop_list = []
3030
3131#---------------------------------
32- rm = pyvisa .ResourceManager ()
33- # Note: using pyvisa directly for setup commands
34- my_instrument = rm .open_resource ("GPIB::17" )
35- LCR = AgilentE4980 ("GPIB::17" )
36-
37- my_instrument .timeout = 100000
38- my_instrument .read_termination = '\n '
39- my_instrument .write_termination = '\n '
40-
41- my_instrument .write ('*RST; *CLS' )
42- my_instrument .write (':DISP:ENAB' )
43- time .sleep (2 )
44-
45- my_instrument .write (':INIT:CONT' )
46- my_instrument .write (':TRIG:SOUR EXT' )
47- time .sleep (2 )
48-
49- my_instrument .write (':APER MED' )
50- my_instrument .write (':FUNC:IMP:RANGE:AUTO ON' )
51- time .sleep (2 )
52-
53- my_instrument .write (':MMEM EXT' )
54- time .sleep (2 )
55-
56- my_instrument .write (':MEM:DIM DBUF, ' + str (100 ))
57- time .sleep (1 )
58-
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 )
67- #---------------------------------
32+ # Initialize Instrument
33+ try :
34+ rm = pyvisa .ResourceManager ()
35+ my_instrument = rm .open_resource ("GPIB::17" )
36+ LCR = AgilentE4980 ("GPIB::17" )
37+
38+ my_instrument .timeout = 100000
39+ my_instrument .read_termination = '\n '
40+ my_instrument .write_termination = '\n '
41+
42+ my_instrument .write ('*RST; *CLS' )
43+ my_instrument .write (':DISP:ENAB' )
44+ time .sleep (2 )
45+
46+ my_instrument .write (':INIT:CONT' )
47+ my_instrument .write (':TRIG:SOUR EXT' )
48+ time .sleep (2 )
49+
50+ my_instrument .write (':APER MED' )
51+ my_instrument .write (':FUNC:IMP:RANGE:AUTO ON' )
52+ time .sleep (2 )
53+
54+ my_instrument .write (':MMEM EXT' )
55+ time .sleep (2 )
6856
57+ my_instrument .write (':MEM:DIM DBUF, ' + str (100 ))
58+ time .sleep (1 )
59+
60+ my_instrument .write (':MEM:FILL DBUF' )
61+ time .sleep (2 )
62+ my_instrument .write (':MEM:CLE DBUF' )
63+ time .sleep (3 )
64+ print (my_instrument .write (':BIAS:STATe ON' ))
65+ time .sleep (2 )
66+ my_instrument .write (':VOLT:LEVEL ' + str (V_ac ))
67+ time .sleep (2 )
68+
69+ except Exception as e :
70+ print (f"Instrument initialization failed: { e } " )
71+ # In a real scenario, you might want to exit here
72+ pass
73+
74+ #---------------------------------
6975
7076# LCR_fcn for the actual measurements
7177def LCR_fcn (volt_ind ):
72- # --- FIX: Removed unnecessary global declarations here ---
78+ # Removed unused globals that caused linting errors
7379 global v1
7480 global output1
7581
@@ -83,7 +89,7 @@ def LCR_fcn(volt_ind):
8389 output1 = LCR .values (":FETCh:IMPedance:FORMatted?" )
8490 time .sleep (2 )
8591
86- C_list .append (output1 [0 ]) # Fixed append logic
92+ C_list .append (output1 [0 ])
8793 v1 = my_instrument .query (':BIAS:VOLTage:LEVel?' )
8894 V_list .append (v1 )
8995 time .sleep (4 )
@@ -94,7 +100,7 @@ def LCR_fcn(volt_ind):
94100# Proto_fcn for the measurements protocol
95101def Proto_fcn ():
96102 global loop_ind_new
97- # --- FIX: Removed ' global protocol_list' (unnecessary for append) ---
103+ # Removed unused global protocol_list
98104
99105 loop_ind_new += 1
100106
@@ -142,7 +148,6 @@ def Loop_fcn(loop):
142148 data_dict = {'Volt' : V_list , 'Cp' : C_list , 'Loop' : loop_list , 'Protocol' : protocol_list }
143149 df = pd .DataFrame (data_dict )
144150
145- # Ensure directory exists or save to local for safety if path fails
146151 try :
147152 df .to_csv (filename , sep = ',' , index = False , encoding = 'utf-8' )
148153 print (f"Data saved to { filename } " )
@@ -152,11 +157,12 @@ def Loop_fcn(loop):
152157 print (f"Could not save to specified path. Saved to { fallback_name } instead." )
153158
154159 # 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+ if V_list and C_list :
161+ plt .scatter (V_list , C_list )
162+ plt .title ("Cp vs V , Loops:" + str (loop ) + " V_max:" + str (V ) + " step size : " + str (V_step ))
163+ plt .xlabel ("V" )
164+ plt .ylabel ("Cp" )
165+ plt .show ()
160166
161167 except Exception as e :
162168 print (f"An error occurred: { e } " )
0 commit comments