zoukankan      html  css  js  c++  java
  • Python visa 读取powermeter


    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 " Remain 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()


  • 相关阅读:
    前端百度地图开发使用总结
    换电脑后如何同步git本地仓库及分支
    vue mint-ui初次使用总结
    git学习入门总结
    深夜,当音乐响起,数据在MySQL中静静的疯狂计算
    且说Tomcat、Servlet、JSP和Spring
    国庆与中秋7天死磕Web的时光
    Android编程初涉,以控制摄像头为例
    谈现阶段如何形成可行的健康生活习惯方案
    说Java网络编程
  • 原文地址:https://www.cnblogs.com/xia-dong/p/12667958.html
Copyright © 2011-2022 走看看