Skip to content

Commit 52d6e81

Browse files
ran autopep8 --in-place --recursive --aggressive --aggressive .
1 parent 32cf5af commit 52d6e81

43 files changed

Lines changed: 9401 additions & 3030 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Delta_mode_Keithley_6221_2182/Backends/Delta_K6221_K2182_L350_T_Sensing_Backend_v1.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#-------------------------------------------------------------------------------
1+
# -------------------------------------------------------------------------------
22
# Name: Combined Delta and Lakeshore Measurement
33
# Purpose: Perform a Delta mode measurement with a Keithley 6221 while
44
# simultaneously monitoring temperature with a Lakeshore 350.
55
# Author: Prathamesh
66
# Created: 09/09/2025
77
# Version: 1.0
8-
#-------------------------------------------------------------------------------
8+
# -------------------------------------------------------------------------------
99
import pyvisa
1010
import time
1111

@@ -15,7 +15,8 @@
1515
LAKESHORE_350_ADDRESS = "GPIB1::15::INSTR"
1616
DELTA_CURRENT = 0.002 # Delta current in Amps
1717
OUTPUT_FILENAME = 'Delta_Lakeshore_Data.csv'
18-
TEMPERATURE_LIMIT = 302.5 # Temperature in Kelvin to stop the measurement
18+
TEMPERATURE_LIMIT = 302.5 # Temperature in Kelvin to stop the measurement
19+
1920

2021
def run_combined_measurement():
2122
"""
@@ -71,7 +72,8 @@ def run_combined_measurement():
7172

7273
# --- 3. Data Logging Setup ---
7374
with open(OUTPUT_FILENAME, 'w', newline='') as file:
74-
file.write("Time (s),Current (A),Voltage (V),Resistance (Ohm),Temperature (K)\n")
75+
file.write(
76+
"Time (s),Current (A),Voltage (V),Resistance (Ohm),Temperature (K)\n")
7577

7678
# --- 4. Measurement Loop ---
7779
print("\n--- Measurement Started ---")
@@ -101,11 +103,13 @@ def run_combined_measurement():
101103

102104
# Append to file
103105
with open(OUTPUT_FILENAME, 'a', newline='') as file:
104-
file.write(f"{elapsed_time:.2f},{DELTA_CURRENT},{voltage},{resistance},{temperature}\n")
106+
file.write(
107+
f"{elapsed_time:.2f},{DELTA_CURRENT},{voltage},{resistance},{temperature}\n")
105108

106109
# Check for temperature limit
107110
if temperature > TEMPERATURE_LIMIT:
108-
print(f"\nTemperature limit of {TEMPERATURE_LIMIT} K reached. Stopping measurement.")
111+
print(
112+
f"\nTemperature limit of {TEMPERATURE_LIMIT} K reached. Stopping measurement.")
109113
break
110114

111115
# Wait before next measurement
@@ -127,7 +131,8 @@ def run_combined_measurement():
127131
print("Shutting down Keithley 6221...")
128132
keithley_6221.clear()
129133
time.sleep(0.1)
130-
keithley_6221.write("SOUR:CLE:IMM") # Abort measurement and turn source off
134+
# Abort measurement and turn source off
135+
keithley_6221.write("SOUR:CLE:IMM")
131136
keithley_6221.write("*rst")
132137
keithley_6221.close()
133138
print("Keithley 6221 connection closed.")
@@ -136,7 +141,7 @@ def run_combined_measurement():
136141
if lakeshore_350:
137142
try:
138143
print("Shutting down Lakeshore 350...")
139-
lakeshore_350.write('RANGE 0') # Turn off heater
144+
lakeshore_350.write('RANGE 0') # Turn off heater
140145
time.sleep(0.5)
141146
lakeshore_350.close()
142147
print("Lakeshore 350 connection closed.")
@@ -145,6 +150,7 @@ def run_combined_measurement():
145150

146151
print("\n--- Measurement Complete ---")
147152

153+
148154
# --- Main execution block ---
149155
if __name__ == "__main__":
150156
run_combined_measurement()
Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
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+
# -------------------------------------------------------------------------------
88
import pyvisa
99
import time
1010

1111
# --- Configuration ---
1212
# Use constants for easier changes later.
1313
KEITHLEY_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+
1718
def 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("\nShutting 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("\nShutting 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 ---
124135
if __name__ == "__main__":
125136
run_delta_measurement()

0 commit comments

Comments
 (0)