PyVISA

1.16.2 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the ResourceManager, list available instruments, open a connection to an instrument, query its identification string, and then close the connection. It includes basic error handling and notes for serial configuration. Replace 'YOUR_INSTRUMENT_ADDRESS' with an actual resource string for your device. If no physical VISA backend is found, it automatically uses the PyVISA-py backend if installed.

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

view raw JSON →