It was running using jupyter notebook, but there is no reason why it wouldn't work from regular python console.
import pyvisa
import numpy as np
import itertools
import time
import csv
import pandas as pd
import matplotlib.pyplot as plt
import datetime
rm = pyvisa.ResourceManager()
[print(x) for x in rm.list_resources()];
#use instrument names OR serial numbers
#Power supply settings
PSU_NAME = "DP832"
VIN_NUMBER_OF_SAMPLES = 20
VIN_MIN = 15
VIN_MAX = 22
IIN_MAX = 1
#DC load settings
DC_LOAD_NAME = "SDL1020"
IOUT_NUMBER_OF_SAMPLES = 20
IOUT_MIN = 0.01
IOUT_MAX = 0.3
DELAY = 0.5
CSV_FILE = 'measurements.csv'
EFFICIENCY_IMAGE = 'Efficiency.png'
VOLTAGE_IMAGE = 'Output.png'
#Creating measurement parameters
VIN = np.linspace (VIN_MIN,VIN_MAX,VIN_NUMBER_OF_SAMPLES)
IOUT = np.linspace (IOUT_MIN,IOUT_MAX,IOUT_NUMBER_OF_SAMPLES)
N= len(list(itertools.product(VIN,IOUT)))
print (f"Taking {N} measurements")
print (f"Expected time: {N*(DELAY+2)} seconds")
#Connecting to instruments
# Example usage:
#instrument_identifier = "DP832" # Or any other identifier
def find_instrument(instrument_identifier,rm):
instruments = rm.list_resources()
for instrument_name in instruments:
try:
instrument = rm.open_resource(instrument_name)
idn = instrument.query("*IDN?")
if instrument_identifier in idn:
print("Instrument found:")
print(idn)
return instrument
else:
instrument.close()
except:
pass
return None
PSU = find_instrument(PSU_NAME,rm)
DCL = find_instrument(DC_LOAD_NAME,rm)
def measure_point(vin, iin, iout):
PSU.write(f"APPL CH1,{vin},{iin}")
#if PSU.query(":OUTPut:STATe? CH1").find("ON") == -1:
# PSU.write(":OUTPut:STATe CH1,ON")
DCL.write(f":SOURce:CURRent:LEVel:IMMediate {iout}")
#if DCL.query(":SOURce:INPut?").find("ON") == -1:
# DCL.write(":SOURce:INPut ON")
time.sleep(DELAY)
a= PSU.query(":MEASure:CURRent:DC? CH1", 0.1)
b= PSU.query(":MEASure:VOLTage:DC? CH1", 0.1)
c= DCL.query(":MEASure:CURRent:DC?", 0.1)
d= DCL.query(":MEASure:VOLTage:DC?", 0.1)
a = float(a.strip())
b = float(b.strip())
c = float(c.strip())
d = float(d.strip())
#print ((a,b,c,d))
print(".", end="")
try:
eff = (c*d)/(a*b)*100
except ZeroDivisionError:
eff = 0
return vin,iout,eff,a,b,c,d
vin,iout = next(itertools.product(VIN, IOUT))
measure_point(vin,IIN_MAX,iout)
PSU.write(":OUTPut:STATe CH1,ON")
DCL.write(":SOURce:INPut ON")
with open(CSV_FILE,'w', newline='') as csvfile:
print (datetime.datetime.now())
headerList = ['VIN','IOUT','EFF', 'CIN', 'VIN_A', 'IOUT_A','VOUT']
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(headerList)
for vin,iout in itertools.product(VIN,IOUT):
#print (vin,iout)
m = measure_point(vin,IIN_MAX,iout)
writer.writerow(m)
#Turn off output
PSU.write("OUTP CH1,OFF")
DCL.write(":SOURce:INPut OFF")
#Beep
PSU.write(":SYSTem:BEEPer:IMMediate")
time.sleep(0.5)
PSU.write(":SYSTem:BEEPer:IMMediate")
time.sleep(0.5)
PSU.write(":SYSTem:BEEPer:IMMediate")
print (datetime.datetime.now())
print ("Measurements done")
df = pd.read_csv(CSV_FILE)
df
plt.tricontourf(df['VIN'], df['IOUT'], df['EFF'], cmap='seismic',levels=30)
plt.xlabel('Input voltage')
plt.ylabel('Output current')
plt.colorbar(label='Efficiency')
plt.savefig(EFFICIENCY_IMAGE) # Save the plot
plt.show()
plt.tricontourf(df['VIN'], df['IOUT'], df['VOUT'], cmap='viridis',levels=30)
plt.xlabel('Input voltage')
plt.ylabel('Output current')
plt.colorbar(label='Output voltage')
plt.savefig(VOLTAGE_IMAGE) # Save the plot
plt.show()