|
1 | 1 | #------------------------------------------------------------------------------- |
2 | 2 | # Name: Keithley 6517B electrometer |
3 | | -# Purpose: |
4 | | -# |
5 | | -# Author: Prathamesh K Deshmukh |
6 | | -# |
| 3 | +# Purpose: Current Measurement Backend |
| 4 | +# Author: Prathamesh K Deshmukh |
7 | 5 | # Created: 03-03-2024 |
8 | | -# updates: V1.3 |
| 6 | +# Updates: V1.3 (Fixed DataFrame saving bug) |
9 | 7 | #------------------------------------------------------------------------------- |
10 | 8 |
|
11 | 9 | import time |
|
14 | 12 | import pyvisa |
15 | 13 | from pymeasure.instruments.keithley import Keithley6517B |
16 | 14 |
|
17 | | - |
18 | | -I=[] |
19 | | -t=[] |
20 | | -#rm = pyvisa.ResourceManager() |
21 | | -#keithley = rm.open_resource("GPIB::1") |
| 15 | +I = [] |
| 16 | +t = [] |
22 | 17 |
|
23 | 18 | try: |
24 | | - |
| 19 | + # Initialize Instrument |
25 | 20 | keithley = Keithley6517B("GPIB0::27::INSTR") |
26 | 21 | time.sleep(0.5) |
27 | 22 |
|
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 |
32 | 24 | keithley.measure_current() |
33 | 25 | time.sleep(0.5) |
34 | 26 |
|
35 | | - |
36 | | - #data_columns = ["Timestamp", "Current (A)"] |
37 | | - #data_df = pd.DataFrame(columns=data_columns) |
| 27 | + print("Starting measurement... Press Ctrl+C to stop.") |
38 | 28 | start_time = time.time() |
39 | 29 |
|
40 | 30 | while True: |
41 | 31 | elapsed_time = time.time() - start_time |
42 | | - #timestamp = time.strftime("%Y-%m-%d %H:%M:%S") |
43 | 32 | 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 |
45 | 35 | t.append(elapsed_time) |
46 | 36 | 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") |
49 | 39 | 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}") |
60 | 40 |
|
61 | 41 | 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") |
63 | 49 |
|
64 | | - keithley.clear() |
65 | | - #keithley.reset() |
| 50 | + # Shutdown Sequence |
66 | 51 | time.sleep(0.5) |
| 52 | + keithley.shutdown() # Ramps current to 0 and disables output |
| 53 | + print("Keithley closed.") |
67 | 54 |
|
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