AG34970a的PyVisa控件device.write如果写入字符串变量会报错

PyVisa control of AG34970a device.write results in an error if writing a string variable

我正在尝试使用 pyvisa 控制 AG34970a 插槽三中的 gp-switch 卡。

当我在控制台中输入这个时。

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)")

尝试只用单引号或双引号而不是同时用双引号将字符串括起来。它可能告诉您 " 不是有效的分隔参数 (:, ;)。