python系列006

发布时间 2023-04-06 17:43:27作者: zlib001
  1. 先使用一个文件控制设备
import time
# import pyvisa
from pimi import PiDevice

device = PiDevice("ASRL3::INSTR")   #创建实例,并手动写入地址参数,可以借助NI Max读取
whoisPower = device.PiPower()       #调用类中的方法,电源地址

# Send a command to the instrument
# setnum = 1
for setnum in range(5):
    setnum += setnum
    numadd = 1 + setnum / 10
    # whoisPower.write(':VSET1:1')  #
    whoisPower.write(f'VSET1:{numadd}')         #设置电压输出
    # whoisPower.write(f'ISET1:{numadd}')         #设置电流输出
    time.sleep(1)
  1. 变量名不直观,修改下吧
    改进,由于range()不支持float,所以可以放缩处理
import time
# import pyvisa
from pimi import PiDevice

device = PiDevice("ASRL3::INSTR")   #创建实例,并手动写入地址参数,可以借助NI Max读取
whoisPower = device.PiPower()       #调用类中的方法,电源地址

startVoltage = 10
endVoltage = 19
# Send a command to the instrument
for voltage in range(startVoltage, endVoltage, 1):
    voltage = voltage / 10
    whoisPower.write(f'VSET1:{voltage}')         #设置电压输出
    time.sleep(1)
  1. 进一步优化
    可以使用numpy模块生成指定步长的电压序列值,numpy.arange()函数用于在起始值和结束值之间以步长生成序列。
import time
import numpy as np
# import pyvisa
from pimi import PiDevice

device = PiDevice("ASRL3::INSTR")   #创建实例,并手动写入地址参数,可以借助NI Max读取
whoisPower = device.PiPower()       #调用类中的方法,电源地址

startVoltage = 1.0
endVoltage = 1.8
stepSize = 0.1
# Send a command to the instrument
for voltage in np.arange(startVoltage, endVoltage, stepSize):
    # voltage = voltage / 10
    whoisPower.write(f'VSET1:{voltage}')         #设置电压输出
    time.sleep(1)
  1. 使用一个函数,传递这3个值
device = PiDevice("ASRL3::INSTR")   #创建实例,并手动写入地址参数,可以借助NI Max读取
whoisPower = device.PiPower()       #调用类中的方法,电源地址

def set_voltage(startVoltage, endVoltage, stepSize):
    # Send a command to the instrument
    for voltage in np.arange(startVoltage, endVoltage, stepSize):
        # voltage = voltage / 10
        whoisPower.write(f'VSET1:{voltage}')         #设置电压输出
        time.sleep(2)

# startVoltage = float(input("Enter the starting voltage: "))
# endVoltage = float(input("Enter the ending voltage: "))
# stepSize = float(input("Enter the step size: "))
set_voltage(1.0, 2.0, 0.1)