PyVISA
Release 1.3
Torsten Bronger
26 March 2008
Aachen, Germany
bronger@physik.rwth-aachen.de
Copyright c 2005 Torsten Bronger.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation
License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no
Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included as a separate file ‘LICENSE’ in the
PyVISA distribution.
Abstract
PyVISA enables you to control your measurement and test equipment – digital multimeters, motors, sensors and
the like. This document covers the easy-to-use visa module of the PyVISA package. It implements control of
measurement devices in a straightforward and convenient way. The design goal is to combine HT Basic’s simplicity
with Python’s modern syntax and powerful set of libraries.
PyVISA doesn’t implement VISA itself.
Instead, PyVISA provides bindings to the VISA library (a DLL or
“shared object” file). This library is usually shipped with your GPIB interface or software like LabVIEW. Alterna-
tively, you can download it from your favourite equipment vendor (National Instruments, Agilent, etc).
PyVISA is free software under the terms of the GPL. It can be downloaded at PyVISA’s project page. You
can report bugs there, too. Additionally, I’m happy about feedback from people who’ve given it a try. So far, we
have positive reports of various National Instruments GPIB adapters (connected through PCI, USB, and RS 232), the
Agilent 82357 A, and SRS lock-in amplifiers, for both Windows and Linux. However, I’d be really surprised about
negative reports anyway, due to the high abstraction level of PyVISA.
As far as USB instruments are concerned, you must make sure that they act as ordinary USB devices and not as
so-called HDI devices (like keyboard and mouse).
Contents
1 An example
1.1 Example for serial (RS232) device . . . .
. . . . .
1.2 A more complex example .
1.3 VISA resource names .
.
. . . . .
.
.
.
.
.
.
.
. . .
. . . . . . .
. . .
. . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 visa — module contents
2.1 Module functions .
.
.
.
2.2 Module classes
.
.
.
.
.
.
.
General devices .
.
GPIB devices .
Serial devices .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . .
. . .
. . .
. . . .
.
. . . .
. . . .
.
.
. . . .
. . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .
. . .
3 Common properties of instrument variables
. . . .
3.1 Timeouts
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
3
3
4
5
5
5
5
7
7
8
8
9
9
9
10
10
11
11
11
11
11
12
12
12
13
14
.
.
.
3.2 Chunk length .
3.3 Reading binary data .
.
.
.
.
3.4 Termination characters .
“delay” and “send end” .
Example .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . .
. . .
. . . .
.
.
.
.
.
. . . .
. . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .
. . . . .
. . .
. . .
4 Mixing with direct VISA commands
5 Installation
.
.
.
.
.
.
.
. . . .
5.1 Prerequisites .
.
5.2 Setting up the module .
.
.
.
.
.
Windows
Linux .
.
INI file for customisation .
Setting the VISA library in the program . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
.
. . . . .
.
. . . .
. .
. . . . .
. . . . .
. . . . .
. . . . .
. . .
. . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6 About PyVISA
Index
1 An example
Let’s go in medias res and have a look at a simple example:
from visa import *
my_instrument = instrument("GPIB::14")
my_instrument.write("*IDN?")
print my_instrument.read()
This example already shows the two main design goals of PyVISA: preferring simplicity over generality, and doing it
the object-oriented way.
Every instrument is represented in the source by an object instance. In this case, I have a GPIB instrument with
instrument number 14, so I create the instance (i. e. variable) called my instrument accordingly:
my_instrument = instrument("GPIB::14")
"GPIB::14" is the instrument’s resource name. See section 1.3 for a short explanation of that.
Then, I send the message “*IDN?” to the device, which is the standard GPIB message for “what are you?” or – in
some cases – “what’s on your display at the moment?”:
my_instrument.write("*IDN?")
Finally, I print the instrument’s answer on the screen:
2
1 An example
print my_instrument.read()
1.1 Example for serial (RS232) device
The only RS 232 device in my lab is an old Oxford ITC 4 temperature controller, which is connected through COM 2
with my computer. The following code prints its self-identification on the screen:
from visa import *
itc4 = instrument("COM2")
itc4.write("V")
print itc4.read()
Instead of separate write and read operations, you can do both with one ask() call. Thus, the above source code is
equivalent to
from visa import *
itc4 = instrument("COM2")
print itc4.ask("V")
It couldn’t be simpler. See section 2.2 for further information about serial devices.
1.2 A more complex example
The following example shows how to use SCPI commands with a Keithley 2000 multimeter in order to measure
10 voltages. After having read them, the program calculates the average voltage and prints it on the screen.
I’ll explain the program step-by-step. First, we have to initialise the instrument:
from visa import instrument
keithley = instrument("GPIB::12")
keithley.write("*rst; status:preset; *cls")
Here, we create the instrument variable keithley, which is used for all further operations on the instrument. Immediately
after it, we send the initialisation and reset message to the instrument.
The next step is to write all the measurement parameters, in particular the interval time (500 ms) and the number of
readings (10) to the instrument. I won’t explain it in detail. Have a look at an SCPI and/or Keithley 2000 manual.
1.1 Example for serial (RS232) device
3
interval_in_ms = 500
number_of_readings = 10
keithley.write("status:measurement:enable 512; *sre 1")
keithley.write("sample:count %d" % number_of_readings)
keithley.write("trigger:source bus")
keithley.write("trigger:delay %f" % (interval_in_ms / 1000.0))
keithley.write("trace:points %d" % number_of_readings)
keithley.write("trace:feed sense1; feed:control next")
Okay, now the instrument is prepared to do the measurement. The next three lines make the instrument waiting for a
trigger pulse, trigger it, and wait until it sends a “service request”:
keithley.write("initiate")
keithley.trigger()
keithley.wait_for_srq()
With sending the service request, the instrument tells us that the measurement has been finished and that the results are
ready for transmission. We could read them with ‘keithley.ask("trace:data?")’ however, then we’d get
NDCV-000.0004E+0,NDCV-000.0005E+0,NDCV-000.0004E+0,NDCV-000.0007E+0,
NDCV-000.0000E+0,NDCV-000.0007E+0,NDCV-000.0008E+0,NDCV-000.0004E+0,
NDCV-000.0002E+0,NDCV-000.0005E+0
which we would have to convert to a Python list of numbers. Fortunately, the ask_for_values() method does
this work for us:
voltages = keithley.ask_for_values("trace:data?")
print "Average voltage: ", sum(voltages) / len(voltages)
Finally, we should reset the instrument’s data buffer and SRQ status register, so that it’s ready for a new run. Again,
this is explained in detail in the instrument’s manual:
keithley.ask("status:measurement?")
keithley.write("trace:clear; feed:control next")
That’s it. 18 lines of lucid code. (Well, SCPI is awkward, but that’s another story.)
1.3 VISA resource names
If you use the function instrument(), you must tell this function the VISA resource name of the instrument you
want to connect to.
Generally, it starts with the bus type, followed by a double colon ‘::’, followed by the number within the bus. For
example,
GPIB::10
denotes the GPIB instrument with the number 10. If you have two GPIB boards and the instrument is connected to
4
1 An example
board number 1, you must write
GPIB1::10
As for the bus, things like “GPIB”, “USB”, “ASRL” (for serial/parallel interface) are possible. So for connecting to an
instrument at COM2, the resource name is
ASRL2
(Since only one instrument can be connected with one serial interface, there is no double colon parameter.) However,
most VISA systems allow aliases such as ‘COM2’ or ‘LPT1’. You may also add your own aliases.
The resource name is case-insensitive. It doesn’t matter whether you say ‘ASRL2’ or ‘asrl2’.
For further information, I have to refer you to a comprehensive VISA description like
http://www.ni.com/pdf/manuals/370423a.pdf.
2 visa — module contents
This section is a reference to the functions and classes of the visa module, which is the main module of the PyVISA
package.
2.1 Module functions
get_instruments_list([use aliases])
returns a list with all instruments that are known to the local VISA system.
instruments connected with the computer.
The boolean use aliases is True by default, which means that the more human-friendly aliases like “COM1”
instead of “ASRL1” are returned. With some VISA systems1 you can define your own aliases for each device,
e. g. “keithley617” for “GPIB0::15::INSTR”. If use aliases is False, only standard resource names
are returned.
If you’re lucky, these are all
instrument(resource name[, **keyw])
returns an instrument variable for the instrument given by resource name. It saves you from calling one of the
instrument classes directly by choosing the right one according to the type of the instrument. So you have one
function to open all of your instruments.
The parameter resource name may be any valid VISA instrument resource name, see section 1.3. In particular,
you can use a name returned by get_instruments_list() above.
All further keyword arguments given to this function are passed to the class constructor of the respective instru-
ment class. See section 2.2 for a table with all allowed keyword arguments and their meanings.
2.2 Module classes
General devices
class Instrument(resource name[, **keyw])
represents an instrument, e. g. a measurement device. It is independent of a particular bus system, i. e. it may
be a GPIB, serial, USB, or whatever instrument. However, it is not possible to perform bus-specific operations
1such as the “Measurement and Automation Center” by National Instruments
5
on instruments created by this class. For this, have a look at the specialised classes like GpibInstrument
(section 2.2).
The parameter resource name takes the same syntax as resource specifiers in VISA. Thus, it begins with the
bus system followed by “::”, continues with the location of the device within the bus system, and ends with an
optional “::INSTR”.
Possible keyword arguments are:
Keyword
Description
timeout
timeout in seconds for all device operations, see section 3.1. Default: 5
chunk size
Length of read data chunks in bytes, see section 3.2. Default: 20 kB
values format Data format for lists of read values, see section 3.3. Default: ascii
term char
send end
delay
lock
termination characters, see section 3.4. Default: None
whether to assert END after each write operation, see section 3.4. Default: True
delay in seconds after each write operation, see section 3.4. Default: 0
whether you want to have exclusive access to the device. Default: VI_NO_LOCK
For further information about the locking mechanism, see The VISA library implementation.
The class Instrument defines the following methods and attributes:
write(message)
writes the string message to the instrument.
read()
returns a string sent from the instrument to the computer.
read_values([format])
returns a list of decimal values (floats) sent from the instrument to the computer. See section 1.2 above. The list
may contain only one element or may be empty.
The optional format argument overrides the setting of values format. For information about that, see section 3.3.
ask(message)
sends the string message to the instrument and returns the answer string from the instrument.
ask_for_values(message[, format])
sends the string message to the instrument and reads the answer as a list of values, just as read_values()
does.
The optional format argument overrides the setting of values format. For information about that, see section 3.3.
clear()
resets the device. This operation is highly bus-dependent. I refer you to the original VISA documentation, which
explains how this is achieved for VXI, GPIB, serial, etc.
trigger()
sends a trigger signal to the instrument.
read_raw()
returns a string sent from the instrument to the computer. In contrast to read(), no termination characters are
checked or stripped. You get the pristine message.
timeout
The timeout in seconds for each I/O operation. See section 3.1 for further information.
term_chars
The termination characters for each read and write operation. See section 3.4 for further information.
send_end
Whether or not to assert EOI (or something equivalent, depending on the interface type) after each write opera-
tion. See section 3.4 for further information.
delay
Time in seconds to wait after each write operation. See section 3.4 for further information.
6
2 visa — module contents
values_format
The format for multi-value data sent from the instrument to the computer. See section 3.3 for further information.
GPIB devices
class GpibInstrument(gpib identifier[, board number[, **keyw]])
represents a GPIB instrument. If gpib identifier is a string, it is interpreted as a VISA resource name (sec-
tion 1.3). If it is a number, it denotes the device number at the GPIB bus.
The optional board number defaults to zero. If you have more that one GPIB bus system attached to the com-
puter, you can select the bus with this parameter.
The keyword arguments are interpreted the same as with the class Instrument.
Note: Since this class is derived from the class Instrument, please refer to section 2.2 for the basic operations.
GpibInstrument can do everything that Instrument can do, so it simply extends the original class with GPIB-
specific operations.
The class GpibInstrument defines the following methods:
wait_for_srq([timeout])
waits for a serial request (SRQ) coming from the instrument. Note that this method is not ended when another
instrument signals an SRQ, only this instrument.
The timeout argument, given in seconds, denotes the maximal waiting time. The default value is 25 (seconds).
If you pass None for the timeout, this method waits forever if no SRQ arrives.
class Gpib([board number])
represents a GPIB board. Although most setups have at most one GPIB interface card or USB-GPIB device
(with board number 0), theoretically you may have more. Be that as it may, for board-level operations, i. e.
operations that affect the whole bus with all connected devices, you must create an instance of this class.
The optional GPIB board number board number defaults to 0.
The class Gpib defines the following method:
send_ifc()
pulses the interface clear line (IFC) for at least 0.1 seconds.
Note: You needn’t store the board instance in a variable.
‘Gpib().send_ifc()’.
Instead, you may send an IFC signal just by saying
Serial devices
Please note that “serial instrument” means only RS232 and parallel port instruments, i. e. everything attached to COM
and LPT. In particular, it does not include USB instruments. For USB you have to use Instrument instead.
class SerialInstrument(resource name[, **keyw])
represents a serial instrument. resource name is the VISA resource name, see section 1.3.
The general keyword arguments are interpreted the same as with the class Instrument. The only difference
is the default value for term chars: For serial instruments, CR (carriage return) is used to terminate readings and
writings.
Note: Since this class is derived from the class Instrument, please refer to section 2.2 for all operations.
SerialInstrument can do everything that Instrument can do.
The class SerialInstrument defines the following additional properties. Note that all properties can also be
given as keyword arguments when calling the class constructor or instrument().
2.2 Module classes
7
baud_rate
The communication speed in baud. The default value is 9600.
data_bits
Number of data bits contained in each frame. Its value must be from 5 to 8. The default is 8.
stop_bits
Number of stop bits contained in each frame. Possible values are 1, 1.5, and 2. The default is 1.
parity
The parity used with every frame transmitted and received. Possible values are:
Value
no parity
odd parity
even parity
mark parity
space parity
Description
no parity bit is used
the parity bit causes odd parity
the parity bit causes even parity
the parity bit exists but it’s always 1
the parity bit exists but it’s always 0
The default value is no parity.
end_input
This determines the method used to terminate read operations. Possible values are:
Value
last bit end input
term chars end input
Description
read will terminate as soon as a character arrives with its last data bit set
read will terminate as soon as the last character of term chars is received
The default value is term chars end input.
3 Common properties of instrument variables
3.1 Timeouts
Very most VISA I/O operations may be performed with a timeout. If a timeout is set, every operation that takes longer
than the timeout is aborted and an exception is raised. Timeouts are given per instrument.
For all PyVISA objects, a timeout is set with
my_device.timeout = 25
Here, my device may be a device, an interface or whatever, and its timeout is set to 25 seconds. Floating-point values
are allowed. If you set it to zero, all operations must succeed instantaneously. You must not set it to None. Instead, if
you want to remove the timeout, just say
del my_device.timeout
Now every operation of the resource takes as long as it takes, even indefinitely if necessary.
The default timeout is 5 seconds, but you can change it when creating the device object:
my_instrument = instrument("ASRL1", timeout = 8)
This creates the object variable my instrument and sets its timeout to 8 seconds. In this context, a timeout value of
None is allowed, which removes the timeout for this device.
Note that your local VISA library may round up this value heavily. I experienced this effect with my National Instru-
ments VISA implementation, which rounds off to 0, 1, 3 and 10 seconds.
8
3 Common properties of instrument variables