Skip to content

Commit ac55890

Browse files
Update Current_K6517B_Simple_Backend_v10.py
df error fix
1 parent 0388ffc commit ac55890

1 file changed

Lines changed: 24 additions & 38 deletions

File tree

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#-------------------------------------------------------------------------------
22
# Name: Keithley 6517B electrometer
3-
# Purpose:
4-
#
5-
# Author: Prathamesh K Deshmukh
6-
#
3+
# Purpose: Current Measurement Backend
4+
# Author: Prathamesh K Deshmukh
75
# Created: 03-03-2024
8-
# updates: V1.3
6+
# Updates: V1.3 (Fixed DataFrame saving bug)
97
#-------------------------------------------------------------------------------
108

119
import time
@@ -14,57 +12,45 @@
1412
import pyvisa
1513
from pymeasure.instruments.keithley import Keithley6517B
1614

17-
18-
I=[]
19-
t=[]
20-
#rm = pyvisa.ResourceManager()
21-
#keithley = rm.open_resource("GPIB::1")
15+
I = []
16+
t = []
2217

2318
try:
24-
19+
# Initialize Instrument
2520
keithley = Keithley6517B("GPIB0::27::INSTR")
2621
time.sleep(0.5)
2722

28-
#keithley.apply_current() # Sets up to source current
29-
#keithley.current_range = 10e-3 # Sets the source current range to 10 mA
30-
#keithley.compliance_voltage = 10 # Sets the compliance voltage to 10 V
31-
#keithley.enable_source() # Enables the source output
23+
# Setup Measurement
3224
keithley.measure_current()
3325
time.sleep(0.5)
3426

35-
36-
#data_columns = ["Timestamp", "Current (A)"]
37-
#data_df = pd.DataFrame(columns=data_columns)
27+
print("Starting measurement... Press Ctrl+C to stop.")
3828
start_time = time.time()
3929

4030
while True:
4131
elapsed_time = time.time() - start_time
42-
#timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
4332
current = keithley.current # Read current in Amps
44-
#data_df = data_df.append({"Timestamp": elapsed_time, "Current (A)": current}, ignore_index=True)
33+
34+
# Store data in lists
4535
t.append(elapsed_time)
4636
I.append(current)
47-
print("Time: " +str(elapsed_time)+"\t\t\t|\t\t\t Current: "+str(current)+" A")
48-
37+
38+
print("Time: " + str(elapsed_time) + "\t\t\t|\t\t\t Current: " + str(current) + " A")
4939
time.sleep(2)
50-
print("Measurement stopped...")
51-
52-
data_df.to_csv("demo_data.dat", index=False)
53-
print(f"Data saved to file : demo_data.dat")
54-
55-
56-
57-
58-
except Exception as e:
59-
print(f"error with keithley : {e}")
6040

6141
except KeyboardInterrupt:
62-
time.sleep(0.5)
42+
# Graceful Exit on Ctrl+C
43+
print("\nMeasurement stopped by User.")
44+
45+
# --- FIX IS HERE: Define data_df before using it ---
46+
data_df = pd.DataFrame({"Timestamp": t, "Current (A)": I})
47+
data_df.to_csv("demo_data.dat", index=False)
48+
print(f"Data saved to file: demo_data.dat")
6349

64-
keithley.clear()
65-
#keithley.reset()
50+
# Shutdown Sequence
6651
time.sleep(0.5)
52+
keithley.shutdown() # Ramps current to 0 and disables output
53+
print("Keithley closed.")
6754

68-
keithley.shutdown() # Ramps the current to 0 mA and disables output
69-
print("\n\nkeithley closed")
70-
print("\n Measurement stopped by User ")
55+
except Exception as e:
56+
print(f"Error with Keithley: {e}")

0 commit comments

Comments
 (0)