Pure Python implementation of a VISA library for instrument communication.
npx @tessl/cli install tessl/pypi-pyvisa-py@0.8.0A pure Python implementation of the Virtual Instrument Software Architecture (VISA) library that provides comprehensive backend functionality for PyVISA without requiring proprietary VISA drivers. PyVISA-py enables developers to communicate with test and measurement instruments through multiple interfaces including Serial, USB, GPIB, and Ethernet connections using only Python and cross-platform libraries.
pip install pyvisa-pyimport pyvisa_pyStandard usage through PyVISA:
import pyvisa
# Use PyVISA-py as the backend
rm = pyvisa.ResourceManager('@py')Direct backend access:
from pyvisa_py import PyVisaLibrary
# Create the backend directly
backend = PyVisaLibrary()import pyvisa
# Initialize PyVISA with PyVISA-py backend
rm = pyvisa.ResourceManager('@py')
# List available resources
resources = rm.list_resources()
print("Available resources:", resources)
# Open connection to an instrument
# Example resource strings:
# - Serial: "ASRL/dev/ttyUSB0::INSTR"
# - USB: "USB0::0x1234::0x5678::12345::INSTR"
# - TCP/IP: "TCPIP::192.168.1.100::INSTR"
# - GPIB: "GPIB0::10::INSTR"
try:
# Open instrument connection
inst = rm.open_resource("ASRL/dev/ttyUSB0::INSTR")
# Configure communication parameters
inst.timeout = 2000 # 2 second timeout
inst.read_termination = '\\n'
inst.write_termination = '\\n'
# Send commands and read responses
response = inst.query("*IDN?")
print("Instrument ID:", response)
# Write commands
inst.write("*RST") # Reset instrument
# Read data
data = inst.read()
print("Data:", data)
finally:
# Always close connections
inst.close()
rm.close()PyVISA-py implements a session-based architecture that dispatches communication operations to specialized session classes:
This design enables PyVISA-py to serve as a drop-in replacement for proprietary VISA implementations while providing the same high-level abstraction and supporting cross-platform deployment.
The main PyVisaLibrary class that provides the VISA backend implementation with session management, resource operations, and protocol dispatching.
class PyVisaLibrary:
def open(self, session, resource_name, access_mode=0, open_timeout=0): ...
def close(self, session): ...
def read(self, session, count): ...
def write(self, session, data): ...
def list_resources(self, session, query="?*::INSTR"): ...
def get_attribute(self, session, attribute): ...
def set_attribute(self, session, attribute, attribute_state): ...Communication with instruments via serial ports, USB-to-serial adapters, and virtual COM ports with configurable parameters and termination handling.
class SerialSession:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...
@staticmethod
def list_resources(): ...Direct USB device communication supporting both USBTMC (USB Test & Measurement Class) protocol for instruments and raw USB device access.
class USBInstrSession:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...
def clear(self): ...
@staticmethod
def list_resources(): ...
class USBRawSession:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...Network-based instrument communication supporting multiple protocols including VXI-11, HiSLIP, VICP, and raw TCP sockets.
class TCPIPInstrSession:
def __new__(cls, resource_manager_session, resource_name, parsed, open_timeout): ...
@staticmethod
def list_resources(): ...
class TCPIPInstrVxi11:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...
class TCPIPInstrHiSLIP:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...
class TCPIPSocketSession:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...General Purpose Interface Bus communication for controlling GPIB instruments with support for bus management, addressing, and control operations.
class GPIBSessionDispatch:
def __new__(cls, resource_manager_session, resource_name, parsed, open_timeout): ...
class GPIBSession:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...
def gpib_command(self, command_byte): ...
@staticmethod
def list_resources(): ...Support for Prologix GPIB-USB and GPIB-Ethernet adapters that provide GPIB functionality through USB and TCP/IP interfaces.
class PrologixTCPIPIntfcSession:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...
class PrologixASRLIntfcSession:
def __init__(self, resource_manager_session, resource_name, parsed, open_timeout): ...
def read(self, count): ...
def write(self, data): ...PyVISA-py supports standard VISA resource string formats:
ASRL<port>::INSTR (e.g., ASRL/dev/ttyUSB0::INSTR, ASRLCOM1::INSTR)USB<board>::<vendor_id>::<product_id>::<serial>::<interface>::INSTRUSB<board>::<vendor_id>::<product_id>::<serial>::<interface>::RAWTCPIP::<hostname>::<port>::SOCKETTCPIP::<hostname>::INSTRTCPIP::<hostname>::hislip<session>::INSTRVICP::<hostname>::INSTRGPIB<board>::<primary_address>[::secondary_address]::INSTRPRLGX-TCPIP::<hostname>::<port>::INTFCPRLGX-ASRL::<port>::INTFCPyVISA-py functionality can be extended with optional packages:
Install with specific features:
pip install pyvisa-py[serial,usb,gpib-ctypes]PyVISA-py provides comprehensive error handling with VISA-compliant status codes:
class OpenError(Exception):
"""Exception raised when failing to open a resource."""
def __init__(self, error_code=StatusCode.error_resource_not_found): ...
class UnknownAttribute(Exception):
"""Exception raised for unsupported VISA attributes."""
def __init__(self, attribute): ...Common error scenarios: