Script

The Script window allows execution of WaveForms scripts. The scripting language is Java Script, 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.

See Menu in Common Interfaces.

 

1. Toolbar

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

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

 

2. Code

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

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

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()return 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 data 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 Meter#.Input
 Meter
  .Inputfor Analog Discovery uses Scope inputs
   .Channel#
    .DC, TrueRMS, ACRMS
     .value
  .Inputfor Electronics Explorer
   .PositiveSupply, NegativeSupply, DigitalSupply
    .Voltage.value
    .Current.value
   .Voltmeter#
    .Voltage.value
  .Meterschannels in history list
  .History.value
  .Samples.value
 
 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()
  .isReadOnly()
  .isHidden()
  .getLastModified()
  .getCreation()
  .rename(name)
  .copy(path)
  .move(path)
  .deleteFile()
  .readAll()
  .readArray()reads the entire file as array split by comma
  .read(size)reads up to size bytes from the file
  .write()
  .writeLine()
  .append()
  .appendLine()
 
 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.
  .wait(seconds)Waits the specified number time expressed in second unit. With negative value, it waits until not stopped. Returns false when the script was stopped; otherwise returns false.
  .start(program, argument array, work directory)
Starts the program with the given arguments. Returns true on success; otherwise returns false.

 

3. 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.start()
while(!Tool.question("Are we there yet?")){
 Wavegen1.Channel1.Simple.Offset.value += 0.1
}

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

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