0

I have a small question. I have to pass an argument to my measurement device to set a voltage value. My code to do this is as follow

from visa import *
import sys
inst = instrument("USB0::0x164E::0x0DAD::TW00004282")
inst.write("*rst; status:preset; *cls")
inst.write("CONF:VOLT:AC 1")

The above code configures the voltmeter to AC value 1 without any hassle. But it can only set the value 1. I tried making it more generic using the following code.

from visa import *
import sys
inst = instrument("USB0::0x164E::0x0DAD::TW00004282")
inst.write("*rst; status:preset; *cls")
a = 1
inst.write("CONF:VOLT:AC a")

But this piece of code returned an error.

My original code would look something like

from visa import *
import sys
inst = instrument(sys.argv[1]) #Passing USB address from client side
inst.write("*rst; status:preset; *cls")
a = sys.argv[2]  #Passing value of 'a' from the client side
inst.write("CONF:VOLT:AC a")

I would pass the argument value in the end from my client side, which is out of the context of this question.

Now is there another generic way to assign the value of a and then to pass it write function?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Spaniard89
  • 2,359
  • 4
  • 34
  • 55

1 Answers1

2

The "a" within your string is interpreted as a literal a. You should use:

inst.write("CONF:VOLT:AC %s" % sys.argv[2])

Or better convert it to an int and check it first:

volt = int(sys.argv[2])

# Check if volt is in a suitable range...
inst.write("CONF:VOLT:AC %d" % volt)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mata
  • 67,110
  • 10
  • 163
  • 162
  • What if i have to pass two agruments using argv in the same line, something like this `from visa import * import sys inst = instrument(sys.argv[1]) inst.write("*rst; status:preset; *cls") inst.write("voltage:UNIT Vrms") #inst.write("apply:SIN 1000, 0.5") inst.write("apply:SIN %s %s" % sys.argv[2] % sys.argv[3])` Then whhat should i do? is it correct that i am doing here or is there something i am missing? any help would be very much appreciated. – Spaniard89 May 08 '12 at 11:54
  • it's almost correct: `inst.write("apply:SIN %s %s" % (sys.argv[2], sys.argv[3]))` – mata May 08 '12 at 19:08