Quick Start Guide to PyVISA: Controlling Instruments with Python

Quick Start Guide to PyVISA: Controlling Instruments with Python

PyVISA lets you control measurement instruments (oscilloscopes, multimeters, power supplies, signal generators) from Python using the VISA standard. This quick-start guide shows how to install PyVISA, connect to instruments, send/receive commands, and build simple measurement scripts.

What you need

  • A computer with Python 3.8+ installed.
  • The instrument with a supported interface: USBTMC (USB), GPIB, RS-232 (serial), or TCP/IP (VXI-11 / HiSLIP).
  • A VISA backend: either NI-VISA (recommended for GPIB and many setups) or the open-source pyvisa-py for USBTMC, TCPIP, and serial.

Install PyVISA and a backend

  1. Install PyVISA:

bash

pip install pyvisa
  1. Install a backend:
  • NI-VISA (recommended): download and install from National Instruments (requires admin privileges).
  • Or install pyvisa-py:

bash

pip install pyvisa-py

Verify VISA resources

Open a Python REPL or script and run:

python

import pyvisa rm = pyvisa.ResourceManager() print(rm.listresources())

This prints connected instrument resource strings like ‘USB0::0x0957::0x1796::MY123456::INSTR’ or ‘ASRL1::INSTR’ for serial.

Basic workflow: open, configure, query, close

  1. Open the instrument:

python

inst = rm.openresource(‘USB0::0x0957::0x1796::MY123456::INSTR’)
  1. Set common parameters (timeouts, encoding):

python

inst.timeout = 5000# milliseconds inst.encoding = ‘utf-8’ inst.read_termination = ’ ‘ inst.writetermination = ’ ‘
  1. Identify the instrument (standard SCPI):

python

print(inst.query(’*IDN?’))
  1. Send a command (no response):

python

inst.write(‘SYST:REM’) # example: set remote mode
  1. Read a value or query:

python

value = inst.query(‘MEAS:VOLT?’) print(‘Measured voltage:’, value)
  1. Close the session:

python

inst.close() rm.close()

Example: measure voltage from a multimeter

python

import pyvisa rm = pyvisa.ResourceManager() inst = rm.open_resource(‘USB0::0x0957::0x1796::MY123456::INSTR’) inst.timeout = 5000 inst.write_termination = ’ ‘ inst.readtermination = ’ ‘ print(‘ID:’, inst.query(’*IDN?’)) inst.write(‘CONF:VOLT:DC’) # configure DC voltage measurement reading = inst.query(‘READ?’) # perform measurement print(‘Voltage (V):’, float(reading)) inst.close() rm.close()

Serial (RS-232) example

For instruments exposing a serial port (ASRL):

python

inst = rm.open_resource(‘ASRL1::INSTR’) # or ‘ASRL/dev/ttyUSB0::INSTR’ on Linux inst.baud_rate = 9600 inst.data_bits = 8 inst.stopbits = pyvisa.constants.StopBits.one inst.parity = pyvisa.constants.Parity.none print(inst.query(’*IDN?’))

TCP/IP (VXI-11 or HiSLIP) example

python

inst = rm.open_resource(‘TCPIP0::192.168.1.50::inst0::INSTR’) # VXI-11 # or inst = rm.open_resource(‘TCPIP0::192.168.1.50::hislip0::INSTR’) # HiSLIP print(inst.query(’*IDN?’))

Error handling and timeouts

  • Wrap critical calls in try/except to catch pyvisa.errors.VisaIOError.
  • Increase inst.timeout for slow instruments.
  • Use inst.clear() to flush instrument buffers after errors.

Best practices

  • Use .query() for single-request/response commands and .write()/.read() when you need separate control.
  • Close instruments and the ResourceManager to free resources.
  • Use unique resource strings from list_resources()—don’t hardcode when possible.
  • For automated test rigs, add logging, retries, and explicit error handling.
  • Prefer NI-VISA for GPIB; pyvisa-py is useful for USBTMC and when NI-VISA isn’t available.

Troubleshooting quick checklist

  • Instrument powered on and in remote mode.
  • Correct interface drivers installed (USBTMC driver, NI-VISA, GPIB adapter drivers).
  • Correct resource string from rm.list_resources().
  • Matching terminations and encoding.
  • Firewall/network settings for TCP/IP instruments.

Further reading

This guide gives the essentials to get started controlling instruments with PyVISA. Use the examples as templates and adapt commands to your instrument’s SCPI set.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *