@@ -77,97 +77,101 @@ def plot_results(data):
7777 plt .show ()
7878
7979
80- # --- Main Execution ---
81- keithley = None
82- results = []
83-
84- try :
85- # Get sweep parameters from the user
86- start_v , stop_v , steps , delay , filename = get_sweep_parameters ()
87- voltage_sweep = np .linspace (start_v , stop_v , steps )
88-
89- # --- 2. CONNECT TO INSTRUMENT (V5 Logic) ---
90- print (f"\n Attempting to connect to instrument at: { VISA_ADDRESS } " )
91- keithley = Keithley6517B (VISA_ADDRESS )
92- print (f"Successfully connected to: { keithley .id } " )
93-
94- # --- 3. CONFIGURE MEASUREMENT (V5 Logic) ---
95- print ("\n Configuring instrument for measurement..." )
96- keithley .reset ()
97- # Set the function to resistance to ensure the ammeter is configured for
98- # zero correction.
99- keithley .measure_resistance ()
100-
101- # --- 4. PERFORM ZERO CHECK & CORRECTION (Exact V5 Logic) ---
102- print ("\n Starting zero correction procedure..." )
103- time .sleep (5 )
104- print ("Step 1: Enabling Zero Check mode..." )
105- keithley .write (':SYSTem:ZCHeck ON' ) # type: ignore
106- time .sleep (5 )
107- print ("Step 2: Acquiring zero correction value..." )
108- # keithley.write(':SYSTem:ZCORrect:ACQuire')
109- time .sleep (5 )
110- print ("Step 3: Disabling Zero Check mode..." )
111- keithley .write (':SYSTem:ZCHeck OFF' )
112- time .sleep (5 )
113- print ("Step 4: Enabling Zero Correction..." )
114- keithley .write (':SYSTem:ZCORrect ON' )
115- time .sleep (5 )
116- print ("Zero Correction Complete." )
117-
118- # --- 5. SETUP AND PERFORM I-V SWEEP ---
119- print (f"\n Starting I-V sweep from { start_v } V to { stop_v } V..." )
120- keithley .current_nplc = 1 # Set integration rate for noise reduction
121-
122- keithley .enable_source ()
123-
124- with open (filename , 'w' , newline = '' ) as f :
125- writer = csv .writer (f )
126- writer .writerow (
127- [f"# Measurement Date: { datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )} " ])
128- writer .writerow (
129- [f"# Sweep Parameters: Start={ start_v } V, Stop={ stop_v } V, Steps={ steps } , Delay={ delay } s" ])
130- writer .writerow (["Timestamp (s)" , "Applied Voltage (V)" ,
131- "Measured Current (A)" , "Resistance (Ohm)" ])
132-
133- start_time = time .time ()
134- for i , voltage in enumerate (voltage_sweep ):
135- keithley .source_voltage = voltage
136- time .sleep (delay )
137- resistance = keithley .resistance
138- timestamp = time .time () - start_time
139- # resistance = keithley.resistance
140- current = voltage / resistance if resistance != 0 else float ('inf' )
141-
142- print (
143- f"Step { i + 1 } /{ steps } : V={ voltage :.3f} V, I={ current :.4e} A, R={ resistance :.4e} Ω" )
144-
145- data_point = [
146- f"{ timestamp :.3f} " ,
147- f"{ voltage :.4e} " ,
148- f"{ current :.4e} " ,
149- f"{ resistance :.4e} " ]
150- results .append (data_point )
151- writer .writerow (data_point )
152-
153- print ("\n --- I-V Sweep Complete ---" )
154- print (f"Data saved successfully to '{ filename } '" )
155-
156- except VisaIOError :
157- print ("\n [VISA Connection Error]" )
158- print (f"Could not connect to the instrument at '{ VISA_ADDRESS } '." )
159- print ("Please check the address, cable connections, and if the instrument is on." )
160- except ValueError :
161- print ("\n [Input Error] Please enter valid numbers for the sweep parameters." )
162- except Exception as e :
163- print (f"\n [An Unexpected Error Occurred] Details: { e } " )
164-
165- finally :
166- # --- 7. SAFELY SHUT DOWN (V5 Logic) ---
167- if keithley :
168- print ("\n Shutting down instrument..." )
169- keithley .shutdown ()
170- print ("Voltage source OFF and instrument is safe." )
171-
172- # --- 8. PLOT RESULTS ---
173- plot_results (results )
80+ def main ():
81+ """Main function to run the I-V experiment."""
82+ keithley = None
83+ results = []
84+
85+ try :
86+ # Get sweep parameters from the user
87+ start_v , stop_v , steps , delay , filename = get_user_parameters ()
88+ voltage_sweep = np .linspace (start_v , stop_v , steps )
89+
90+ # --- 2. CONNECT TO INSTRUMENT (V5 Logic) ---
91+ print (f"\n Attempting to connect to instrument at: { VISA_ADDRESS } " )
92+ keithley = Keithley6517B (VISA_ADDRESS )
93+ print (f"Successfully connected to: { keithley .id } " )
94+
95+ # --- 3. CONFIGURE MEASUREMENT (V5 Logic) ---
96+ print ("\n Configuring instrument for measurement..." )
97+ keithley .reset ()
98+ # Set the function to resistance to ensure the ammeter is configured for
99+ # zero correction.
100+ keithley .measure_resistance ()
101+
102+ # --- 4. PERFORM ZERO CHECK & CORRECTION (Exact V5 Logic) ---
103+ print ("\n Starting zero correction procedure..." )
104+ time .sleep (5 )
105+ print ("Step 1: Enabling Zero Check mode..." )
106+ keithley .write (':SYSTem:ZCHeck ON' ) # type: ignore
107+ time .sleep (5 )
108+ print ("Step 2: Acquiring zero correction value..." )
109+ # keithley.write(':SYSTem:ZCORrect:ACQuire')
110+ time .sleep (5 )
111+ print ("Step 3: Disabling Zero Check mode..." )
112+ keithley .write (':SYSTem:ZCHeck OFF' )
113+ time .sleep (5 )
114+ print ("Step 4: Enabling Zero Correction..." )
115+ keithley .write (':SYSTem:ZCORrect ON' )
116+ time .sleep (5 )
117+ print ("Zero Correction Complete." )
118+
119+ # --- 5. SETUP AND PERFORM I-V SWEEP ---
120+ print (f"\n Starting I-V sweep from { start_v } V to { stop_v } V..." )
121+ keithley .current_nplc = 1 # Set integration rate for noise reduction
122+
123+ keithley .enable_source ()
124+
125+ with open (filename , 'w' , newline = '' ) as f :
126+ writer = csv .writer (f )
127+ writer .writerow (
128+ [f"# Measurement Date: { datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )} " ])
129+ writer .writerow (
130+ [f"# Sweep Parameters: Start={ start_v } V, Stop={ stop_v } V, Steps={ steps } , Delay={ delay } s" ])
131+ writer .writerow (["Timestamp (s)" , "Applied Voltage (V)" ,
132+ "Measured Current (A)" , "Resistance (Ohm)" ])
133+
134+ start_time = time .time ()
135+ for i , voltage in enumerate (voltage_sweep ):
136+ keithley .source_voltage = voltage
137+ time .sleep (delay )
138+ resistance = keithley .resistance
139+ timestamp = time .time () - start_time
140+ current = voltage / resistance if resistance != 0 else float ('inf' )
141+
142+ print (
143+ f"Step { i + 1 } /{ steps } : V={ voltage :.3f} V, I={ current :.4e} A, R={ resistance :.4e} Ω" )
144+
145+ data_point = [
146+ f"{ timestamp :.3f} " ,
147+ f"{ voltage :.4e} " ,
148+ f"{ current :.4e} " ,
149+ f"{ resistance :.4e} " ]
150+ results .append (data_point )
151+ writer .writerow (data_point )
152+
153+ print ("\n --- I-V Sweep Complete ---" )
154+ print (f"Data saved successfully to '{ filename } '" )
155+
156+ except VisaIOError :
157+ print ("\n [VISA Connection Error]" )
158+ print (f"Could not connect to the instrument at '{ VISA_ADDRESS } '." )
159+ print ("Please check the address, cable connections, and if the instrument is on." )
160+ except ValueError :
161+ print ("\n [Input Error] Please enter valid numbers for the sweep parameters." )
162+ except Exception as e :
163+ print (f"\n [An Unexpected Error Occurred] Details: { e } " )
164+
165+ finally :
166+ # --- 7. SAFELY SHUT DOWN (V5 Logic) ---
167+ if keithley :
168+ print ("\n Shutting down instrument..." )
169+ keithley .shutdown ()
170+ print ("Voltage source OFF and instrument is safe." )
171+
172+ # --- 8. PLOT RESULTS ---
173+ plot_results (results )
174+
175+
176+ if __name__ == "__main__" :
177+ main ()
0 commit comments