Using the Script Editor

Introduction

This guide explains the use of WaveForms'Script Editorinstrument. This instrument is used to create, run, and debug scripts that can control all of WaveForms' other instruments.


Prerequisites


Guide

1. Opening the Script Editor

1.1

Plug in the Test & Measurement Device, then start WaveForms and make sure the device is connected.

如果没有连接到主机设备时n WaveForms launches, theDevice Managerwill be launched. Make sure that the device is plugged in and turned on, at which point it will appear in the Device Manager's device list (1.). Click on the device in the list to select it, then click the Select button (2.) to close the Device Manager.

Note:“DEMO” devices are also listed, which allow the user to use WaveForms and create projects without a physical device.

Note:The Device Manager can be opened by clicking on the “Connected Device” button in the bottom right corner of the screen (3.), or by selecting “Device Manager” from the “Settings” menu at the top of the screen.


1.2

Once the Welcome page loads, in the instrument panel at the left side of the window, click on theScriptbutton to open theScript Editorinstrument.


1.3

When theScript Editorhas opened, it will display the text editor (1), the output panel (2) below the editor, and the control toolbar (3) at the top.


2. Using the Script Editor

This section walks through how to use theScript Editor. Two examples are presented below. Other example scripts are available within the Script Editor.

Oscilloscope and Waveform Generator

2.1 Hardware Setup

To demonstrate theScript Editor,OscilloscopeandWaveform Generatorinstruments will be used.

For devices using MTE cables, first, connect the Test & Measurement scope channel 1 pin (orange wire) to the device's Waveform Generator Channel 1 output pin (yellow wire). For devices that use differential input channels, make sure to connect the scope channel 1 negative pin (orange wire with white stripes) to the ground pin associated with Waveform Generator Channel 1 (black wire).

If you instead want to use BNC cables, or your device does not support MTE cables, please seeUsing the Oscilloscopeand/orUsing the Waveform Generatorfor additional information.


2.2 Software Setup

For theScript Editorto make use of the instruments, they must be open in WaveForms. In WaveForms' Welcome tab, open theOscilloscopeinstrument, then open theWaveform Generatorinstrument. Their settings do not need to be changed.


Return to theScripttab to view theScript Editorinstrument.

Open theScope and Wavegenexample script, by selecting it from theExampledropdown.


Prior to running the script, what it does and how it works will be briefly discussed:

  1. The “clear” call empties theOutputpane of any statements that have previously been printed.
  2. The second line checks whether theOscilloscopeandWaveform Generatorinstruments have been opened, and throws an error if either is missing.
  3. Several options for the two instruments are then set. Using mostly default options, the Waveform Generator Channel 1 outputs a 1KHz sine wave with a 1V amplitude and an initial DC offset of 0.5V. The Oscilloscope trigger is set to capture repeated trigger events of the input signal rising past a 0V level.
  4. Next, both instruments are started with a “run” call, which has the same effect as if theRunbutton in each instrument was pressed.
  5. The remaining steps are looped 10 times, waiting for one second between each iteration - wait returns True once the number of seconds passed to it have elapsed.
  6. The “Scope1.wait” statement checks whether theOscilloscopeorScript Editorinstruments have been manually stopped since the script began running, and throws an error if either is no longer running.
  7. Several variables are then used to store some measurements about the data that was captured on Oscilloscope Channel 1. In this case, the “Middle” and “Average” measurements are printed.
    • The “Middle” is the average of only the highest and lowest samples captured.
    • The “Average” is the average of all of the samples in the dataset.
  8. Lastly, theWaveform Generator's DC offset voltage is changed, by subtracting half of the Middle voltage.

Summarizing, theWaveform GeneratorandOscilloscopeinstruments are configured, run, and used to slowly tune the DC offset of the Waveform Generator so that the middle of the captured data approaches 0V.

// This script adjusts the Wavegen offset based on Scope measurement.clear()if(!('Wavegen1'inthis)||!('Scope1'inthis))throw"Please open a Scope and a Wavegen instrument";Wavegen1.Channel1.Mode.text="Simple";Wavegen1.Channel1.Simple.Offset.value=0.5;Scope1.Trigger.Trigger.text="Repeated";Wavegen1.run();Scope1.run();for(varidx=0;wait(1)&&idx<10;idx++){// if(!Scope1.wait()) throw "Stopped";varmiddle=Scope1.Channel1.measure("Middle");varaverage=Scope1.Channel1.measure("Average");print(idx+" Middle: "+middle+" V"+" Average: "+average+" V");// adjust Wavegen offsetWavegen1.Channel1.Simple.Offset.value-=middle/2;}

More information on the functions and objects available in theScript Editorcan be found in the in WaveForms'Helptab'sScriptpage. Additional configuration options for each of the individual instruments not covered by the Script page can be found in the pages dedicated to those instruments.


2.3 Running the Script

Return to the Scripts page by clicking on its tab in the bar at the top of the screen.

Click the Run () button in theScript Editortab to start running the example.

Print statements and uncaught errors are shown in theOutput面板。


2.4 Debugging

If in the process of writing a script, the script needs to be stepped through to find errors, the Script Editor's debugger can be used.

Debugging is done inside WaveForms itself, allowing the user to step through the execution of the script. The debugger is accessed by clicking the Debug () button, which will open a new window with the Qt debugger tool.

Protocol Analyzer

2.1 Hardware Setup

To demonstrate theScript Editor,Protocol Analyzerinstrument will be used to transmit and receive UART signals.

Connect the Test & Measurement Device's digital I/O channel 0 pin (DIO0) to its digital I/O channel 1 pin (DIO1).


2.2 Software Setup

For theScript Editorto make use of theProtocol Analyzerinstrument, it must be open in WaveForms. In WaveForms' Welcome tab, open theProtocolinstrument and open the UART Send and Receive page. Its settings do not need to be changed.

Open theProtocol UARTexample script, by selecting it from theExampledropdown.


Prior to running the script, what it does and how it works will be briefly discussed:

The Protocol Analyzer instrument is configured in UART mode, and is used to send and receive data. First, the string “Hello” is sent, received, and printed to the Output pane. This is repeated with the string “Hello”, with an tab character added to the end, and for an array of data specified as integers.

// Script using Protocol UART.if(!('Protocol'inthis))throw"Please open the Protocol tool";Protocol.Mode.text="UART"// make sure UART tab is selectedProtocol.UART.Receiver()// reset receiverProtocol.UART.Send("Hello")// send text with endingvarrx=Protocol.UART.Receive()// receive textprint(rx)Protocol.UART.Send("Hello ",false)/ /发送短信定制的结局varrx=Protocol.UART.Receive()// receive textprint(escape(rx))Protocol.UART.SendArray([1,2,3,4,5,0],false)/ /发送数组定义dingvarrx=Protocol.UART.ReceiveArray()// receive arrayprint(rx)

More information on the functions and objects available in theScript Editorcan be found in the in WaveForms'Helptab'sScriptpage. Additional configuration options for each of the individual instruments not covered by the Script page can be found in the pages dedicated to those instruments.


2.3 Running the Script

Click the Run () button to start running the example.

Print statements and uncaught errors are shown in theOutput面板。


2.4 Debugging

If in the process of writing a script, the script needs to be stepped through to find errors, the Script Editor's debugger can be used.

Debugging is done inside WaveForms itself, allowing the user to step through the execution of the script. The debugger is accessed by clicking the Debug () button, which will open a new window with the Qt debugger tool.


3. Script Editor User Interface Overview

This section walks through the different controls and features present in theScript Editor.

3.1 The Control Toolbar

The control toolbar contains menus and control interfaces used to configure theScript Editor, listed below:

  • Debug: Opens a QT Script Debugger window.
  • Run: Executes the script.
  • 停止: Stops debugging or execution of the script. For停止to work, the code needs to use theinstrument.wait() function.
  • Abort: Forces the execution to halt immediately.
  • Zoom In&Zoom Out: Used to increase or reduce the font size of the code editor respectively.
  • Clear: Clears the Output Panel
  • Example: Contains a set of example scripts that use a variety of different instruments.


3.2 The Menu Bar

Fileis used to open an existing WaveFormsScript Editorproject, save the currentScript Editorproject, or close theScript Editor.

Editcontains buttons for Undo and Redo, as well as options for enabling shortcuts for code completion and finding and replacing.

Controlaccesses the Debug, Run, Stop and Abort actions.

Viewhas options to enable the Output panel and a plotting window.

TheWindowmenu can be used to access other instruments, the WaveForms home page and the WaveForms Help.


3.3 The Search Toolbar

This toolbar can be used to search for certain parts in the script, and to replace those parts. Settings for wildcard support in the input string can be opened with theicon.


3.4 The Script Editor

The Script Editor is an editor where the JavaScript code can be viewed and edited.


3.5 Output Panel

The output panel contains the results of print() statements and error messages.


3.6 WaveForms Help Tab

Accessed viaHelpin the tab bar, the WaveForms Help Tab contains reference materials for all WaveForms instruments.

Section 4,“Code”, of the Help tab's“Script”page documents the functions and objects that can be used in WaveForms scripts, the scriptingAPIfor WaveForms.

Next Steps

For more guides on how to use the Digilent Test and Measurement Device, return to the device's Resource Center, linked from theTest and Measurementpage of this wiki.

For more information on WaveForms visit theWaveForms Reference Manual.

For technical support, please visit theTest and Measurementsection of the Digilent Forums.