PyVISA
PyVISADocs

API Reference

Explore the PyVISA API to manage and interact with instruments via VISA standards.

Control measurement instruments using the VISA standard with the PyVISA library.

import pyvisa

pyvisa.log_to_screen()
rm = pyvisa.ResourceManager()

instruments = rm.list_resources()

# Using "with" closes the instrument properly even if an exception occurs
with rm.open_resource(instruments[-1]) as inst:
    # Common for serial instruments
    inst.write_termination, inst.read_termination = '\n', '\r\n'

    # Identify the instrument (manufacturer, model, serial number, firmware version)
    idn_response = inst.query('*IDN?')
    print(f"Instrument ID: {idn_response}")
    
    # Set voltage to 12V
    inst.write("VOLT 12")
    print(f"Voltage Set: {inst.query('VOLT?')}V")

Resource Manager

The ResourceManager handles VISA resources and manages sessions.

Methods

MethodDescriptionReturns
list_resources(query?)Returns all available VISA resourcestuple[str]
open_resource(resource, **kwargs)Opens connection to an instrumentInstrument
close()Closes the Resource Manager sessionNone

Example:

import pyvisa
rm = pyvisa.ResourceManager()
resources = rm.list_resources()
instrument = rm.open_resource(resources[0])
instrument.close()

Instrument

The Instrument class sends and receives commands from measurement devices.

Methods

MethodDescriptionReturns
query(command)Sends command and reads responsestr
write(command)Sends command to instrumentint (bytes written)
read()Reads data from instrumentstr
clear()Clears instrument state and buffersNone

Attributes

AttributeDescriptionTypeDefault
timeoutTimeout for operations (milliseconds)int2000
write_terminationCharacters appended to writesstrNone
read_terminationExpected termination charactersstrNone
baud_rateSerial baud rateint9600
query_delayDelay between write/read (seconds)float0.0
paritySerial parity settingParityParity.none
stop_bitsSerial stop bitsStopBitsStopBits.one
data_bitsSerial data bitsint8
chunk_sizeBuffer size (bytes)int20480

Example:

inst.timeout = 2000  # Timeout in milliseconds
inst.write_termination = '\n'
inst.read_termination = '\r\n'
inst.chunk_size = 20480  # Default chunk size

instrument.query('*IDN?')
instrument.write('VOLT 12')
response = instrument.read()
print(response)

Exception Handling

PyVISA raises exceptions for error handling during instrument communication.

Exceptions

ExceptionDescriptionWhen Raised
VisaIOErrorVISA I/O operation failureCommunication errors
InvalidSessionInvalid or closed sessionUsing closed instruments
InvalidResourceInvalid resource stringMalformed VISA addresses
TimeoutErrorOperation timeoutExceeding timeout period

Example:

try:
    instrument.write('*IDN?')
    response = instrument.read()
except pyvisa.VisaIOError as e:
    print(f"Error: {e}")

How is this guide?