PyVISA
PyVISA is a Python package that provides bindings to the "Virtual Instrument Software Architecture" (VISA) library. It enables control of various measurement devices and test equipment (e.g., oscilloscopes, multimeters, power supplies) through different interfaces like GPIB, RS232, USB, and Ethernet. It acts as a frontend to either a vendor-provided VISA shared library (like NI-VISA or Keysight IO Library Suite) or a pure Python implementation like PyVISA-py. The library is actively maintained, with the current version being 1.16.2, and receives regular updates.
Warnings
- breaking API changes in PyVISA 1.6+ from versions < 1.5. The direct use of `pyvisa.instrument()` and `pyvisa.get_instruments_list()` methods are removed. The `ask()` method on an instrument object was renamed to `query()`.
- gotcha PyVISA requires an underlying VISA implementation. It does not provide the VISA library itself. You must install a vendor's VISA library (e.g., NI-VISA, Keysight IO Library Suite) or the pure Python backend, PyVISA-py.
- gotcha Bitness mismatch between Python and the VISA library can cause 'No matching architecture' errors. For example, a 64-bit Python interpreter cannot load a 32-bit VISA library.
- gotcha The `ResourceManager` might fail to find the VISA library automatically, resulting in an `OSError: Could not find VISA library`.
- gotcha Serial (ASRL) instruments often require specific baud rates and termination characters to be set for correct communication, which are not always the default.
- deprecated The `Instrument.delay` attribute was removed in PyVISA 1.6. Also, the `term_chars` attribute for setting termination characters is deprecated.
Install
-
pip install -U pyvisa -
pip install -U pyvisa-py
Imports
- ResourceManager
from pyvisa import ResourceManager
Quickstart
import os
import pyvisa
# For simulation or when no physical VISA library is installed, use the PyVISA-py backend
# Otherwise, PyVISA will try to find a system-installed VISA library (e.g., NI-VISA)
# You can also explicitly specify a backend or path: rm = pyvisa.ResourceManager('@py')
# For a specific VISA library path: rm = pyvisa.ResourceManager('/path/to/visa.dll')
rm = pyvisa.ResourceManager()
try:
# List available resources (instruments)
resources = rm.list_resources()
print(f"Available resources: {resources}")
if not resources:
print("No instruments found. Ensure your instruments are connected and the VISA backend is correctly configured (e.g., NI-VISA or PyVISA-py).")
else:
# Open the first available instrument, or a specific one if known
# Replace 'YOUR_INSTRUMENT_ADDRESS' with an actual address from rm.list_resources()
# e.g., 'GPIB0::12::INSTR', 'ASRL1::INSTR', 'USB0::0xXXXX::0xXXXX::SN::INSTR', 'TCPIP0::192.168.1.100::INSTR'
# For this example, we'll try to connect to a dummy address if no real one is found
# In a real scenario, you would select an actual instrument from 'resources'
instrument_address = os.environ.get('PYVISA_INSTRUMENT_ADDRESS', resources[0] if resources else 'ASRL1::INSTR') # Fallback if no resources
if instrument_address.startswith('ASRL') and 'ASRL1::INSTR' in resources:
# Special handling for serial, often requires baud_rate
# This is a placeholder; actual baud_rate must match the instrument
inst = rm.open_resource(instrument_address, baud_rate=9600)
inst.write_termination = '\n'
inst.read_termination = '\n'
else:
inst = rm.open_resource(instrument_address)
# Query the instrument's identification string
idn = inst.query('*IDN?')
print(f"Instrument ID: {idn.strip()}")
# Example: Write a command and read a response
# inst.write('MEAS:VOLT:DC?') # Example SCPI command
# response = inst.read()
# print(f"Measurement: {response.strip()}")
inst.close()
print("Instrument connection closed.")
except pyvisa.VisaIOError as e:
print(f"VISA I/O Error: {e}")
print("Ensure the VISA backend is installed and configured, and the instrument address is correct.")
except Exception as e:
print(f"An unexpected error occurred: {e}")