If you have been using PyVISA before version 1.5, you might want to read Migrating from PyVISA < 1.5.
Let’s go in medias res and have a look at a simple example:
>>> import visa >>> rm = visa.ResourceManager() >>> rm.list_resources() ('ASRL1::INSTR', 'ASRL2::INSTR', 'GPIB0::14::INSTR') >>> my_instrument = rm.open_resource('GPIB0::14::INSTR') >>> print(my_instrument.query('*IDN?'))
This example already shows the two main design goals of PyVISA: preferring simplicity over generality, and doing it the object-oriented way.
visa, we create a
ResourceManager object. If called without
arguments, PyVISA will use the default backend (NI) which tries to find the
VISA shared library for you. You can check, the location of the shared library
used simply by:
>>> print(rm) <ResourceManager('/path/to/visa.so')>
In some cases, PyVISA is not able to find the library for you
resulting in an
OSError. To fix it, find the library path
yourself and pass it to the ResourceManager constructor.
You can also specify it in a configuration file as discussed
in Configuring the NI backend.
Once that you have a
ResourceManager, you can list the available resources
list_resources method. The output is a tuple listing the
VISA resource names.
In this case, there is a GPIB instrument with instrument number 14, so you ask
ResourceManager to open “‘GPIB0::14::INSTR’” and assign the returned
object to the my_instrument.
open_resource has given you an instance of
(a subclass of the more generic
>>> print(my_instrument) <GPIBInstrument('GPIB::14')>
Resource subclasses representing the different types of resources, but
you do not have to worry as the
ResourceManager will provide you with the appropiate
class. You can check the methods and attributes of each class in the Resource classes
Then, you query the device with the following message:
Which is the standard GPIB message for “what are you?” or – in some cases –
“what’s on your display at the moment?”.
query is a short form for a
operation to send a message, followed by a
is the same as:
>>> my_instrument.write('*IDN?') >>> print(my_instrument.read())
Example for serial (RS232) device¶
Consider an Oxford ITC4 temperature controller, which is connected through COM2 with my computer. The following code prints its self-identification on the screen:
itc4 = rm.open_resource("COM2") itc4.write("V") print(itc4.read())
Instead of separate write and read operations, you can do both with
query() call. Thus, the above source code is equivalent to:
It couldn’t be simpler.