import visa
import os,csv,sys,time
from getopt import getopt
def Useage():
print sys.argv[0].split(‘/‘)[-1],‘-m mode -t seconds‘
print "mode: [voltage|current|power|all]"
sys.exit()
class PowerMeter(object):
def __init__(self):
v = visa.ResourceManager()
self.PM=v.open_resource(‘ASRL7::INSTR‘)
self.PM.write("NUMERIC:FORMAT ASCII")
if self.PM.query("*IDN?").find("WT310") >= 1:
self.ID = "WT310"
self.MODE = "NUMERIC"
else:
self.ID = "WT210"
self.MODE = "MEASURE"
def GetVoltage(self):
self.voltage = self.PM.query("{}:NORMAL:VALUE? 1".format(self.MODE))
voltage=float(self.voltage)
return voltage
def GetCurrent(self):
self.current = self.PM.query("{}:NORMAL:VALUE? 2".format(self.MODE))
current=float(self.current)
return current
def GetPower(self):
self.power = self.PM.query("{}:NORMAL:VALUE? 3".format(self.MODE))
power=float(self.power)
return power
if __name__ == "__main__":
csv_file="Power_meter.csv"
header=[]
lines=[]
pwr=PowerMeter()
try:
opts,args=getopt(sys.argv[1:],‘hm:t:‘,[‘help‘,‘mode‘])
except:
Useage()
if len(opts) != 2 or len(args) != 0 :
Useage()
for o, v in opts:
if o in (‘-h‘,‘--help‘):
Useage()
sys.exit()
elif o in (‘-m‘,‘--mode‘):
Mode=v
elif o == ‘-t‘:
Sample_time=int(v)
if Mode == "voltage":
header.append("Voltage")
elif Mode == "current":
header.append("Current")
elif Mode == "power":
header.append("Power")
elif Mode == "all":
header=("Voltage,Current,Power".split(‘,‘))
else:
print "value of -m error"
Useage()
for i in xrange(0,Sample_time):
print "\rRemain Time: {0}".format(Sample_time-i),
tmp=[]
for j in header:
if hasattr(pwr,‘Get‘+j):
tmp.append(getattr(pwr,‘Get‘+j)())
lines.append(tmp)
time.sleep(1)
with open(csv_file,‘wb+‘) as f:
writer=csv.writer(f)
writer.writerow(header)
for line in lines:
writer.writerow(line)
f.close()
原文:https://www.cnblogs.com/xia-dong/p/12667958.html