python控制仪器实现自动化常用pyvisa库,基本控制可大致分为创建仪器控制对象、写入控制指令、读取仪表信息和查询仪表状态,下面进行基本的讲解。
pyvisa库创建仪表控制对象
import tkinter.messagebox
import pyvisaclass InstrumentControl:inst = Nonedef __init__(self, equipment_name: str, equipment_visa: str, visa_dll='c:/Windows/System32/visa32.dll'):"""建立仪器初始对象@param equipment_name: 仪器名@param equipment_visa: 仪器visa地址@param visa_dll: visa.all地址"""try:self.rm = pyvisa.ResourceManager(visa_dll)print("打开visa32.dll文件成功")self.inst = self.open_by_name(equipment_name, equipment_visa)except Exception as e:tkinter.messagebox.showerror('错误', '在{}中找不到visa32.dll,请确认正确安装了NI-MAX和驱动')print(f"打开visa32.dll文件失败, 报错原因:{e}")def open_by_name(self, device_name, gpib_ip):"""仪表通讯@param device_name:@param gpib_ip:@return: 仪器控制对象"""self.inst = self.rm.open_resource(gpib_ip)print(self.inst)print("打开资源:" + gpib_ip)query_name_str = self.command_query("*IDN?")print("查询名称为:" + query_name_str)if device_name in query_name_str:print("打开仪表成功,打开的是:" + device_name)print("打开仪表成功,打开的是:" + device_name)return self.instreturn self.inst
写入控制指令
def command_write(self, write_cmd):"""命令写入@param write_cmd: 写入指令@return: 执行结果"""print("执行了写入指令:" + write_cmd)ret_res = self.inst.write(write_cmd)print("返回了:" + str(ret_res))return ret_res
读取仪表信息
def command_read(self, read_cmd):"""命令读取@param read_cmd: 读取指令@return: 读取信息"""print("执行了读取指令:" + read_cmd)ret_res = self.inst.read(read_cmd)print("返回了:" + str(ret_res))return ret_res
查询仪表状态
def command_query(self, query_cmd):"""命令查询@param query_cmd: 查询指令@return: 查询信息"""print("执行了查询指令:" + query_cmd)ret_res = self.inst.query(query_cmd).replace('\n', '')print("返回了:" + str(ret_res))return ret_res
完整代码如下:
import tkinter.messagebox
import pyvisaclass InstrumentControl:inst = Nonedef __init__(self, equipment_name: str, equipment_visa: str, visa_dll='c:/Windows/System32/visa32.dll'):"""建立仪器初始对象@param equipment_name: 仪器名@param equipment_visa: 仪器visa地址@param visa_dll: visa.all地址"""try:self.rm = pyvisa.ResourceManager(visa_dll)print("打开visa32.dll文件成功")self.inst = self.open_by_name(equipment_name, equipment_visa)except Exception as e:tkinter.messagebox.showerror('错误', '在{}中找不到visa32.dll,请确认正确安装了NI-MAX和驱动')print(f"打开visa32.dll文件失败, 报错原因:{e}")passdef open_by_name(self, device_name, gpib_ip):"""仪表通讯@param device_name:@param gpib_ip:@return: 仪器控制对象"""self.inst = self.rm.open_resource(gpib_ip)print(self.inst)print("打开资源:" + gpib_ip)query_name_str = self.command_query("*IDN?")print("查询名称为:" + query_name_str)if device_name in query_name_str:print("打开仪表成功,打开的是:" + device_name)print("打开仪表成功,打开的是:" + device_name)return self.instreturn self.instdef command_write(self, write_cmd):"""命令写入@param write_cmd: 写入指令@return: 执行结果"""print("执行了写入指令:" + write_cmd)ret_res = self.inst.write(write_cmd)print("返回了:" + str(ret_res))return ret_resdef command_read(self, read_cmd):"""命令读取@param read_cmd: 读取指令@return: 读取信息"""print("执行了读取指令:" + read_cmd)ret_res = self.inst.read(read_cmd)print("返回了:" + str(ret_res))return ret_resdef command_query(self, query_cmd):"""命令查询@param query_cmd: 查询指令@return: 查询信息"""print("执行了查询指令:" + query_cmd)ret_res = self.inst.query(query_cmd).replace('\n', '')print("返回了:" + str(ret_res))return ret_res
仪器控制底层写完,用程控电源D66319来进行调用,例子如下:
class D66319(InstrumentControl):def __init__(self, equipment_name: str, equipment_visa: str):"""电源D66319仪器控制:param equipment_name: 仪器名称:param equipment_visa: 仪器visa地址"""super(D66319, self).__init__(equipment_name, equipment_visa)def query_equipment_info(self):"""查询设备信息:return: """result = self.command_query("*IDN?")return resultdef open_output(self):"""打开电源输出开关"""self.command_write('OUTP1 ON')
基本的仪器控制逻辑,能实现自动化控制仪器,到此结束,我是活动的笑脸,有疑问欢迎找博主解答。