1- #-------------------------------------------------------------------------------
1+ # -------------------------------------------------------------------------------
22# Name: Delta Mode Measurement (Improved)
33# Purpose: Perform a Delta mode measurement with robust error handling
44# and a guaranteed graceful shutdown.
55# Author: keithley_6221-DSL
66# Created: 01/09/2024
7- #-------------------------------------------------------------------------------
7+ # -------------------------------------------------------------------------------
88import pyvisa
99import time
1010
1111# --- Configuration ---
1212# Use constants for easier changes later.
1313KEITHLEY_6221_ADDRESS = "GPIB0::13::INSTR"
14- DELTA_CURRENT = 1 * 10 ** (- 11 ) # Current in Amps
14+ DELTA_CURRENT = 1 * 10 ** (- 11 ) # Current in Amps
1515# Example 0.001
1616
17+
1718def run_delta_measurement ():
1819 """
1920 Initializes the Keithley 6221, runs the measurement loop,
@@ -28,32 +29,37 @@ def run_delta_measurement():
2829 print (f"Available VISA resources: { rm .list_resources ()} " )
2930
3031 keithley_6221 = rm .open_resource (KEITHLEY_6221_ADDRESS )
31- keithley_6221 .timeout = 10000 # Set a 10-second timeout (in milliseconds)
32+ # Set a 10-second timeout (in milliseconds)
33+ keithley_6221 .timeout = 10000
3234 keithley_6221 .write_termination = '\n '
3335 keithley_6221 .read_termination = '\n '
3436
3537 print (f"Connected to: { keithley_6221 .query ('*IDN?' ).strip ()} " )
3638
3739 # --- 2. keithley_6221 Configuration ---
3840 print ("Configuring Keithley 6221 for Delta Mode..." )
39- keithley_6221 .write ("*rst; status:preset; *cls" ) # Reset and clear status
41+ # Reset and clear status
42+ keithley_6221 .write ("*rst; status:preset; *cls" )
4043 keithley_6221 .write ("CURR:COMP 50" ) # Reset and clear status
4144
42-
4345 time .sleep (1 )
44- keithley_6221 .write ("UNIT V" ) # Set measurement unit to Volts
45- keithley_6221 .write (f"SOUR:DELT:HIGH { DELTA_CURRENT } " ) # Set delta current
46- keithley_6221 .write ("SOUR:DELT:ARM" ) # Arm the delta measurement sequence
47- time .sleep (1 ) # Give it a moment to arm
48- keithley_6221 .write ("INIT:IMM" ) # Start (initiate) the measurement
46+ # Set measurement unit to Volts
47+ keithley_6221 .write ("UNIT V" )
48+ # Set delta current
49+ keithley_6221 .write (f"SOUR:DELT:HIGH { DELTA_CURRENT } " )
50+ # Arm the delta measurement sequence
51+ keithley_6221 .write ("SOUR:DELT:ARM" )
52+ # Give it a moment to arm
53+ time .sleep (1 )
54+ # Start (initiate) the measurement
55+ keithley_6221 .write ("INIT:IMM" )
4956
5057 print ("\n --- Measurement Started ---" )
5158 print ("Press Ctrl+Alt_F9 to stop the measurement." )
5259 start_time = time .time ()
5360
5461 # --- 3. Measurement Loop ---
5562
56-
5763 # --- 3. Measurement Loop (Corrected) ---
5864 while True :
5965 # Query for the latest data point.
@@ -65,19 +71,21 @@ def run_delta_measurement():
6571
6672 # The first item is Voltage, the second is Resistance
6773 voltage = float (data_points [0 ])
68- resistance = float (voltage / DELTA_CURRENT ) # Use the resistance calculated by the keithley_6221
74+ # Use the resistance calculated by the keithley_6221
75+ resistance = float (voltage / DELTA_CURRENT )
6976
70- voltages .append (voltage ) # Still good to log the raw voltage
77+ voltages .append (voltage ) # Still good to log the raw voltage
7178
7279 elapsed_time = time .time () - start_time
7380
74- # Print the formatted output using the keithley_6221's resistance value
75- print (f"Time: { elapsed_time } s |Current:{ DELTA_CURRENT } A | Voltage: { voltage } V | Resistance: { resistance } Ohms" )
81+ # Print the formatted output using the keithley_6221's resistance
82+ # value
83+ print (
84+ f"Time: { elapsed_time } s |Current:{ DELTA_CURRENT } A | Voltage: { voltage } V | Resistance: { resistance } Ohms" )
7685
7786 # Control how often you poll for a new reading
7887 time .sleep (1 )
7988
80-
8189 except KeyboardInterrupt :
8290 # This block catches Ctrl+C
8391 print ("\n --- User pressed Ctrl+C. Stopping measurement. ---" )
@@ -92,34 +100,37 @@ def run_delta_measurement():
92100 print (f"\n --- An unexpected error occurred: { e } ---" )
93101
94102 finally :
95- # --- 4. Graceful Shutdown (Most Robust) ---
96- # This block will ALWAYS run.
97- if keithley_6221 :
98- print ("\n Shutting down keithley_6221..." )
99- try :
100- # 1. Clear the VISA/GPIB interface. THIS IS THE KEY FIX.
101- # This clears the keithley_6221's I/O buffers and resets its parser.
102- keithley_6221 .clear ()
103- time .sleep (0.1 )
104- keithley_6221 .write ("*rst; status:preset; *cls" )
105-
106- print ("VISA interface cleared." )
107-
108- # 2. Turn off the current source. This is the most critical safety step.
109- #keithley_6221.write("SOUR:CLE:IMM")
110- print ("keithley_6221 source is OFF." )
111- time .sleep (0.1 )
112-
113- except pyvisa .errors .VisaIOError as e :
114- # If any command fails after a clear, there might be a deeper issue,
115- # but we print it without crashing the script.
116- print (f"Warning during shutdown sequence: { e } " )
117-
118- # 4. Finally, close the connection to the keithley_6221.
119- #keithley_6221.close()
120- print ("keithley_6221 connection closed." )
121-
122- print ("--- Measurement Complete ---" )
103+ # --- 4. Graceful Shutdown (Most Robust) ---
104+ # This block will ALWAYS run.
105+ if keithley_6221 :
106+ print ("\n Shutting down keithley_6221..." )
107+ try :
108+ # 1. Clear the VISA/GPIB interface. THIS IS THE KEY FIX.
109+ # This clears the keithley_6221's I/O buffers and resets its
110+ # parser.
111+ keithley_6221 .clear ()
112+ time .sleep (0.1 )
113+ keithley_6221 .write ("*rst; status:preset; *cls" )
114+
115+ print ("VISA interface cleared." )
116+
117+ # 2. Turn off the current source. This is the most critical safety step.
118+ # keithley_6221.write("SOUR:CLE:IMM")
119+ print ("keithley_6221 source is OFF." )
120+ time .sleep (0.1 )
121+
122+ except pyvisa .errors .VisaIOError as e :
123+ # If any command fails after a clear, there might be a deeper issue,
124+ # but we print it without crashing the script.
125+ print (f"Warning during shutdown sequence: { e } " )
126+
127+ # 4. Finally, close the connection to the keithley_6221.
128+ # keithley_6221.close()
129+ print ("keithley_6221 connection closed." )
130+
131+ print ("--- Measurement Complete ---" )
132+
133+
123134# --- Main execution block ---
124135if __name__ == "__main__" :
125136 run_delta_measurement ()
0 commit comments