Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 36 ยท Projects and Practice

Project: A SCPI Instrument Simulator

Build a software simulation of a SCPI power supply to develop and test LabVIEW instrument code without hardware: command parser, state, error queue, and a driver with a simulate switch.

You often write instrument code before the instrument arrives, or you want automated tests that do not depend on a bench. The answer is an instrument simulator: a program that answers the same text commands as the real device. In this project you build the heart of one, a parser and state machine for a small SCPI power supply, and then wire a LabVIEW driver that can use it through a Simulate switch. LabVIEW is not available on this site, so the simulator runs as Python; the LabVIEW side is described in steps.

This project reuses ideas from VISA, SCPI and unit testing.

Requirements

The simulated supply has one channel with these commands:

Command Effect
*IDN? Reply SIM,PSU-1,0001,1.0
*RST Voltage 0, current limit 1 A, output off, errors cleared
:SOUR:VOLT <v> / :SOUR:VOLT? Set or read the voltage (0 to 30 V)
:SOUR:CURR:LIM <a> / ? Set or read the current limit (0 to 3 A)
:OUTP ON|OFF / :OUTP? Enable the output; query returns 1 or 0
:MEAS:VOLT?, :MEAS:CURR? Measured values, from a resistive load model
:SYST:ERR? Pop the oldest error: code,"message"

Errors: -113 undefined header, -224 illegal parameter value.

Design

  1. Normalise the input: strip, uppercase, split on ; for multiple commands.
  2. Match against a table that maps long and short keywords (VOLTage and VOLT). A common trick is to store the long form and accept any prefix of at least four characters; this model simply lists both forms.
  3. Execute with a function that changes the state or returns a reply string.
  4. Push errors on a queue; :SYST:ERR? pops it.
  5. Only commands that are queries (end with ?) produce a reply; the caller reads exactly once per query.

The simulator

Python A SCPI power supply simulator

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 last line shows a multi-command string: two queries produce two replies, which the real LabVIEW code would read back with two reads (or one read if the instrument joins replies with a semicolon; the model returns a list for clarity).

The LabVIEW driver design

Write the driver as a set of subVIs, Initialize, Set Voltage, Read Voltage, Read Current, Output Enable, Check Error and Close. Each takes a session and the error cluster, builds the string and calls a lower layer, Query/Send. The lower layer is where you switch:

  • Real: VISA Write and VISA Read on the session.
  • Simulated: a subVI that implements the same table as the Python class above using a Case structure on the command string, or a LabVIEW class PSU_Sim that overrides Send and Query.

A Boolean Simulate on Initialize selects the object. All upper-level code (state machine, tests, UI) is identical. This is the same layering you use to test code without hardware.

Tests to write

Python Tests for the simulator

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.

Extensions

  • Add a second channel and a channel selector (:INST:NSEL 2).
  • Add latency: delay replies so timeout handling can be tested.
  • Add fault injection: a command that makes the next read time out or return garbage, to test error paths.
  • Serve the simulator over a TCP socket (port 5025) so the real VISA code, TCPIP0::localhost::5025::SOCKET, talks to it with no code change.
  • Record real instrument traffic and replay it as test data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why build an instrument simulator?

    A simulator lets you develop early and run automated tests anywhere.
  2. 2. How should the simulator treat an unknown command?

    Real instruments queue errors so :SYST:ERR? can report them.
  3. 3. How does the LabVIEW driver choose between real and simulated?

    A thin layer or class that is swapped keeps all upper-level code unchanged.
  4. 4. Which commands get a reply in the simulator?

    Set commands produce no reply, as with real SCPI instruments.

Summary

  • A simulator is a parser plus state plus an error queue that answers like the real instrument.
  • Layer the LabVIEW driver so a Simulate switch swaps the transport.
  • Test normal values, out-of-range values and unknown commands, and the empty error queue.
  • Extend with fault injection and a TCP front end.

Next: Practice Exercises and Review