PyVISA-sim
PyVISA-sim is a Python library that provides a simulated backend for PyVISA, allowing users to develop and test instrument control applications without needing physical hardware. It supports common resource types like TCPIP, GPIB, RS232, and USB, mimicking their behavior. The current version is 0.7.1, released on 2024-05-13, and it follows a regular, active release cadence.
Common errors
-
pyvisa.errors.VisaIOError: VI_ERROR_RSRC_NFOUND (resource not found)
cause The specified resource string (e.g., 'USB::0x1000::0x2000::0x3000::INSTR') does not exist in the currently loaded simulation. `pyvisa-sim` provides default resources, but custom ones need to be configured.fixCheck `rm.list_resources()` to see available resources. For custom resources, configure `pyvisa-sim` with a custom YAML file or by registering resources programmatically. -
ValueError: No backend available for the given session. Make sure you have the 'pyvisa-sim' package installed and are using the '@sim' backend alias.
cause `pyvisa-sim` is not installed, or the `@sim` backend string is misspelled/missing when initializing `ResourceManager`.fixInstall `pyvisa-sim` using `pip install pyvisa-sim`. Verify the backend string is exactly `'@sim'` when creating the resource manager. -
AttributeError: module 'pyvisa_sim' has no attribute 'SomeClass'
cause Attempting to import or use a class directly from the `pyvisa_sim` module that is not intended for direct user interaction or does not exist.fix`pyvisa-sim` acts as a backend for `pyvisa`. Most user interaction should be via `pyvisa.ResourceManager('@sim')` and standard PyVISA methods, not by importing symbols directly from `pyvisa_sim`.
Warnings
- gotcha `pyvisa-sim` requires `pyvisa` to be installed and used. It's a backend plugin, not a standalone library. Trying to use it without `pyvisa` will lead to errors.
- gotcha To engage the simulation, you MUST explicitly use the `@sim` backend string (e.g., `pyvisa.ResourceManager('@sim')`). If omitted or misspelled, PyVISA will attempt to connect to actual hardware or the default backend, leading to unexpected behavior or connection errors.
- gotcha `pyvisa-sim` provides *simulated* responses. While it aims to mimic real instruments, complex or highly specific instrument behaviors may not be perfectly replicated without further configuration (e.g., custom YAML files or programmatic resource registration).
Install
-
pip install pyvisa-sim
Imports
- ResourceManager
import pyvisa; rm = pyvisa.ResourceManager('@sim')
Quickstart
import pyvisa
# Create a resource manager that uses the simulated backend
# The '@sim' string tells PyVISA to load pyvisa-sim as the backend
rm = pyvisa.ResourceManager('@sim')
# List available simulated resources (pyvisa-sim comes with some defaults)
print("Available resources:", rm.list_resources())
# Open a simulated instrument resource
# This instrument is defined within pyvisa-sim's default simulation
try:
instrument = rm.open_resource('USB::0x1000::0x2000::0x3000::INSTR')
# Basic SCPI commands
print("Writing '*IDN?'...")
instrument.write('*IDN?')
idn = instrument.read()
print("Read IDN:", idn)
# Example of a command that changes state (simulation only)
instrument.write('MEASURE:VOLTAGE:DC?')
voltage = instrument.read()
print("Simulated DC Voltage:", voltage)
# Close the instrument
instrument.close()
print("Instrument closed.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Close the resource manager
rm.close()
print("Resource manager closed.")