Skip to content

Commit f4043d9

Browse files
some fixes to 6517B and LCR
1 parent ca9d0c2 commit f4043d9

3 files changed

Lines changed: 69 additions & 217 deletions

File tree

Keithley_6517B/Pyroelectricity/Backends/Current_K6517B_Simple_Backend_v10.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Purpose: Current Measurement Backend
44
# Author: Prathamesh K Deshmukh
55
# Created: 03-03-2024
6-
# Updates: V1.3 (Fixed DataFrame saving bug)
6+
# Updates: V1.4 (Fixed DataFrame saving bug)
77
#-------------------------------------------------------------------------------
88

99
import time
@@ -42,15 +42,21 @@
4242
# Graceful Exit on Ctrl+C
4343
print("\nMeasurement stopped by User.")
4444

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")
45+
# --- FIX: Create the DataFrame before saving ---
46+
if t and I:
47+
data_df = pd.DataFrame({"Timestamp": t, "Current (A)": I})
48+
data_df.to_csv("demo_data.dat", index=False)
49+
print(f"Data saved to file: demo_data.dat")
50+
else:
51+
print("No data collected to save.")
4952

5053
# Shutdown Sequence
51-
time.sleep(0.5)
52-
keithley.shutdown() # Ramps current to 0 and disables output
53-
print("Keithley closed.")
54+
try:
55+
time.sleep(0.5)
56+
keithley.shutdown() # Ramps current to 0 and disables output
57+
print("Keithley closed.")
58+
except Exception as e:
59+
print(f"Error closing instrument: {e}")
5460

5561
except Exception as e:
56-
print(f"Error with Keithley: {e}")
62+
print(f"Error with Keithley: {e}")

LCR_Keysight_E4980A/Backends/CV_KE4980A_Simple_Backend_v10.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020

2121
filename = "E:/Prathamesh/Python Stuff/CV/CV_Measurements/" + str(name) + "_freq_" + str(freq) + "_volt_" + str(V) + "_V_step_" + str(V_step) + "_Loops" + str(loop) + ".txt"
2222

23-
#---------------------------------------------------------------
24-
2523
loop_ind_new = 0
2624
protocol_list = []
2725
V_list = []
2826
C_list = []
2927
loop_list = []
3028

3129
#---------------------------------
32-
# Initialize Instrument
3330
try:
3431
rm = pyvisa.ResourceManager()
3532
my_instrument = rm.open_resource("GPIB::17")
@@ -42,21 +39,16 @@
4239
my_instrument.write('*RST; *CLS')
4340
my_instrument.write(':DISP:ENAB')
4441
time.sleep(2)
45-
4642
my_instrument.write(':INIT:CONT')
4743
my_instrument.write(':TRIG:SOUR EXT')
4844
time.sleep(2)
49-
5045
my_instrument.write(':APER MED')
5146
my_instrument.write(':FUNC:IMP:RANGE:AUTO ON')
5247
time.sleep(2)
53-
5448
my_instrument.write(':MMEM EXT')
5549
time.sleep(2)
56-
5750
my_instrument.write(':MEM:DIM DBUF, ' + str(100))
5851
time.sleep(1)
59-
6052
my_instrument.write(':MEM:FILL DBUF')
6153
time.sleep(2)
6254
my_instrument.write(':MEM:CLE DBUF')
@@ -65,76 +57,66 @@
6557
time.sleep(2)
6658
my_instrument.write(':VOLT:LEVEL ' + str(V_ac))
6759
time.sleep(2)
68-
6960
except Exception as e:
70-
print(f"Instrument initialization failed: {e}")
71-
# In a real scenario, you might want to exit here
72-
pass
61+
print(f"Initialization error: {e}")
62+
# Continue for simulation purposes, or exit in real usage
7363

7464
#---------------------------------
7565

7666
# LCR_fcn for the actual measurements
7767
def LCR_fcn(volt_ind):
78-
# Removed unused globals that caused linting errors
68+
# --- FIX: Removed unused globals ---
7969
global v1
8070
global output1
8171

8272
my_instrument.write(':BIAS:VOLTage:LEVel ' + str(volt_ind))
83-
8473
time.sleep(5)
8574
my_instrument.write(':INITiate[:IMMediate]')
86-
8775
time.sleep(2)
8876

8977
output1 = LCR.values(":FETCh:IMPedance:FORMatted?")
9078
time.sleep(2)
9179

92-
C_list.append(output1[0])
80+
if output1:
81+
C_list.append(output1[0])
82+
9383
v1 = my_instrument.query(':BIAS:VOLTage:LEVel?')
9484
V_list.append(v1)
9585
time.sleep(4)
9686

97-
print("Output: " + str(output1) + " | Volt : " + str(v1) + " | Cp : " + str(output1[0]) + " | Loop: " + str(loop_ind_new) + " | ")
98-
87+
print("Output: " + str(output1) + " | Volt : " + str(v1) + " | Loop: " + str(loop_ind_new))
9988

10089
# Proto_fcn for the measurements protocol
10190
def Proto_fcn():
10291
global loop_ind_new
103-
# Removed unused global protocol_list
104-
10592
loop_ind_new += 1
10693

107-
# First protocol 0 to V {A}
94+
# Protocol Steps
10895
for v_ind in np.arange(0, V + V_step, V_step):
10996
LCR_fcn(v_ind)
11097
loop_list.append(loop_ind_new)
11198
protocol_list.append("A")
11299

113-
# Second protocol V to 0 {B}
114100
for v_ind in np.arange(V, 0 - V_step, -V_step):
115101
LCR_fcn(v_ind)
116102
loop_list.append(loop_ind_new)
117103
protocol_list.append("B")
118104

119-
# Third protocol 0 to -V {C}
120105
for v_ind in np.arange(0, -V - V_step, -V_step):
121106
LCR_fcn(v_ind)
122107
loop_list.append(loop_ind_new)
123108
protocol_list.append("C")
124109

125-
# Second protocol -V to 0 {D}
126110
for v_ind in np.arange(-V, 0 + V_step, V_step):
127111
LCR_fcn(v_ind)
128112
loop_list.append(loop_ind_new)
129113
protocol_list.append("D")
130114

131-
132115
# Loop_fcn for the looping number of times
133116
def Loop_fcn(loop):
134117
for loop_ind in range(loop):
135118
Proto_fcn()
136119

137-
138120
if __name__ == "__main__":
139121
try:
140122
Loop_fcn(loop)
@@ -144,25 +126,20 @@ def Loop_fcn(loop):
144126
time.sleep(1)
145127
LCR.shutdown()
146128

147-
# Save Data
148129
data_dict = {'Volt': V_list, 'Cp': C_list, 'Loop': loop_list, 'Protocol': protocol_list}
149130
df = pd.DataFrame(data_dict)
150131

151132
try:
152133
df.to_csv(filename, sep=',', index=False, encoding='utf-8')
153134
print(f"Data saved to {filename}")
154135
except Exception:
155-
fallback_name = "LCR_Data_Backup.csv"
156-
df.to_csv(fallback_name, sep=',', index=False)
157-
print(f"Could not save to specified path. Saved to {fallback_name} instead.")
136+
df.to_csv("LCR_Backup.csv", index=False)
137+
print("Saved to LCR_Backup.csv")
158138

159-
# Plotting
160139
if V_list and C_list:
161140
plt.scatter(V_list, C_list)
162-
plt.title("Cp vs V , Loops:" + str(loop) + " V_max:" + str(V) + " step size : " + str(V_step))
163-
plt.xlabel("V")
164-
plt.ylabel("Cp")
141+
plt.title("Cp vs V")
165142
plt.show()
166143

167144
except Exception as e:
168-
print(f"An error occurred: {e}")
145+
print(f"Error: {e}")

0 commit comments

Comments
 (0)