Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Instrument Control and DAQ

The SCPI Command Language

Learn SCPI, the text command language of most bench instruments: command syntax, queries, common commands, status bytes, and how to build and parse commands in LabVIEW.

Once you can open a session with VISA, you need to know what to write. Most modern bench instruments (multimeters, power supplies, oscilloscopes, function generators) accept SCPI, the Standard Commands for Programmable Instruments. SCPI is plain text, so in LabVIEW you build commands with string functions and read replies with string and number conversion functions.

As in the rest of this chapter, LabVIEW cannot run here. The Python blocks are labelled stand-ins that model the string handling you would do with LabVIEW's Format Into String, Scan From String and Spreadsheet String To Array.

Anatomy of a command

SCPI commands form a tree. Colons separate levels; a question mark makes it a query.

TEXT
*IDN?                      identify (IEEE 488.2 common command)
*RST                       reset to defaults
:MEASure:VOLTage:DC?       query a DC voltage
:SOURce:VOLTage 5.0        set output voltage to 5 V
:OUTPut ON                 enable the output
  • Common commands begin with * and are the same on every compliant instrument: *IDN?, *RST, *CLS, *OPC?, *ESR?, *STB?.
  • Subsystem commands are device specific but follow naming rules. Keywords have a long and short form written in mixed case: MEASure can be sent as MEAS or MEASURE, and case does not matter.
  • A space separates the command from its parameter; commas separate multiple parameters.
  • Commands end with a termination character, usually a line feed (\n). In LabVIEW, \n in a string constant with "\ Codes Display" turned on is that character.
  • Several commands can be chained with a semicolon.

Building commands in LabVIEW

On the block diagram you use Format Into String (Programming > String). Its format string :SOUR:VOLT %.3f\n takes a numeric wire and produces :SOUR:VOLT 5.000 followed by a line feed. Wire the result to VISA Write.

For replies, Scan From String with %f converts +4.99812E+00 to a double. If the reply is a comma-separated list, use Spreadsheet String To Array with a comma delimiter, or Match Pattern and a loop.

Python Format and scan SCPI strings (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.

Queries and the output buffer

An instrument has an output buffer. A query puts its reply there; VISA Read takes it out. Rules that cause many bugs:

  • Every query needs exactly one read. An unread reply stays in the buffer and the next read returns stale data ("off by one" replies).
  • A set command (no ?) produces no reply, so do not read after it.
  • Some replies are binary blocks: # then a digit giving the length of the length field, then the length, then the bytes. Oscilloscope waveforms often use this.
Python Decode an IEEE 488.2 binary block header

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.

In LabVIEW, read the block with VISA Read (set the byte count large enough), then use String To Byte Array or Unflatten From String with the right data type and byte order.

Synchronising: *OPC? and polling

Commands are executed in order, but some take time (a sweep, an auto-range). To wait for completion send *OPC?; the instrument replies 1 when all previous operations have finished. Alternatively poll the Status Byte with VISA Read STB, or use a service request (SRQ) event so the VI does not block.

The status system

Every SCPI instrument keeps status registers. The ones you use daily:

Register Query Meaning
Status Byte *STB? Summary bits: error queue not empty, message available, event status
Standard Event Status *ESR? Command error, execution error, operation complete
Error queue :SYSTem:ERRor? Returns code,"message"; 0,"No error" when empty

A good habit: after configuring an instrument, read :SYST:ERR? until it returns zero, and show any messages. It turns silent typos into readable errors.

Writing a reusable wrapper

Do not scatter VISA Write and Read all over the diagram. Create small subVIs such as Query String, Query Number and Send Command that take a session and error cluster, handle the termination character, check the error queue and return a typed result. When the instrument changes you edit one place. The queued message handler pattern later in this track is a good home for such wrappers.

If an instrument does not answer, test the exact string in NI MAX's Interactive Control or in the instrument's front-panel console first. If it works there, the problem is your termination character, byte count or timeout.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a question mark at the end of an SCPI command mean?

    Queries end in ? and require a read to get the reply.
  2. 2. Which command asks an instrument to identify itself?

    *IDN? returns manufacturer, model, serial number and firmware.
  3. 3. You send a set command like :OUTP ON and then call VISA Read. What happens?

    Set commands produce no reply, so reading blocks until the timeout.
  4. 4. How do you wait for all previous commands to finish?

    *OPC? returns 1 once the earlier operations are complete.

Summary

  • SCPI is a text tree of commands; ? makes a query and * marks common commands.
  • Build commands with Format Into String, parse replies with Scan From String or array conversion.
  • One read per query, none after set commands.
  • Use *OPC?, the status byte and the error queue to synchronise and diagnose.
  • Wrap instrument traffic in small subVIs.

Next: DAQ Tasks and Channels