Hi there, first of all than you so much put putting python tutorial for how to read/write to measurement equipment. I’m new to python , I’m trying to used for loop to collect Voltage, Current and Power measurement from power supply and log it. I’m only familiar with simple for loop to loop through number but when it come with 3 different variable and reading its value for certain number of time. I’m stuck, not sure where to began. if you could give me an hint or some example I would appreciate very much.
thanks you Mr. James for replying to me and taking an interest to help me. I’m putting out constant voltage our and running for an hrs.
import pyvisa
import time
#from time import sleep
import os
dataString = time.strftime("%Y-%m-%d_%H%M")
filepath = "./data.csv"
rm = pyvisa.ResourceManager()
BK9801 = rm.open_resource('USB0::0xFFFF::0x9800::802287112766710021::INSTR')
#BK9801.write(':OUTP OFF') #star opsition
#BK9801.write('VOLTage:Level 110')
#BK9801.write('OUTP ON')
vMeasured = float(BK9801.query('MEAS:VOLT?'))
cMeasured = float(BK9801.query('MEAS:CURR?'))
wMeasured = float(BK9801.query('MEAS:POWer?'))
for i in vMeasured; i <10:
print(i)
#My voltage is constant and i'm running it for an hrs; here i wan't to create a for loop that loop throught vmeasured, c measured & wMeasured..
# and collect data for every min for an hrs
#for now i can only print it on console but i'm not able to loop through
#should i create an array and loop through
#print("Voltage SP(V) is =", vMeasured)
#for (i = cMeasured; i<=10; i++)
# print("Current(I) =", cMeasured)
#for (i = wMeasured; i<=10; i++)
# print("Power Consumption (W) =", wMeasured)
with open(filepath, "a") as file:
if os.stat(filepath).st_size == 0: #check for file empty
file.write("Setpoint [V], Current[I], Power[W]\n")
file.write("{:12.2f},{:13.5f},{:14.2f}\n".format(vMeasured, cMeasured, wMeasured))
file.close()
I can see you’re having trouble with the loop section. Correct me if I’m wrong here, but your for loop is trying to iterate through a float (vMeasured). To my knowledge this will not work (Although my Python is rusty)
What I think you’d want to do is set the supply voltage before the loop: supply.write(':APPL CH1,' + str(voltage_you_want_to_apply) + ',0.2') (not sure what that 0.2 is, my knowledge on the lab equipment coding is kind of not there, maybe Michael can help me out here)
And then you’d have a while loop that checks the time:
timeout = time.time() + 3600 #the number of seconds you need to record for
while time.time() < timeout:
vMeasured = float( dmm.query(':MEASure:VOLTage:DC?')
print(str(vMeasured))
sleep(5) #the interval you want to take measurements at
I don’t have the gear or code in front of me to test this, but it should give you an idea of what you want to move towards
Hi James,
thanks a lot it worked, for my purpose I change a little bit from your suggested code but it worked
vMeasured = float(BK9801.query(‘MEAS:VOLT?’))
cMeasured = float(BK9801.query(‘MEAS:CURR?’))
wMeasured = float(BK9801.query(‘MEAS:POWer?’))
timeout = time.time() + 160 #run time
while time.time() < timeout:
if time.time() > timeout:
BK9801.write(‘OUTP OFF’)
print(‘Run time over’)
else: #vMeasured = float(BK9801.query(‘MEAS:VOLT?’))
print(str(vMeasured)) #cMeasured = float(BK9801.query(‘MEAS:CURR?’))
print(str(cMeasured)) #wMeasured = float(BK9801.query(‘MEAS:POWer?’))
print(str(wMeasured))
sleep(10)
cheers mate
Bimal
2 Likes
And you can get our latest projects and tips straight away by following us on: