Example 1. "Hello World"

Instrument:
N1000A
N109x
UXR Scope
Flex Apps:
FlexDCA
FlexRT
Meas. mode:
Scope
Eye
Jitter
TDR
Package License:
L-RND

This Python example is the equivalent of a typical "Hello World" script. It is the simplest possible example designed to prove that your FlexDCA / Python environment is working correctly and to illustrate all of the basic steps required to create a user operator. After you've been successful with this example, try the Lesson 2. Creating a "Show Input Variables" User Operator Script.

Like all user operator Python scripts, HelloWorld.py consists of the following items:

  • A required algorithm function. Your script can have additional functions, but it must have the algorithm function as the main function.
  • FlexDCA passes a dictionary of input variables (variables), including the input waveform (variable['SrcData']) to the algorithm function.
  • Return a dictionary of measurement results to FlexDCA including the output waveform key:value pair: FiltData" : FiltData.

Procedure

  1. Make sure that you have fullfilled the basic requirements for Python.
  2. Copy the following XML listing into a text editor. Name the file HelloWorld.xml.
  3. Copy

    HelloWorld.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Function xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Script>HelloWorld.py</Script>
        <Name>Hello World!</Name>
    <FunctionType>1 Source</FunctionType>
    </Function>
    The <Icon> element is optional. This element allows you to place a picture on your user operator icon.
    Example without <Icon> element
    Example with <Icon> element
  4. Copy the following script, HelloWorld.py and paste it into your Python editor. Save HelloWorld.py in the same folder that you saved the HelloWorld.xml file. In Helloworld.py:
    • Input waveform, SrcData, that is passed as a key:value pair in the variables dictionary.
    • This is a very simple script that doesn't even filter the waveform. It simply passes the input waveform to the output waveform, FiltData.
    • The Gain variable has been set to 2.0, so that you can more easily see the two waveforms displayed.
    • The print statement that can be used for troubleshooting by writing text to the Show Output dialog.
    • The algorithm function is required in every Python user operator script. Notice that the algorithm function begins by initializing the nine input variables that are returned to FlexDCA in a dictionary (key:value pairs). The order that you list the key:value pairs is not import, however all key:value pairs must be returned.

    The algorithm function name must be written in lower case. Naming the function Algorithm.m or ALGORITHM, for example, will break the script.

    Copy
    HelloWorld.py
    def algorithm(variables):
        FiltData = []
        XOrg = 0.0
        XInc = 0.0
        Gain = 2.0
        FilterWidth = 0.0
        FilterDelay = 0.0
        XUnits = 'Same'
        YUnits = 'Same'
        ErrorMsg = ''
        
        SrcData = variables['SrcData']
        XOrg = variables['XOrg']
        XInc = variables['XInc']
        print('Hello World!')
        FiltData = SrcData
        return { "FiltData" : FiltData,
                 "XOrg" : XOrg,
                 "XInc" : XInc,
                 "Gain" : Gain,
                 "FilterWidth" : FilterWidth,
                 "FilterDelay" : FilterDelay,
                 "XUnits" : XUnits,
                 "YUnits" : YUnits,
                 "ErrorMsg" : ErrorMsg
                }
  5. Place FlexDCA into Oscilloscope mode and display a single waveform.
  6. On FlexDCA's menu, click Measure > Waveform Signal Processing (Math).
  7. At the top of the Waveform Signal Processing dialog, select the User tab and drag a single-input User operator icon into the operator construction area as shown in the following picture.
  8. Click on the user operator to open the User Operator Setup dialog.
  9. In the dialog, click Browse and select your XML configuration file, HelloWorld.xml.
  10. In the Waveform Signal Processing dialog, drag a Function Color to the User Operator as shown in the following picture. This will display the output waveform of the function.
  11. Click Autoscale and the input and output waveforms should be displayed. Of course, your waveforms will be different from those shown here. The important thing is that both the input and output waveforms are displayed. Notice that the Signals legend identifies the operator's output waveform.
  12. On FlexDCA's menu, click Measure > Waveform Signal Processing (Math) to open the Waveform Signal Processing dialog if you have closed it.
  13. Click on your operator to open the User Operator Setup dialog, and click the Show Output button. This button only appears on the dialog after the measurement has been started.
  14. In the displayed Output dialog, you should see the output of the print statement in your HelloWorld.py script. One Hello World! is printed for each waveform acquisition. For this picture, FlexDCA was in Single acquisition mode with the Single button clicked three times. You can use this feature to troubleshoot your scripts.