Instrument Automation With Python by Keysight 5992-4268
Instrument Automation With Python by Keysight 5992-4268
SCPI
Every instrument that is remotely controllable has a set of documented SCPI commands
that allow the user to control the instrument using a programmatic interface rather
than using front panel controls or a graphical user interface. In some cases, the SCPI
command set for instruments is included in its user manual, but often the manufacturer
provides a standalone programmer manual that documents all the available commands.
Some standard commands are available on every SCPI-enabled instrument, including
*RST (reset/default setup), *ESR? (check error status register), *OPC? (operation complete
query), *CLS (clear status register), and *IDN? (identification query), but most commands
are instrument-specific.
VISA
Instrument communication is generally facilitated by the VISA standard. VISA stands
for Virtual Instrument Software Architecture, and it is a standardized mechanism for
communication between test equipment and a controlling computer. The VISA standard
is maintained by the IVI Foundation and is implemented by major test and measurement
companies. Keysight’s implementation is called Keysight IO Libraries.
In most cases instruments are controlled through an Ethernet connection. The flexibility
and widespread popularity of this interface allows for fast communication and abstraction
of location, meaning that while both the controlling computer and test equipment are
connected to the same network, they can be located an arbitrary distance from each
other while maintaining communication. The throughput and latency provided by Ethernet
communication is also fast enough for the overwhelming majority of use cases.
VXI-11 and HiSLIP are additional layers on top of the TCP/IP protocol that allow a user
to communicate with an instrument over a LAN connection. They are supported by most
VISA software packages and provide features like GPIB emulation and instrument locking
in addition to basic communication and control. HiSLIP is significantly faster than the
much older VXI-11 protocol. Raw Socket communication is a little different. It provides a
lightweight LAN interface for communicating with an instrument, but little else. Notably,
it is the fastest interface because it has almost no overhead. Most modern test and
measurement equipment allows the user to choose between the three interfaces.
Regardless of the interface used, a few basic functions are required to successfully control
an instrument: Write, read/query, binary block read, and binary block write functions.
When writing test automation scripts, the programmer manual for the test equipment
in question should always be available for reference. Keysight also has a free software
called Command Expert that acts as an interactive programmer manual, complete with a
graphical tree diagram for the command set, a handy search feature, intelligent argument
insertion, real-time command testing, and script exporting.
Code example
Now that the basics of instrument communications have been discussed, it’s time to explore
a Python script that communicates with a real instrument using PyVISA. The script will set
up an oscilloscope, extract waveform data, and plot it. Although this short walkthrough will
touch on programming techniques, is not intended to teach programming and assumes a
basic knowledge of Python syntax.
Generally, the first few lines of code in a Python script import any Python packages required
by the script. A file containing Python code has a .py extension and is also known as a
Python module. A Python package is a hierarchical structure of multiple Python modules,
and Python packages can be added to a Python installation from the Python Package Index.
An import statement allows a Python script to use any of the code in the package or module
being imported.
A connection to the oscilloscope must be made using the PyVISA package. This
script uses two pieces of code from PyVISA: the ResourceManager class and its
open_resource method. The ResourceManager can list all the available VISA resources
and create a connection to those resources. The script creates an instance of the
ResourceManager class and assigns it to the variable rm. ResourceManager has
a method called open_resource, which takes an instrument’s VISA identifier as the
argument and returns a VISA instrument object, which can be used to communicate
with the instrument. In this example, the computer and the oscilloscope are connected
through a local area network and the scope’s IP address is known. VISA identifiers
are in the form <connection type>::<address>::<interface>::INSTR. So for a TCP/IP
instrument with a 192.168.1.3 IP address using the HiSLIP interface, it’s VISA identifier
would be TCPIP::192.168.1.3::hislip0::INSTR. This VISA identifier can also be copied
directly from the VISA software running on the PC. The VISA object is assigned to a
variable called scope.
After these variables are defined, the script can begin communicating with the scope.
A VISA instrument object has three basic communication methods: write(), read(), and
query(). write() takes a string as an argument and sends that string to the instrument,
read() gets whatever information is placed in the instrument’s output buffer, and query()
concatenates a write() and read() back-to-back.
Once the scope has been brought to a default state, vertical, horizontal, and trigger
settings can be configured to match the waveform in question. Vertical range, time range,
and trigger level were defined earlier in the program, so those values will be inserted into
their respective SCPI command stings. The values in this example were chosen for the
specific conditions of this exercise.
There are many ways to format strings in Python, but this example uses F-strings. F-strings
replace any expression placed inside curly braces with a value at runtime.
A quick note on SCPI commands. SCPI commands are not case-sensitive, but each
command has an abbreviated version that is denoted by uppercase letters. Take the
“AUToscale” command, for example. Writing either ‘aut’ or ‘autoscale’ to the scope will
have the same effect. The scope’s SCPI parser recognizes either text string as a valid
command to automatically scale the horizontal and vertical ranges of the scope to match
the waveform on screen.
Many SCPI commands will have arguments. As defined by the SCPI standard, arguments
are either numbers or strings separated from the main command by a space. Some string
arguments require double quotes around them (e.g., when specifying a save directory),
and this requirement will be specified in the command’s documentation.
It is useful to explicitly select the scope channel being used as the data source. In the
case of this example, channel 1 is automatically selected by the default setup, but there
are cases where other channels are of interest.
Defining the waveform format on the instrument side is an important but often overlooked
step, as it determines how the script must interpret the waveform data. In this case, the
“byte” argument in the “WAVeform:FORMat <format>” specifies that any waveform data sent
from the scope is formatted as signed 8-bit integers. There are other formats, but “byte”
results in the fastest waveform transfer rates and has sufficient resolution for this example.
The IEEE 488.2 standard defines a format for sending and receiving fixed-length data
blocks to and from test and measurement equipment. This format includes a header
that specifies how many bytes or characters are contained in the block of data, and this
header must be parsed to send and receive this data correctly. PyVISA’s query_binary_
values() method parses the header and handles the transfer of data automatically. In this
case it takes two arguments: the SCPI query that instructs the instrument to generate
data and a datatype argument, which determines the data type used to interpret the data
in the block. It is critical in this case to ensure that the instrument and the script are using
the same data type. It is easy to accidentally swap signed and unsigned data types, and
this confusion will lead to incorrect results.
Waveform Waveform
0.8 2.0
0.6
0.4 1.5
Voltage (V)
Voltage (V)
0.2
0.0 1.0
−0.2
−0.4 0.5
−0.6
−0.8 0.0
−2 −1 0 1 2 −2 −1 0 1 2
Time (sec) 1e-7 Time (sec) 1e-7
Figure 6: Script and instrument data type match Figure 7: Script and instrument data type mismatch
Now that the data has been transferred to the computer, it is useful to visualize it. Generally,
the data transferred from the instrument is in an unscaled binary format that must be
multiplied by a scaling factor for visualization and calculation purposes. Fortunately, most
instruments’ SCPI command sets include queries to get the required scaling factors.
Because VISA objects return data from normal queries as strings, they must be converted
to numerical data types prior to being used for numerical processing. Python makes type
conversions ridiculously simple. To convert a compatible string or integer value to a floating
point data type, simply call the float() function on the data to be converted. In this case,
float() is called on the SCPI queries to convert the returned scaling factors to floats.
X increment, Y increment, X origin, and Y origin are needed to scale the data correctly, and
there are individual commands for each. The length of the raw data returned from the scope
is calculated in this block as well.
The X values can be used to create a time vector for plotting the waveform data, and the
Y values can be used to convert the raw binary data from the “WAVeform:DATA?” query
into voltages. In addition to standard for() loops, Python also has a concise list generation
syntax called list comprehension that is used to scale the data here.
Waveform
0.8
0.6
0.4
Voltage (V)
0.2
0.0
−0.2
−0.4
−0.6
−0.8
−2 −1 0 1 2
Time (sec) 1e-7
This example only scratches the surface of Python’s capabilities for test automation. In
addition to remotely controlling test equipment, Python can further enhance workflows
with digital signal processing, complex data visualization, or integration with machine
learning and data analytics.
Resources
1. 8 World-Class Software Companies That Use Python: https://realpython.com/
world-class-companies-using-python/
5. IVI Foundation Specifications (VISA is near the bottom of the page): http://www.
ivifoundation.org/specifications/default.aspx
7. Getting Data From the Analyzer (binary block data format): http://na.support.
keysight.com/vna/help/latest/Programming/Learning_about_GPIB/Getting_
Data_from_the_Analyzer.htm#block