API Reference
Complete PyVISA API reference - resource manager, instrument queries, binary transfers, event handling, and VISA resource strings.
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
| Method | Description | Returns |
|---|---|---|
list_resources(query?) | Returns all available VISA resources | tuple[str] |
open_resource(resource, **kwargs) | Opens connection to an instrument | Instrument |
close() | Closes the Resource Manager session | None |
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
| Method | Description | Returns |
|---|---|---|
query(command) | Sends command and reads response | str |
write(command) | Sends command to instrument | int (bytes written) |
read() | Reads data from instrument | str |
clear() | Clears instrument state and buffers | None |
Attributes
| Attribute | Description | Type | Default |
|---|---|---|---|
timeout | Timeout for operations (milliseconds) | int | 2000 |
write_termination | Characters appended to writes | str | None |
read_termination | Expected termination characters | str | None |
baud_rate | Serial baud rate | int | 9600 |
query_delay | Delay between write/read (seconds) | float | 0.0 |
parity | Serial parity setting | Parity | Parity.none |
stop_bits | Serial stop bits | StopBits | StopBits.one |
data_bits | Serial data bits | int | 8 |
chunk_size | Buffer size (bytes) | int | 20480 |
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
| Exception | Description | When Raised |
|---|---|---|
VisaIOError | VISA I/O operation failure | Communication errors |
InvalidSession | Invalid or closed session | Using closed instruments |
InvalidResource | Invalid resource string | Malformed VISA addresses |
TimeoutError | Operation timeout | Exceeding timeout period |
Example:
try:
instrument.write('*IDN?')
response = instrument.read()
except pyvisa.VisaIOError as e:
print(f"Error: {e}")Rohde & Schwarz Instruments
How to control R&S spectrum analyzers, signal generators, and oscilloscopes with PyVISA. Connection snippets, examples, and vendor quirks.
SCPI Commands
SCPI command reference for PyVISA. Includes the full command tree, common error codes, and Python examples for test instrument control and automation.