仪表公共
新建一个py文件,命名为InsPublic,用于后续其他仪表导入
from pyvisa import *__author__ = 'Bob'class InsPublicVisa():def __init__(self):self.rm = ResourceManager()def insOpen(self,insAddr):insAddr = 'TCPIP::%s::inst0::INSTR' % insAddrtry:self.insX = self.rm.open_resource(insAddr)except:print('insOpen failed,pls check insAddr')def insID(self):return self.insX.query('*IDN?')def insClose(self):self.insX.close()if __name__ == '__main__':insAddr = '192.168.52.190'ps = InsPublicVisa()ps.insOpen(insAddr)print(ps.insID())if ps.insID().find('36311A'): # 以一台keysight电源为例进行测试print('yes')ps.insClose()
电源
from InsPublic import InsPublicVisa__author__ = 'Bob'class InsDCPSTriple(InsPublicVisa):def __init__(self):super(InsDCPSTriple, self).__init__()passdef setVolt(self,chanel,volt):'''设置指定通道电压'''self.insX.write('APPL CH%d, %f, %d' % (chanel,volt,chanel))def setVoltandON(self,chanel,volt):'''设置通道电压并同时开启通道'''self.insX.write('APPL CH%d, %f, %d' % (chanel,volt,chanel))self.insX.write('OUTP ON,(@%d)' % chanel)def setVoltandOFF(self,chanel,volt):'''设置通道电压并同时关闭通道'''self.insX.write('APPL CH%d, %f, %d' % (chanel,volt,chanel))self.insX.write('OUTP OFF,(@%d)' % chanel)def setCurrMax(self,chanel,currmax):'''设置通道电流上限值'''self.insX.write('CURR %f, (@%d)' % (currmax,chanel))def getVolt(self,chanel):'''查询指定通道电压,当通道关闭时,查询电压为0,当通道开启时,查询电压为当前值'''volt = self.insX.query('MEAS:VOLT? (@%d)' % chanel) # 和下面两行代码等效# self.insX.write('MEAS:VOLT? (@%d)' % chanel)# volt = self.insX.read()return voltdef getCurr(self,chanel):'''查询指定通道电流'''curr = self.insX.query('MEAS:CURR? (@%d)' % chanel)return currdef chanelON(self,chanel):'''开启指定通道'''self.insX.write('OUTP ON,(@%d)' % chanel)def chanelOFF(self,chanel):'''关闭指定通道'''self.insX.write('OUTP OFF,(@%d)' % chanel)if __name__ == '__main__':ipAddr = '192.168.52.190'ps = InsPublicVisa()# ps = InsDCPSTriple()ps.insOpen(ipAddr)# 实例化对象和电源型号匹配的电源类if ps.insID().find('36311A'):ps = InsDCPSTriple()# elif ps.insID().find('xxx'):# passelse:passps.insOpen(ipAddr)ps.setVoltandON(1, 1.2)ps.setVoltandOFF(2, 1.5)ps.setVoltandON(3, 1.8)ps.setCurrMax(1,2)print(ps.getVolt(1))print(ps.getCurr(1))print(ps.getVolt(3))print(ps.getCurr(3))ps.insClose()
数字万用表
(带温度测试功能)
from InsPublic import InsPublicVisa__author__ = 'Bob'class InsDmm(InsPublicVisa):def __init__(self):super(InsDmm, self).__init__()passdef getVoltAuto(self):# volt = self.insX.query('READ?')'''DC电压测量范围和测量精度根据测试对象自动调整,每次自动调整的代价就是牺牲一定的测试速度'''volt = self.insX.query('MEAS:VOLT:DC?')return voltdef getVoltManual(self,range,resolution):'''手动设置DC电压测量范围和测量精度,为了兼顾测量速度和精度,一般推荐以下范围精度的组合range:10V,resolution:0.0001V(0.1mV)range:1V,resolution:0.00001V(0.01mV)'''volt = self.insX.query('MEAS:VOLT:DC? %d, %f'% (range, resolution))return voltdef getTemp(self):'''利用DMM的温度测试功能,获取配套热电偶的温度(34461一般不配热电偶,需要自购)此处选用华控方壳PT100 18B20铂热电阻温度传感器探头(A级)与34461进行配套该PT100铂热电阻温度传感器性能可靠,价格便宜,值得推荐'''temp = self.insX.query('MEAS:TEMP? RTD')return temppass
if __name__ == '__main__':'''DCV test'''insAddr = '192.168.52.163'dmm1 = InsDmm()dmm1.insOpen(insAddr)print(dmm1.getVoltAuto())print(dmm1.getVoltManual(10, 1e-4))dmm1.insClose()'''Temp test'''insAddr = '192.168.52.191'dmm2 = InsDmm()dmm2.insOpen(insAddr)print(dmm2.getTemp())dmm2.insClose()
(以上所有代码经仪表实测OK)