Script

The Script window allows execution of WaveForms scripts. The scripting language is JavaScript, based on the ECMA Script standard.

From the script code, objects behind the user interface can be accessed. This allows configuration using the interface and automating parts of it with script.

 

1. Menu

See Menu in Common Interfaces.

 

2. Control

The control toolbar allows you to debug and execute the script.

 

3. Output

The output window contains the print() messages and error log.

 

4. Code

The instrument's object can be accessed from the script and has the most relevant child objects and parameters shown in the table below.

The instrument parameters are stored in objects with the following properties:

 wait(seconds) Waits the specified time expressed in seconds. If the value is 0 or if the argument is missing, then the function is ignored. With negative value, it waits until stopped. Returns false when the script is stopped; otherwise returns true.
 print(arg0,arg1,...)Prints the arguments to the Output window.
 
Device
 .isConnectedis connected to a device
 .namedevice name
 .SNserial number
 
instrument#common instrument object
 .single()single acquisition
 .run()run instrument
 .stop()stop instrument
 .wait()wait for the acquisition to be done or instrument stopped
 .State
  .textinstrument states: Disabled, Ready, Done, Stop, Busy, Error, Config, Armed, Trig'd, Running, Auto, Scan, Wait
  .running()returns true when the instrument is in active state, >= Config
  .Rate.valuerefresh rate of the instrument, the device communication rate
 .Time
  .Position.value
  .Base.value
  .Samples.value
  .Rate.value
 
 Scope#
  .measure(name)global measurements
  .Triggertrigger configuration
   .Mode.textacquisition mode: Normal, Auto, None
   .Run.textrun-scan mode: Repeated, Screen, Shift
   .Type.texttrigger type: Edge, Pulse, Transition
   .Source.texttrigger source
   .Condition.textcondition: Rising, Falling or Positive, Negative
   .Level.value
   .Hysteresis.value
   .LCondition.value
   .Length.value
   .HoldOff.value
   .Filter.textsample type to use when looking for trigger condition: Decimate, Average
  .BufferSize.valuePC buffer size
  .Buffer.valueselected PC buffer index
  .Digital.checkedenable or disable digital channels
  .DigitalChannelsdigital channels, same as Analyzer#.Channels
  .DigitalTriggersame as Analyzer#.Trigger
  .channel#the following properties are available for Math and Ref channels as well:
   .checkedchannel checked, visible
   .dataget data array
   .measure(name)Channel measurements: Minimum, Maximum, Average, Peak2Peak, Low, High, Amplitude, Middle, ...
   .Offset.value
   .Range.value
   .histogramgets the histogram array
   .HistogramIndex2Data(index)returns the “voltage” value corresponding to the histogram array index
  .Channel#real channels
   .Attenuation.value
   .Noise.checked
   .SampleMode.textsampling mode: Decimate, Average, Min/Max
  .Ref#reference channels
   .dataset data
   .clone(channel)clone with channel
   .TimeRefreference channel time
   .Units.text
   .Noise.checked
  .Math#mathematic channels
   .Operation.textsimple mode operation
   .Asimple mode channel A
   .Bsimple mode channel B
   .Custom.checkedenable custom mode
   .Function.textcustom function
   .Units.text
  .Histogram
   .Autoscale.checked
   .Top.valuetop of the scale to use when auto-scale is unchecked
 
 .Wavegen#
  .Synchronization.textsynchronization mode: No synchronization, Independent, Synchronized, Auto synchronized
  .Statescommon synchronization states settings
   .Trigger.text
   .Wait.value
   .Run.value
   .Repeat.value
   .RepeatTrigger.checked
  .Channel#
   .Mode.textconfiguration mode: Simple, Basic, ...
   .Options
    .IdleOutput.textidle output value: Offset, Initial, Disabled
   .Stateschannel states configuration
   .Simple
    .Type.textwaveform type: Sine, Square, ...
    .Frequency, Offset, Amplitude, Symmetry, Phase
    .value
   .Basic
    .Type.text
    .Frequency, Offset, Amplitude, Symmetry, Phase
     .value
     .Min.value
     .Max.value
   .Custom
    .Type
     .text
     .set(name,array)set custom waveform data (normalized to +-1) identified by name
     .add(array)add custom waveform, name of waveform is returned
    .Frequency, Offset, Amplitude, Symmetry, Phase
     .value
   .Sweep
    .Type
     .text
     .set(name,array)set custom waveform data (normalized to +-1) identified by name
     .add(array)add custom waveform, name of waveform is returned
    .Offset, Symmetry, Phase
     .value
    .Frequency
     .checkedenable sweeping
     .Start.valuestart frequency
     .Stop.valuestop frequency
     .Time.valuesweep time
    .Amplitude
     .checkedenable damping
     .Start.valuestart amplitude
     .Stop.valuestop amplitude
     .Time.valuedamp time
 
 .Analyzer#
  .Triggertrigger configuration
   .Mode.textacquisition mode: Normal, Auto, None
   .Clock.textclock source: Internal, External
   .Source.texttrigger source
   .Run.textrun-scan mode: Repeated, Screen, Shift
  .BufferSize.valuePC buffer size
  .Buffer.valueselected PC buffer index
  .Channels
   .channelchannel like: DIO1, Bus1, SPI1
    .namechannel name
    .dataget text array
    .eventsget events text array
    .eventStampsget time stamp array
   .DIO#
    .DIO.valuedigital I/O pin index
    .Trigger.texttrigger: Ignore, Rise, Fall, Edge, High, Low
   .Bus#
    .Enable.DIO.valueenable pin
    .Clock.DIO.valueclock pin
    .Active.textenable active level: Low, High
    .Polarity.textsampling clock edge: Rising, Falling
    .Format.text
    .Endianness.text
    .MSB.text
    .LSB.text
    .Pin#
     .DIO.valuedigital I/O pin index
   .SPI#
    .Select, Clock, Data
     .DIO.valuedigital I/O pin index
    .Active.textselect active level: Low, High
    .Sample.textsampling clock edge: Rising, Falling
    .First.textdata bit order: LSB, MSB
    .Bits.valueword bits
    .Format.text
    .Leading.valueskip leading bits
    .Ending.valueskip ending bits
   .I2C#
    .Clock, Data
     .DIO.valuedigital I/O pin index
   .UART#
    .Data
     .DIO.valuedigital I/O pin index
    .Bits.valueword bits
    .Parity.textparity setting: “None”, “Even”, “Odd”, “Mark (High)”, “Space (Low)”
    .Rate.valuebaud rate setting
 
 .Patterns#
  .States
   .Trigger.text
   .Wait.value
   .Run.value
   .Repeat.value
   .RepeatTrigger.checked
  .Preview.textpreview mode: Manual, Auto
  .Channels
   .channelchannel like: DIO1, Bus1, SPI1
    .namechannel name
    .dataget data array
   .DIO#
    .DIO.valuedigital I/O pin index
   .Bus#
    .Format.text
    .Endianness.text
    .MSB.text
    .LSB.text
    .Pin#
     .DIO.valuedigital I/O pin index
 
 .StaticIO
  .Channel#groups of 8 signals
   .Mode.textselect between: I/Os, Slider, Progress
   .Input.valueinput value
   .DIO#I/Os
    .Input.checkedinput value
    .Mode.textselect between: LED, Button, Switch
    .Button.textselect between: 0/1, 1/0, Z/1, 1/Z, Z/0, 0/Z
    .Switch.textselect between: TS, PP, OS, OD
 
 Supplies
  .MasterEnable.checked
  .Outputfor Analog Discovery
   .PositiveSupply.Enable.value
   .NegativeSupply.Enable.value
  .Outputfor Electronics Explorer
   .PositiveSupply, NegativeSupply
    .Enable.value
    .Voltage.value
    .Current.value
   .DigitalSupply, Refernce1, Reference2
    .Enable.value
    .Voltage.value
  .Inputfor Electronics Explorer same as Logger#.Input
 Logger
  .Inputfor Analog Discovery uses Scope inputs
   .Channel#
    .DC, TrueRMS, ACRMS
     .value
  .Inputfor Electronics Explorer
   .PositiveSupply, NegativeSupply, DigitalSupply
    .Voltage.value
    .Current.value
   .Voltmeter#
    .Voltage.value
  .Loggerschannels in history list
  .History.value
  .Samples.value
 
 Network#
  .Frequencyfrequency settings
   .Samples.valuefrequency steps
   .Start.valuestart frequency
  .WaveGenWaveGen settings
   .Channel.text"External", "WaveGen1", "WaveGen2"
   .Offset.valuestimulus signal offset
   .Amplitude.valuestimulus signal amplitude
   .Settle.valuesettle time between frequency adjustment and capture
  .Channel#
   .Range.textvoltage range
   .Offset.valueoffset
   .Attenuation.valuechannel attenuation
   .Gain.valuegain, relation between WaveGen amplitude and channel range
   .magnitudearray of magnitude values
   .phasearray of phase values
   .frequencyarray of frequency steps
 
 File(path-name)file object constructor
  .exists()returns true if the file exists; otherwise returns false
  .getName()returns the file name
  .getPath()returns the file path
  .getSize()return file size in bytes
  .isReadOnly()return true if the file is read-only; otherwise returns false
  .isHidden()return true if the file is hidden; otherwise returns false
  .getLastModified()returns the date and time when the file was last modified
  .getCreation()returns the date and time when the file was created
  .rename(name)
  .copy(path)
  .move(path)
  .deleteFile()
  .read()read file content as string
  .read(size)reads up to size bytes from the file as string
  .readArray() reads the entire file as string array, expects values split by comma, white space, or new line
  .write(string)write argument as string to file
  .writeLine(string)write argument as string to file followed by a new line
  .append(string)appends argument as string to file
  .appendLine(string)appends argument as string to file
  .readInteger() read file content as integer (32-bit) array
  .writeInteger([])write array argument as integer array
  .appendInteger(number)appends argument as integer to file
  .readFloat() read file content as floating point number (32-bit) array
  .writeFloat([])write array argument as floating point number array
  .appendInteger(number)appends argument as floating point number to file
  .readDouble() read file content as double precision floating point number (64-bit) array
  .writeDouble([])write array argument as double precision floating point number array
  .appendDouble(number)appends argument as double precision floating point number to file
 
 Toolhelper functions
  .question(text)
Opens a Yes or No question message box with the given text. Returns true when Yes pressed; otherwise returns false.
  .getText(label, text)
Get a string from the user.
  .getNumber(label, value, minimum, maximum, decimal places)
Gets a floating point input from the user.
  .getInteger(label, value, minimum, maximum)
Gets an integer input from the user.
  .getItem(label, value, items array, current index, editable)
Lets the user select an item from a list. Returns the selected text.
  .start(program, argument array, work directory)
Starts the program with the given arguments. Returns true on success; otherwise returns false.

 

5. Examples

The following code starts the waveform generator instrument, performs an oscilloscope acquisition, then computes and prints the average value.

Wavegen1.run()
Scope1.single()
Scope1.wait()
var average = 0
var data = Scope1.Channel1.data
data.forEach(function(sample){
  average += sample
})
average /= data.length
print("Average: "+average+"V")

The following code saves the acquisition sample array to a file.

File("C:/temp/acquisition.csv").write(Scope1.Channel1.data)

The following code sets the trigger source and level of oscilloscope instrument based on user input.

Scope1.Trigger.Source.text = Tool.getItem("Source", Scope1.Trigger.Source.preset, Scope1.Trigger.Source.value)
Scope1.Trigger.Level.value = Tool.getNumber("Level", Scope1.Trigger.Level.value)

The following code creates a custom waveform and sets it to be further used.

wave = Array()
for(var i = 0; i < 10; i++){
  wave.push(0)
  wave.push(i/10)
}
Wavegen1.Channel1.Mode.text = "Custom"
Wavegen1.Channel1.Custom.Type.set("MyWave", wave)
Wavegen1.Channel1.Custom.Type.text = "MyWave"

The following code increases the offset level.

Wavegen1.Channel1.Mode.text = "Simple"
Wavegen1.start()
while(!Tool.question("Are we there yet?")){
  Wavegen1.Channel1.Simple.Offset.value += 0.1
  print("Offset: "+ Wavegen1.Channel1.Simple.Offset.text)
}

The following code sets the slider for one Static I/O and increments the value twice a second.

StaticIO.Channel0.Mode.text = "Slider"
for(var i = 0; wait(0.5); i++, i%=256){
  StaticIO.Channel0.Slider.value = i
}
StaticIO.Channel0.Mode.text = "Progress"