2600S-901-01 Rev. C / January 2008 Return to Section Topics 2-5
Series 2600 System SourceMeter® Instruments Reference Manual Section 2: TSP Programming
scripts can be made available simultaneously within the limits of the memory available to the run-
time environment.
Named scripts are stored as global variables in the run-time environment. Like all other global
variables, when the unit is powered off, they are lost. There is non-volatile storage on the instrument
that can be used to store downloaded scripts across power cycles. See “
Saving a user script” later in
this section for more information.
Functions
As previously explained, named scripts behave just like TSL functions. Executing a script is just like
executing a function with the same name as the script. Scripts, like functions, may return values.
Unlike functions, scripts may not take any parameters. In order to pass parameters to a chunk, you
must make a TSL function.
Functions are created with a message in one of the following forms:
MyFunction = function (parameter1, parameter2) function body end
or
function MyFunction(parameter1, parameter2) function body end
Where function body is a TSP chunk that will be executed when the function is called. The
above function can be executed by sending the following message:
MyFunction(value for parameter1, value for parameter2)
Where value for parameterN represents the values to be passed to the function call for the
given parameters. Note that when a function is defined, it is just another global variable in the run-
time environment. Just like all global variables, functions will persist until they are removed from
the run-time environment, overwritten, or the unit is turned off.
Scripts that create functions
It is inconvenient in most cases to define a function in one message. The solution is to create a
script that defines a function. The scripts will be like any other script. It will not cause any action to
be performed on the instrument until it is executed. Remember that creating a function is just
creating a global variable that is a function. That global variable will not exist until the chunk that
creates it is executed. In this case the chunk that creates it is a script. Therefore, the function will
not exist until the script that creates it is executed. This is often confusing to first time users.
Example: Create the function MyFunction with a script named MakeMyFunction. The sequence of
messages to do this is shown as follows:
loadscript MakeMyFunction
MyFunction = function (who) --The .. operator concatenates two strings.
print("Hello " .. who)
end
endscript
After this sequence of messages is sent, the MakeMyFunction script exists on the instrument in a
global variable named
MakeMyFunction. The MyFunction function however does not yet exist
because we have not executed the
MakeMyFunction script. Let us now send the message
MakeMyFunction(). That message instructs the instrument to run the MakeMyFunction script
which then creates the
MyFunction global variable that happens to be a function.
If we now send the message MyFunction("world"), the instrument will execute the MyFunction
function, which causes the instrument to generate a response message with the text “Hello world”
in it.