Test& Measurement World, October 1998, pp. 39-45
Program Your Scope from a PC
Proper programming lets you correctly initialize a DSO, then capture waveforms and measure signal parameters.

by Helen Muterspaugh, Hewlett-Packard, Colorado Springs, CO

You probably feel right at home turning a scope’s knobs for the time/div, volts/div, and trigger-level settings. Unfortunately, you can lose that comfortable feeling when you connect the scope to a computer to perform automated measurements. Don’t let a lack of familiarity deter you from using a PC to control your scope. Computers and custom software can help you use scopes to speed the development, test, and analysis of circuits and systems, regardless of the communications bus you use.

If you write your own test software, you can break down the task of controlling a scope into three fundamental steps—initializing the scope, capturing a waveform, and analyzing the data. These steps replicate the process you typically follow when you use a scope’s front-panel controls.

Initialize the Scope

When you operate a scope from its front panel, you can easily turn the knobs to view a signal and perform measurements on it. When you control a scope with a computer, however, you lose the visual image of the signal. Because you can’t see the signal, you need to initialize the scope before each use. 

Set the scope to a factory-default configuration to ensure the same starting condition every time. Then, use the scope’s front-panel controls to best display the waveform. Record each critical setting such as time/div, volts/div, and trigger level for use in your program. Assume you need to measure a 1-kHz, 100-mVp-p sine wave. You might need to set the scope’s front panel to 20 mV/div (or 160 mV full screen: 20 mV x 8 vertical divisions = 160 mV), 500 µs/div (or 5 ms full screen: 500 µs x 10 horizontal divisions = 5 ms) with a trigger level of 10 mV. Figure 1 shows the signal and the scope settings.

Figure 1. Programming commands let you control an oscilloscope with a computer. Commands perform the same actions as front-panel controls to configure the instrument.
Once you have the proper settings, you must convert them into software commands for the computer. Many scopes accept commands that send the scope’s current configuration data back to the PC. You can store the response in a file you can later upload to the scope to set the instrument. I don’t recommend this practice because the configuration strings typically follow a convention that’s unique to a scope model or manufacturer. Interpreting the settings may prove difficult, and if you change scopes, your initialization code may not properly control the new scope. Instead, if you use individual statements to configure each of the scope’s settings, you’ll get self-documenting code that describes the scope’s setup. 

You can program most scopes with strings of ASCII characters. The programming model in Figure 2 shows a set of subsystems. From the subsystem tree, you can build command strings to program the scope. Figure 2 shows several subsystems and their ASCII character strings for an HP scope—SYSTem:, ACQuire:, CHANnel:, TIMebase:, and TRIGger:. Other scope manufacturers use similar subsystem commands.

Figure 2. A subsystem tree lets you build ASCII string commands that program a scope.
Scopes that comply with the IEEE 488.2 standard will set themselves to their factory default settings when you send the "*RST" command. Setting the scope to its factory-default settings guarantees the scope will run consistently, and you’ll get repeatable measurements.

Convert each of the front panel’s critical settings to their equivalent commands in the subsystem tree. When setting the scope’s volts/div or time/div parameters, you can use either a SCALE or RANGE command. Therefore, you can set the volts/div parameter with either a ":CHAN1:RANGE 160 mV" command or a ":CHAN1:SCALE 20 mV" command. A trigger command and time-base command complete the setup in this example. The Basic code in lines 100–140 of Listing 1 demonstrate how I configured my scope. Your scope’s commands may differ but will follow a similar structure.

Listing 1
As a test of the initialization routine, run your code and observe the scope’s settings. To better observe and debug the code, have the PC step through each line. Add commands that require you to press a key between each line to pause the program. Or, you can single-step through the program if your programming development software has that feature. If you misspelled a word or omitted a space, colon, or quotation mark, the scope may reject the command. You should get an error on the scope’s display. Don’t ignore error messages, for they indicate an error in your code which might not properly configure the scope.

You also can detect invalid commands by writing a procedure that queries the scope for an error message. For example, with some HP scopes, the ":SYSTEM:ERROR?" query will return a zero value if it detects no errors, but it will return a nonzero value and error string if it receives an invalid command. Once you debug the initialization process and view the desired signal, you can remove the error query.

A PC can send any command to a scope at any time, so you must be careful how you program a sequence of commands to occur. The scope’s current settings and menus will limit the functions and parameters you can use from the front panel. For example, volts/div and offset settings limit the available range of trigger voltages. With an HP scope, if you set the scope to 100 mV/div (800 mV full screen) with zero offset, then you can’t set the trigger level to 1 V (off screen). At the 100 mV/div setting, the scope limits the trigger level to about 600 mV.

When you operate the scope by hand, you typically set the volts/div and offset values before setting the trigger level. Because you can send any command in any order from the PC, you must make sure you send the commands in the proper sequence or risk not getting the desired setup. For example, the scope can have a trigger level of 1 V after you’ve set the volts/div setting to 1 V/div. But if the scope’s current volt/div setting is at 100 mV/div and the PC sends the ":TRIGGER:LEVEL 1V" command, the scope will display an error message because the 1-V level is "off" the display screen. The error string will return a non-zero result, and the scope may set the trigger level to the closest possible value (in this case, 600 mV). If, however, you had first programmed the scope’s vertical sensitivity to 1 V/div and then programmed the 1-V trigger level, you would have achieved the desired trigger level.

Again, watch the scope’s display while the PC steps through the program or queries the scope for errors. Once the initialization procedure runs properly, you’re ready to capture the signal. 

Capture the Signal

When using a PC to control a scope, the program must ensure the scope has captured a signal before it can instruct the scope to measure parameters such as frequency or peak-to-peak voltage (Vp-p). If you send several configuration commands—such as those that set time/div, volts/div, or trigger level—to the scope followed by a measurement request, you may get an invalid measurement. The scope may have had time to configure itself but may not have had time to properly trigger and capture a waveform before the computer requests a measurement. You can guarantee correct waveform acquisition by using either explicit commands or synchronization techniques.

For example, HP scopes use the explicit command "DIGITIZE" to guarantee the scope has acquired a waveform before it makes any measurements. The "DIGITIZE" command erases any old waveform data, arms the trigger, acquires new waveform data when the trigger occurs, and stops the acquisition process. After the software recognizes the end of this process, your program can send measurement commands to the scope. The "DIGITIZE" command will delay incoming scope processes, such as an attempt at a measurement, until the scope completes signal acquisition. The program lines 200–220 in Listing 1 show how to use the "DIGITIZE" command.

You also can guarantee signal capture through synchronization techniques. For example, you can use the IEEE 488.2 operation-complete command "*OPC" to force a scope to wait until it completely captures a waveform before it executes another command. The "*OPC" command will set a bit to generate an interrupt . You can program the PC to wait for the interrupt before executing the next line of code. Or, a program can send an "*OPC?" query to the scope. The scope will complete all processes and then respond to the query. With some scopes, you can use the "*WAI" (wait) command to hold further scope communications and proc-esses until the scope completes a signal capture.

Some engineers will attempt to solve the capture/measure problem by adding wait statements to their programs. In a Basic program, WAIT 5 will cause the program to wait for 5 s, which may not be enough time for the scope to configure itself and capture a signal. Finding the correct wait time is often a hit-or-miss process in which you increase the length of the wait until things appear to work. This method is risky because you can write a program that works well with a wait statement, but adding just one or two new configuration commands may throw off the critical timing. Then, your measurements will be incorrect.

Analyze the Data

After capturing a waveform, you need to decide how to analyze it. If you require a simple parametric measurement such as the Vp-p or frequency, you can use the scope’s measurement subsystem. For example, to measure Vp-p and frequency, send the ":MEASURE:VPP?;FREQUENCY?" command and get the results from the scope. Lines 300–320 in Listing 1 show you how to query the scope so it can return the peak-to-peak voltage and frequency of the waveform stored in its memory.

A scope’s programming commands can perform all the functions of the front panel and more. For example, the ":MEASURE:ALL?" query will return the scope’s entire set of parametric measurements to your program, but the scope I used for this article lets me view just four to six measurements at a time on the display. If I transfer the measurements to a PC, I can see all the results at once. Many scope manufacturers use similar commands (such as "MEASURE?" or "VALUE?") to return the parametric measurements.

If you need a more thorough analysis, you can transfer a complete waveform record from the scope to a PC where you can analyze the data using your own program or data-analysis package. A numeric array holds the values that define the waveform—one value represents one data point of the digitized signal. 

Before you can transfer data, you must specify the data format (ASCII, single byte, or multiple bytes), record length, and waveform source. Lines 400–440 in Listing 1 show the format for transferring data to a PC.

The data block consists of a header that specifies the number of bytes of waveform data you will transfer, the data itself, and a terminator. In Basic, you should process each parameter on a separate line of code (lines 450–500).

Scopes usually store waveforms in a format such as analog-to-digital converter (ADC) raw counts that you must convert into volts (y-axis) with a time scale (x-axis). For example, with an 8-bit ADC converter, a data value of 1 might indicate a point at the bottom of the display while 255 will indicate a value at the top of the display. So, the program must next request scaling factors—Yreference, Yincrement, and Yorigin—to convert the data to units of volts. Likewise, you also must associate a time value with each point (lines 500–540).

The values of both the x and y scale factors depend on the scope’s horizontal and vertical scale settings, respectively. For example, Yorigin is the channel offset setting, or the voltage value at the center of the display. Yincrement is the vertical sensitivity value based on the volts/div setting. 

After it requests and receives the scale factors, the scope can convert each data point to a corresponding voltage/time pair. Lines 550–590 show a loop that converts 4000 raw data values to units of volts with a time scale.

Some scopes can perform the conversion from ADC counts to volts rather than send only raw counts to a computer. That relieves the PC of having to perform the conversions, and the PC can store the results to a fixed or floppy disk. For example, the command ":DISK:STORE CHANNEL1,"FILE1",YVALUES" will first convert the waveform data to voltage values and then store to the specified file. TMW

Copyright © 1999 Cahners Business Information, a division of Reed Elsevier. All rights reserved.

Test & Measurement World, 275 Washington Street, Newton, MA 02458-1630, USA