PyVISA API Reference
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.
- Name
list_resources
- Type
- method
- Description
Returns a list of available VISA resources.
- Name
open_resource
- Type
- method
- Description
Opens a connection to a VISA instrument.
- Name
close
- Type
- method
- Description
Closes the Resource Manager session.
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.
- Name
query
- Type
- method
- Description
Sends a command and reads the response.
- Name
write
- Type
- method
- Description
Sends a command to the instrument.
- Name
read
- Type
- method
- Description
Reads data from the instrument.
- Name
clear
- Type
- method
- Description
Clears the instrument state.
- Name
timeout
- Type
- attribute
- Description
Timeout for read/write operations in milliseconds.
- Name
write_termination
- Type
- attribute
- Description
Character(s) appended to each write operation.
- Name
read_termination
- Type
- attribute
- Description
Character(s) expected at the end of a read operation.
- Name
baud_rate
- Type
- attribute
- Description
Baud rate for serial communication.
- Name
query_delay
- Type
- attribute
- Description
Delay between write and read in query operations (seconds).
- Name
parity
- Type
- attribute
- Description
Parity setting for serial communication.
- Name
stop_bits
- Type
- attribute
- Description
Stop bits for serial communication.
- Name
data_bits
- Type
- attribute
- Description
Data bits for serial communication.
- Name
chunk_size
- Type
- attribute
- Description
Buffer size for data transfers (bytes).
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.
- Name
VisaIOError
- Type
- exception
- Description
Raised for VISA I/O operation failures.
- Name
InvalidSession
- Type
- exception
- Description
Raised when accessing an invalid session.
try: instrument.write('*IDN?') response = instrument.read() except pyvisa.VisaIOError as e: print(f"Error: {e}")