Example SCPI Script
This script connects to an N6705C power analyzer using its alias. The defaultChannel for the connection is set to 2. By default, all the commands sent to the device are addressed to that channel.
The script then uses a repeat loop to set the voltage to different values and retrieves the actual measured voltages. At the end, it displays some information about the differences between the set and actual voltages.
The script uses a tell block to enclose the portion of the script that communicates with the device. This means that all the device-related commands, like reset and setValue, are sent to the device.
Note: SenseTalk commands within the tell block continue to function normally. Technically, they are also sent to the powerAnalyzer object, but since that object doesn’t implement a put or insert command, the commands are passed along to the engine as usual.
Tip: For a line-by-line description of the script, see the Example Script Analysis section.
Example Script:
set powerAnalyzer to visaDevice ("N6705CPower")
set powerAnalyzer’s defaultChannel to 2
tell powerAnalyzer
reset
setValue output, on
repeat with testVoltage = 3300 millivolts to 4.4 volts stepping by 100 millivolts
setValue voltage, testVoltage
query "measure voltage"
put value(it) volts into measuredVolts
put ABS(measuredVolts - testVoltage) into difference
convert difference to microvolts
insert difference into allDifferences
put !"Set voltage to [[testVoltage]] Actual measured was [[measuredVolts]] (difference [[difference]])"
end repeat
setValue output, off
end tell
put
put "Differences: " & allDifferences sorted
put
put "Average: " & average of allDifferences
put "Median: " & median of allDifferences
Example Script Analysis
To help you to write your own scripts, here is a line-by-line description of the functionality in the example SCPI script.
set powerAnalyzer to visaDevice ("N6705CPower")
Establishes powerAnalyzer as a VisaDevice object, which is used for all communication with the instrument.
set powerAnalyzer’s defaultChannel to 2
Setting the device’s defaultChannel means that all commands directed to the device using setValue or query calls use this channel. This simplifies and streamlines the use of these functions. If required, you can still direct individual calls to a different channel, for example: on channel 3.
tell powerAnalyzer
By enclosing lines in a tell block, until the end tell statement, the script directs all calls, such as reset, setValue, and query, to the powerAnalyzer object without needing to name it. This greatly simplifies the script.
reset
Sends a reset *RST command to the powerAnalyzer object.
setValue output, on
To turn on the power output on channel 2, the script sends an OUTPUT SCPI command to the powerAnalyzer object with the parameter value on.
Without quotes, if there is a variable called output, then SenseTalk uses the value of that variable. If there is no variable called output, as in this case, then "output" is used instead.
repeat with testVoltage = 3300 millivolts to 4.4 volts stepping by 100 millivolts
A repeat loop sets the variable testVoltage to each of the values from 3.3 volts (3,300 millivolts) to 3.4 volts, to 3.5 volts, and so on, up to 4.4 volts. As written, this statement illustrates two useful SenseTalk features for working with test and measurement devices:
- The use of values with units
- The ability to mix different compatible units in your calculations. In this case, volts and millivolts.
setValue voltage, testVoltage
Sends a VOLTAGE SCPI command to the powerAnalyzer object with the parameter value equal to the current value of testVoltage. As with output, the word "voltage" can be in quotes.
query "measure voltage"
Sends a MEASURE:VOLTAGE? SCPI command to the powerAnalyzer object. Because query is called as a command, rather than a function, the measured value returned from the device is automatically stored in the variable it.
put value(it) volts into measuredVolts
This command does two important things. It calls the value function to convert it into a simple numeric string, as it is typically returned by the device as a text string in scientific notation, e.g.: "+1.000000E+01".
Note: SenseTalk automatically converts simple string representations of numbers into their numeric values when needed, but it doesn’t do this for values in scientific notation.
Secondly, since the value returned from the device represents a voltage, the statement assigns volts as the value’s units. This allows later commands in the script to take advantage of SenseTalk’s unit conversion and calculation abilities. It also guards against mismatched unit errors. For example, if you try to add volts and amps, SenseTalk throws an exception.
put ABS(measuredVolts - testVoltage) into difference
Assigns the absolute value of the difference between the measured voltage and the set voltage to the variable difference.
convert difference to microvolts
Converts the variable difference from units of volts to microvolts, which is a more readable form for this output since the differences are small.
insert difference into allDifferences
Inserts the value difference into a list which, after the repeat loop is finished, contains the measured difference for each testVoltage.
put !"Set voltage to [[testVoltage]] Actual measured was [[measuredVolts]] (difference [[difference]])"
Displays a message for the current testVoltage, which shows the:
- Test voltage
- Measured voltage
- Difference between the two measurements.
The !before the string means that each expression in double square brackets within the string is replaced by its value.
end repeat
The end of the repeat loop for each testVoltage.
setValue output, off
To turn off the power output on channel 2, the script sends an OUTPUT SCPI command to the powerAnalyzer object with the parameter value off.
end tell
The end of the tell block directing messages to the powerAnalyzer object.
put
put "Differences: " & allDifferences sorted
put
put "Average: " & average of allDifferences
put "Median: " & median of allDifferences
Displays messages reporting information about the measured differences in voltages.