The operation of the Library Interface Section 1-1
2
1-1 The operation of the Library Interface
This section explains how a library function is loaded into user memory, how the corresponding Basic user
function is defined and how the function is called from the Basic program. Section 1-1-1 explains these three
steps in detail, and section 1-1-2 shows an example of using a simple library function according to the same
three steps.
All Basic statements in the example of section 1-1-2 are explained in section 1-3. All that is needed for the
example is a cross-compiled library function and an ASCII unit (test-)setup as shown here:
1-1-1 How to use a library function
1. Load the library function
Loading a library function is done with the LIB LOAD statement. The library function to be loaded can be
of two types:
compiled for a fixed address,
compiled for a free address.
If the library function is compiled to be stored at a fixed address, this address should be passed to the LIB
LOAD statement. Else, the ASCII unit will allocate a proper memory block for the library function itself. For
more information on fixed or free addresses for library functions, see section 2-2-2.
If a library function is to be loaded at a fixed address, but some Basic variables reside somewhere in that
part of memory, LIB LOADing will result in an ‘out of memory’ error. The CLEAR command can be used to
remove these temporary memory blocks, assuming that Basic is operating in dynamic mode.
2. Define a Basic user function for the library function
Defining a Basic user function for the library function is done with the DEF LIBFN statement.
This statement tells Basic the type of the parameters and the return value. This statement is in a way
similar to the normal DEF FN statement, the only difference is that in this case the functionality is
described in the library function, instead of in a Basic function. Also, the programmer must specify the
number of the library function to which this user function is referring.
It is possible with the DEF LIBFN statement to determine output and throughput parameters as well. This
is done by means of the VARPTR function. This function returns the address of a variable, so that the
library function can access the parameters by reference, allowing parameter values to be altered.
Two other options are included, STACK and WDT. If the option STACK is specified, the library interface
will display the contents of passed parameters on the stack before and after calling the library function.
This allows debugging of parameter passing. By specifying the WDT option, the programmer can specify
whether or not Basic should refresh the watchdog timer. The watchdog timer is a counter, counting
downwards and reaching zero 60ms after it was reset (refreshed). If it is not constantly refreshed in time,
the PC unit assumes that the ASCII unit has crashed and stops operating due to a so-called ‘special unit
error’. Normally, the WDT should be refreshed by from within the library function. Only when the ASCII
unit causes a special unit error on the PLC, the WDT option can be used to determine whether or not the
library function has refreshed the WDT in time. The WDT option should not be used in normal situations,
because the functionality of the WDT is to track if units have crashed. By specifying the WDT option, the
PLC system cannot detect if the library function crashes.