pythonpyvisa

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


I'm trying to control a gp-switch card in slot three of an AG34970a using pyvisa.

When I enter this in the console.

dac.write("ROUTe:CLOSe (@301,302,303,304,305,306,307,308)")

the switch functions normally and quietly, and the console outputs

(48L, <StatusCode.success: 0))

my device is named dac.

but when I try to use a string variable instead the response in the console is the same but the instrument beeps and does not close the switches. The error code on the device is error 103 which I believe means an invalid separator but since one works I don't understand why the other wouldn't.

switch_str = '"ROUTe:CLOSe (@301,302,303,304,305,306,307,308)"'
dac.write(switch_str)

Is it not possible to use a variable in conjunction with the .write command?

I figured it out. Well sort of. Whatever was going wrong was happening when I was concatenating my string before passing it to the device. I ended up being more explicit with the parts of the write command which were going to remain constant and only passing in the list of channels to close. I'm still not sure why it wasn't working before but it is now. Thanks for all your help.

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

Solution

  • Try wrapping the string with only single or double quotations and not both. It may be telling you that " is not a valid separation parameter (: , ;).