我正在尝试使用pyvisa控制AG34970a插槽3中的gp交换卡。
当我在控制台中输入此内容时。
dac.write("ROUTe:CLOSe (@301,302,303,304,305,306,307,308)")
开关正常且安静地运行,并且控制台输出
(48L, <StatusCode.success: 0))
我的设备名为dac。
但是,当我尝试使用字符串变量来代替时,控制台中的响应是相同的,但是仪器发出蜂鸣声并且没有关闭开关。设备上的错误代码是错误103,我认为这意味着一个无效的分隔符,但是由于一个有效,我不理解为什么另一个无效。
switch_str = '"ROUTe:CLOSe (@301,302,303,304,305,306,307,308)"'
dac.write(switch_str)
不能将变量与.write命令一起使用?
我想到了。好吧。当我在将字符串传递给设备之前对其进行串联时,出了什么问题。最后,我对write命令的各个部分变得更加明确,这些部分将保持不变,只传递要关闭的通道列表。我仍然不确定为什么以前不起作用,但现在却不起作用。感谢你的帮助。
fields = ['301', '302', '303', '304', '305', '306', '307']
voltage = dac.query_ascii_values('MEAS:VOLT:DC? AUTO,DEF,(@101:107)')
#convert the resulting voltage values into floats
flvolts=[float(i) for i in voltage]
#create a dictionary with the fields and corresponding voltage values
dictionary=dict(zip(fields, flvolts))
#evaluate the voltage list to determine the lowest value
minval = min(flvolts)
#produce a list of gp-switch channels that need to be closed to get cells balanced
#targetval was created further up. initially it is the same as minval but minval can
#change. Targetval shouldn’t
switch_list=({k for (k,v) in dictionary.items() if v >= targetval})
#begin generating the string to be sent to the device by converting the floats to strings
str_list=[str(i) for i in switch_list]
#Set up an empty list
formatted_str_list=""
#Format str_list into a string of comma separated numbers.
for i in str_list:
formatted_str_list += str(i) + ","
#instruct the device to close the channels that need drained
#because they are higher than the minval
dac.write("ROUTe:CLOSe (@" + formatted_str_list + "308)")
最佳答案
尝试仅用单引号或双引号而不是将双引号引起来的字符串进行包装。可能会告诉您“不是有效的分隔参数(:,;)。