实现自动化控制,电源为基础的模块,下面为大家讲解电源66319D的控制逻辑。
新建底层控制逻辑
在文件basis_contorl.py中写入仪器控制底层代码,代码如下:
import tkinter.messagebox
import pyvisaclass InstrumentControl(object):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
编写好底层控制代码,在文件instrument_66319d.py写入电源66139D的控制指令,代码如下:
from instrument_control.basis_contorl import InstrumentControlclass D66319(InstrumentControl):def __init__(self, equipment_name: str, equipment_visa: str):super(D66319, self).__init__(equipment_name, equipment_visa)def query_equipment_info(self):"""查询设备信息:return: 设备信息"""result = self.command_query("*IDN?")return resultdef switch_display(self, interface: int):"""切换显示界面:param interface: 1 or 2"""self.command_write(f"DISPLAY:CHANNEL {interface}")def set_output_2(self, output_2: int):"""设置output2的输出功率:param output_2: 1,2....."""self.command_write(f"OUTP2 {output_2};*OPC?")def set_output(self, output: int):"""设置output1的输出功率:param output: 1,2....."""self.command_write(f"OUTP1 {output};*OPC?")def set_voltage_2(self, voltage_2: int):"""设置二口的电压:param voltage_2: 1,2....."""self.command_write(f'VOLT2 {voltage_2}')def set_voltage(self, voltage):"""设置一口的电压:param voltage: 1,2....."""self.command_write(f'VOLT1 {voltage}')def open_output_2(self):"""打开二口的输出开关"""self.command_write('OUTP2 ON')def open_output(self):"""打开一口的输出开关"""self.command_write('OUTP1 ON')def query_maximum_voltage(self):"""查询可编程的最大输出电压:return: 电压值"""result = self.command_query("VOLT? MAX").replace("\n", '')return resultdef open_over_voltage_protection(self):"""开启过压保护"""self.command_write("CURR:PROT:ON")def close_over_voltage_protection(self):"""关闭过压保护"""self.command_write("CURR:PROT:OFF")def set_output_resistance(self, output_resistance: int):"""设置输出电阻:param output_resistance: 输出电阻值"""self.command_write(f"RES {output_resistance}")def query_average_output_voltage(self):"""查询平均输出电压:return: 均输出电压"""result = self.command_query("MEAS:VOLT?")return resultdef query_average_output_current(self):"""查询平均输出电流:return: 平均输出电流"""result = self.command_query("MEAS:CURR?")return resultdef query_average_output_current_2(self):"""查询二口的平均输出电流:return: 二口的平均输出电流"""result = self.command_query("MEAS:CURR2?")return resultdef set_samples(self, samples: int):"""设置采样的样本数量:param samples: 样本数量"""self.command_write(f"SENS:SWE:POIN {samples}")def set_time_interval(self, time_interval: float):"""设置每个样本的间隔时间:param time_interval: 间隔时间"""self.command_write(f"SENS:SWE:TINT {time_interval}")def set_measurement_ranges(self, measurement_range: str):"""设置电流测试范围(3A:0~MAX, 1A:0~1, 0.02:0~0.02):param measurement_range: 测试范围"""self.command_write(f"SENS:CURR:RANG {measurement_range}")def query_measurement_ranges(self):"""查询电流测试范围:return: 电流测试范围"""result = self.command_query("SENS:CURR:RANG?")return resultdef set_measurement_detectors(self, measurement_detectors):"""设置电流探测器类型(AC_DC, DC):param measurement_detectors: 电流探测器类型"""self.command_write(f"SENS:CURR:DET {measurement_detectors}")def query_measurement_detectors(self):"""查询电流探测器类型:return: 电流探测器类型"""result = self.command_query("SENS:CURR:DET?")return resultdef query_voltage_rms_measurement(self, electric_type):"""查询电压的rms测量值(AC:交流电, DC:直流电, AC_DC:直流加交流, DC:直流):param electric_type: 电流类型:return: 电压的rms测量值"""result = self.command_query(f"MEAS:VOLT:{electric_type}?")return resultdef query_current_rms_measurement(self, electric_type):"""查询电流的rms测量值(AC:交流电, DC:直流电, AC_DC:直流加交流, DC:直流):param electric_type: 电流类型:return: 电流的rms测量值"""result = self.command_query(f"MEAS:CURR:{electric_type}?")return resultdef query_voltage_max_min(self):"""查询电压的脉冲或交流波形的最大或最小值:return: 电压的脉冲或交流波形的最大或最小值"""result_max = self.command_query("FETC:VOLT:MAX?")result_min = self.command_query("FETC:VOLT:MIN?")return result_max, result_mindef query_current_max_min(self):"""查询电流的脉冲或交流波形的最大或最小值:return: 电流的脉冲或交流波形的最大或最小值"""result_max = self.command_query("FETC:CURR:MAX?")result_min = self.command_query("FETC:CURR:MIN?")return result_max, result_mindef query_average_voltage(self):"""测试平均电压:return: 平均电压"""result = self.command_query("MEAS:DVM:DC?")return resultdef query_rms_voltage(self):"""测试rms电压:return: rms电压"""result = self.command_query("MEAS:DVM:ACDC?")return result
控制仪器时先设置控制对象,具体例子如下:
set_equipment_name = "D66319"set_equipment_visa = "仪器的GPIB地址"d66319_object = D66319(equipment_name=set_equipment_name, equipment_visa=set_equipment_visa)
关于电源66319D的控制方法就到这里了,博主后续还会更新不同仪器的控制方法,各位敬请期待吧,有疑问欢迎找博主解答,我是活动的笑脸。