PyVISA-py
PyVISA-py (version 0.8.1) is an active, pure Python implementation of a Virtual Instrument Software Architecture (VISA) library. It functions as a backend for the PyVISA package, enabling control of various measurement devices over interfaces such as Serial, USB, GPIB, and Ethernet, without requiring proprietary vendor-specific VISA installations. It is actively developed, with recent updates and releases.
Warnings
- gotcha Using PyVISA-py within a Virtual Machine (VM) or Docker container can lead to unexpected timeouts or connectivity issues when accessing hardware-dependent resources like USB or GPIB. VMs might not properly forward hardware responses, and Docker containers might disconnect idle connections.
- gotcha PyVISA-py's ability to communicate with different instrument types (Serial, USB, GPIB) depends on specific optional Python libraries (e.g., PySerial, PyUSB, linux-gpib, gpib-ctypes). If these are not installed, PyVISA-py may only be able to access TCPIP resources.
- gotcha For USB instruments, `PyUSB` (a dependency for PyVISA-py's USB support) requires an underlying USB driver library (e.g., `libusb`). On Unix-like systems, `udev` rules may need to be modified to grant non-root users access to USB devices. On Windows, you might need to uninstall USBTMC-specific drivers and install a generic driver.
- breaking While not directly in `pyvisa-py`, the underlying `PyVISA` library introduced significant breaking changes in versions 1.5 and 1.6. Key changes include the removal of the direct `pyvisa.instrument()` function in favor of `ResourceManager().open_resource()` and modifications to `ask()`/`query()` methods and event handler arguments.
- gotcha Incorrect `read_termination` or `write_termination` characters, or an incorrect baud rate, are common issues when communicating with serial instruments. The default baud rate is often 9600, but instrument manuals should be consulted.
Install
-
pip install pyvisa-py
Imports
- ResourceManager
import pyvisa rm = pyvisa.ResourceManager('@py')
Quickstart
import pyvisa
# Initialize the Resource Manager with the PyVISA-py backend
# This tells PyVISA to use the pure Python implementation.
rm = pyvisa.ResourceManager('@py')
# List available resources (instruments)
# The output will vary based on connected and supported hardware/emulators
resources = rm.list_resources()
print(f"Available resources: {resources}")
# Example of opening a (possibly simulated) instrument
# This line might fail if no such resource exists or is not properly configured
# For demonstration, we'll try to open a generic ASRL resource if available.
# Replace 'ASRL1::INSTR' with an actual resource from your 'resources' list.
if resources:
try:
# Choose the first resource or a known one, e.g., 'ASRL1::INSTR'
# Ensure the chosen resource type has its optional dependencies installed (e.g., PySerial for ASRL)
instrument_address = resources[0]
# Fallback to a common dummy address if real ones aren't found, for demonstration
if not instrument_address.startswith(('ASRL', 'USB', 'TCPIP', 'GPIB')):
instrument_address = 'ASRL1::INSTR' # This will likely fail without PySerial/a real ASRL port
inst = rm.open_resource(instrument_address)
print(f"Successfully opened: {instrument_address}")
# Example: Query instrument identification (standard SCPI command)
# This will likely only work if you have a real instrument or a simulator running
try:
idn = inst.query('*IDN?')
print(f"Instrument IDN: {idn.strip()}")
except pyvisa.errors.VisaIOError as e:
print(f"Could not query *IDN? on {instrument_address}: {e}")
finally:
inst.close()
print(f"Closed {instrument_address}")
except pyvisa.errors.VisaIOError as e:
print(f"Could not open resource {instrument_address}: {e}")
except IndexError:
print("No resources found to open.")
else:
print("No resources found by PyVISA-py backend.")