Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Instrument Control and DAQ

Talking to Instruments with VISA

Learn how LabVIEW controls instruments through VISA: resource names, the open, write, read, close pattern, timeouts, buses (GPIB, USB, serial, TCP/IP) and error handling.

Most lab and factory work eventually means talking to a box: a multimeter, a power supply, an oscilloscope, a motor drive. LabVIEW does this through VISA (Virtual Instrument Software Architecture), a single API that hides whether the instrument sits on GPIB, USB, RS-232 serial or Ethernet. You learn one set of VIs and use them for all of them.

LabVIEW itself is not available on this site, so the lessons in this chapter describe what you do on the block diagram precisely and use small, clearly labelled Python stand-ins that run here to teach the same logic. Nothing below pretends to talk to real hardware.

What VISA gives you

VISA is a standard maintained by the IVI Foundation. National Instruments (NI) ships an implementation called NI-VISA; other vendors ship their own, and LabVIEW can use any of them. Key ideas:

  • A resource is anything VISA can open: an instrument, a serial port, a socket.
  • A resource name is a text string that identifies it.
  • A session is a reference (a number) to an open resource. All VIs take the session as input and return it as output, which also forms a dataflow wire that orders the calls.
  • Every VISA VI has an error in / error out cluster, so the standard error wire runs through the whole conversation.

Resource names

The name encodes the bus, the address and the resource class:

Bus Example resource name
GPIB GPIB0::12::INSTR
USB USB0::0x0957::0x1796::MY12345678::INSTR
Serial ASRL3::INSTR (COM3)
TCP/IP (VXI-11) TCPIP0::192.168.1.50::inst0::INSTR
Raw socket TCPIP0::192.168.1.50::5025::SOCKET

In LabVIEW you rarely type these. A VISA Resource Name control (Controls palette, I/O, VISA) shows a drop-down with every instrument that NI Measurement and Automation Explorer (MAX) can see. You can also use Find Resource to list them in code.

Python Parse VISA resource names (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The open, configure, write, read, close pattern

Almost every instrument program has the same five steps, and on the block diagram they are a left-to-right chain of VIs from the Instrument I/O > VISA palette:

  1. VISA Open: takes the resource name and returns a session.
  2. VISA Configure Serial Port (serial only), or set the timeout with a property node (Timeout Value, in milliseconds).
  3. VISA Write: sends a string such as *IDN?.
  4. VISA Read: reads a string back. Its byte count input is the maximum number of bytes; the default of 20 truncates long replies, a classic beginner bug.
  5. VISA Close: releases the session. Forgetting this leaves the port busy until you close LabVIEW.

Always close sessions, even on error. Put VISA Close after the last VI and wire the error cluster through it: a VI with an error input still runs Close because Close is designed to run regardless of the incoming error.

flowchart LR
    A["VISA Open"] --> B["VISA Write<br/>*IDN?"] --> C["VISA Read"] --> D["VISA Close"]
    A -. "error out" .-> B
    B -. "error out" .-> C
    C -. "error out" .-> D

The session wire and the error wire both run left to right, so dataflow enforces the order.

A simulated conversation

This stand-in mimics the same sequence: an open call, a write, a read with a timeout, and a close. The FakeInstrument class answers a few commands; it is a toy, not a driver.

Python Open, write, read, close (simulated session)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Serial, GPIB and Ethernet differences

  • Serial needs matching baud rate, data bits, parity, stop bits and flow control on both ends. Many serial devices end replies with a termination character; VISA Configure Serial Port has Enable Termination Char for this.
  • GPIB uses numeric addresses (0 to 30) set on the instrument. A bus supports up to 15 devices. It is fast and reliable but needs a controller card or USB-GPIB adapter.
  • USB-TMC and LAN (VXI-11 / HiSLIP) instruments usually need no configuration beyond power and a cable.
  • Raw sockets (port 5025 is a common SCPI port) give you a plain byte stream with no instrument layer; you handle termination yourself.

Handling errors and timeouts

VISA errors are numeric codes in the error cluster. -1073807339 (hex BFFF0015) means timeout; -1073807343 means the resource was not found. A robust program checks the status boolean, shows source and description, and either retries or goes to a safe state. See error handling later in this track.

Set a sensible timeout: long enough for the slowest legal operation (a slow multimeter measurement may take seconds) yet short enough that a disconnected cable does not freeze your program for the default 10 seconds repeatedly.

Instrument drivers

You do not have to write the strings yourself for popular instruments. The Instrument Driver Finder (Help menu or the Tools > Instrumentation menu) searches ni.com for ready-made drivers. A driver is a library of VIs with names like Configure Voltage, Read, Close, and it follows a standard layout (Initialize, Configure, Action/Status, Data, Utility, Close). When no driver exists, build your own with VISA calls and the SCPI command language.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a VISA session represent?

    VISA Open returns a session reference; every later VISA VI takes it as input, which also orders the calls by dataflow.
  2. 2. Which resource name addresses a GPIB instrument at address 12?

    GPIB names have the form GPIBboard::address::INSTR.
  3. 3. A reply is cut off after 20 characters. What is the most likely cause?

    VISA Read returns at most 'byte count' bytes, and its default is 20.
  4. 4. Why wire the error cluster through VISA Close?

    Close always executes and releases the resource, and the error wire also makes it run after the earlier calls.

Summary

  • VISA is one API for GPIB, USB, serial and Ethernet instruments.
  • A resource name identifies the device; VISA Open returns a session.
  • The standard flow is Open, configure, Write, Read, Close, with the error cluster wired through.
  • Set the byte count and timeout deliberately.
  • Use a vendor instrument driver when one exists.

Next: The SCPI Command Language