Professional Documents
Culture Documents
Section 8 Structured Text (ST)
Section 8 Structured Text (ST)
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
247
Creating an ST program
Creating an ST program
ST programs are created in the Project tree.
Project tree > Select the target position in the project tree
> Edit > Insert above, Insert below or Insert next level
> ST program from "Object selection"
> Specify a program name, with a short comment if necessary
Every new ST program has a program body: PROGRAM Program name
END_PROGRAM. The program comment is empty. The creation date is entered as the
version number. The editing state is implausible. The program name is already
preset as the name in the program list (PL). The short comment in the program list is
adopted and can be modified.
248
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
State line
Menu bar
Text area
State line
to001us.bmp
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
249
Syntax colouring
All input in the ST editor is text-oriented. Certain important words in the text are
colour-highlighted for emphasis. These are:
Comments
Keywords
Numeric constants
Numeric constants are defined in the program flow.
Symbolic constants
Symbolic constants are defined in a CONST END_CONST block.
Strings
1.
250
These include data types such as SINT, for example, that are not supported.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
to002us.bmp
Select colour
The colour for the selected object can be chosen. The current colour
is marked.
Reset all
The colours for all objects are reset to the default colours.
Reset
The colour for the selected object is reset to the default colour.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
251
Tabulator width
Options > Tabulator width
> Enter the desired tabulator width.
to003us.bmp
The program code already created is not changed by the alteration in tabulator
width. The new tabulator width is used for all subsequent editing steps.
Display program information
to004us.bmp
Program state
The state line shows the name of the program currently being edited, the editing
position and mode and the current user.
252
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Editing position
(4,1) - Shows the current position of the cursor (row and column).
Editing mode
(INS) - Insert
(OVR) - Overwrite
Special symbols and words control the flow in an ST program and these must not be
used as identifiers in the program. Each special symbol has a particular meaning. ST
interprets the following characters as special symbols:
+-*/&=<>[].,():;@#$
In addition, combinations of special symbols are used as operators and/or
delimiters:
:= =>
Assignment operators
Relational operators
**
Exponentiation operator
..
Subareas
(* *)
There is no case distinction for reserved words. ST reserves the following words,
which are all printed in boldface in this manual:
AND
ARRAY
BY
CASE
CONST(1)
DO
ELSE
ELSIF
FUNCTION_BLOCK(2)
IF
MOD
NOT
OF
OR
PROGRAM
REPEAT
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
253
END_CASE
END_CONST(1)
END_IF
END_FOR
END_FUNCTION_BLOCK(2)
END_PROGRAM
END_REPEAT
END_VAR
END_WHILE
EXIT
FALSE
FOR
RETURN
THEN
TO
TRUE
UNTIL
VAR
VAR_EXTERNAL
VAR_INPUT(2)
VAR_OUTPUT(2)
WHILE
XOR
All standard data types are also treated as keywords. Names of function block inputs
and outputs that are identical with keywords start with an underline character to
distinguish them from the keywords, e.g.:
pin_dt(_DT => dt1);
Identifier
All types, variables, constants, functions, function blocks and arrays are identified
by identifiers. Identifiers within an ST program are words with
Valid identifier
Valid identifier
Syntax error
Real number
Syntax error
Case is significant in identifiers. Identifiers must not include any special symbols
(see Special symbols and reserved words on page 253). Therefore a dash (which
254
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
could also be a minus sign) must not occur in a identifier. There is no limit to the
length of identifiers within ST programs.
The following identifiers are already defined for standard data types:
BOOL
BYTE
DINT
DT
DWORD
INT
REAL
STR8
STR16
STR32
STR64
STR128
STR256
TIME
UDINT
UINT
WORD
There is no distinction between upper and lower case in standard data types.
Identifiers of global variables must also obey the rules for variable names. See
Structure of variable list on page 28. Identifiers for function blocks also follow the
rules for tag names. See Structure of Tag List on page 62. Unlike other
programming languages such as ladder diagram and function block diagram, tag
names consisting only of numeric characters are not allowed in ST programs.
Constant
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
255
256
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Types
The purpose of comments is to clarify the program code. They are not taken into
account when the code is generated. Comments are enclosed in brackets with a star:
(* This is a comment *)
There is no limit to the length of a program line. A program line may include more
than one statement.
Types
Every declaration of a variable must give the type of the variable. The type defines
the value range of the variable and determines the operations that can be executed
with it. See also Overview of simple data types on page 26.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
257
Types
Simple types
Bitfield types
Bitfield types define bit fields of differing length. Bitfield types BYTE, WORD and
DWORD can be used in ST programs.
Operations on two bit fields via a binary operator (i.e. bit-by-bit AND, bit-by-bit OR
etc.) are only possible if the types are the same.
Boolean types
Boolean types can only take on the predefined values FALSE or TRUE, Where
FALSE = 0 and TRUE = 1.
REAL type
The real type is a subset of the real numbers. A real value n has three components.
Here m*2e = n, where m and e are whole numbers.
Strings
A string is a fixed-length sequence of characters. The following string types can be
used in ST programs: STR8, STR16, STR32, STR64, STR128 and STR256. Strings
are stored in ASCII code.
258
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Types
Structured types
A structured type, identified by the type of structure, contains more than one value.
All defined structured data types can be used in ST programs. For a definition of
structured data types, see Structured data types on page 47.
The components of structured types can be accessed with
variable name.component name .
Arrays
Arrays have a set number of components of a single type. Simple and structured
types are both permissible data types. The validity range of the data type for the
array index is defined as DINT (-2 147 483 648 .. +2 147 483 647). The number of
elements of each dimension is determined by the data type of the array index. The
start and end of the definition of the range must be within the range of validity of the
array index.
Arrays are defined by the keyword ARRAY, the range and the data type, e.g.
ARRAY [0..100] OF REAL;
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
259
Variables must be declared before use. The variable declaration states the type of
variable. All standard data types and structured data types that have already been
defined can be used. The variable name and data type are separated by a colon, e.g.
VAR
x, y: INT;
END_VAR
Global variables are also valid outside the ST program. All variables defined in the
variable list can be used as global variables in ST programs. New variables can also
be created in an ST program. After declaration in the ST program, new variables
also have to be entered in the variable list, i.e. they must be instantiated. Instantiate
on page 285 gives further details. Global variables are declared within the keywords
VAR_EXTERNAL END_VAR, e.g.
VAR_EXTERNAL
TIC1379_PV: REAL;
TIC1379_MODE: INT;
TCP_Data_IN01: structTCP12;
(* structTCP12 is a structured data type *)
END_VAR
It is not possible to assign the data type to a list of global variables. Each global
variable must be declared individually.
I/O components of hardware objects cannot be declared directly. The hardware
object has to be declared as a function block in the ST program.
Local variables
Local variables are only valid within the ST program. The names of local variables
only need to be unique within the ST program. The same data type can be assigned
260
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
10]
11]
10]
11]
10]
11]
:=
:=
:=
:=
:=
:=
1;
2;
3;
4;
5;
6;
Initial values are assigned to the variables when the program is loaded.
System variables
Inputs and outputs can be defined for user-defined function blocks. They are defined
in the interface editor of the user-defined function block and the definition is
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
261
Inputs and outputs can only be edited in the interface editor of the user-defined
function block.
Keywords must not be used for the names of inputs and outputs.
The names of inputs and outputs should not exceed 3 characters. Longer names are
permissible. The long name (>3 characters) must be used in the class definition. If
instances of this function block are used in ST programs the long name is truncated
to the first 3 characters.
Function blocks
Like variables, function blocks must be declared before they are used in an ST
program. The names of function blocks must be unique throughout the project, i.e.
they may only be called up once in the project. In ST programs all standard function
blocks and plausible user-defined function blocks can be used (see also Limits of
the system on page 275). Local variables and function blocks can be declared in the
same block.
All function blocks defined in the tag list can be used in ST programs. New function
blocks can also be created directly in an ST program. After declaration in the ST
program, new function blocks also have to be entered in the tag list, i.e. they must be
instantiated. Instantiate on page 285 gives further details. Function blocks are
declared within the keywords VAR END_VAR, e.g.
VAR
TI1379: AI_TR;
TIC1379: C_CU;
TY1379: AO_TR;
262
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Expressions
TI1379_LIN: LIN2;
(* LIN2 is a user-defined function block type *)
m: INT;
END_VAR
It is not possible to assign the function block type to a list of function blocks. Every
function block must be declared individually.
To access the I/O components of hardware objects, the hardware object must be
declared as a function block in the ST program in the section
VAR_EXTERNAL END_VAR , e.g.
VAR_EXTERNAL
DAI02_1_0_1: DAI02;
END_VAR
Expressions
Syntax of expressions
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
263
Expressions
Operators
Symbol
Order of
precedence
(expression)
highest
exponentiation
**
negation
complement
NOT
multiplication
division
modulo
MOD
addition
subtraction
comparison
equality
inequality
<>
Boolean AND
&
Boolean AND
AND
Boolean exclusive or
XOR
Boolean OR
OR
lowest
264
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Expressions
b
c
c
*
c
c
*
:= 2; c := 3;
(*
(*
c
(*
* d
(*
+ d
(*
(c + d)
(*
d := 4;
Result =
Result =
Result =
Result =
Result =
Result =
5 *)
7 *)
9 *)
14 *)
11 *)
21 *)
Function calls
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
265
Statements
VAR
i: INT;
END_VAR
i := add(to_in(CstVal), 42);
Statements
Statements are all constructs that declare an action that can be carried out by the
process station. Statements must end with a semicolon.
Simple statements
:=
Expression
Assignments replace the value of a variable with a new value that is given by an
expression. This expression can include identifiers of functions that are thereby
activated and supply the corresponding values.
An assignment must consist of a variable on the left, followed by the assignment
operator :=, followed by the expression to be evaluated. The statement
A := B;
replaces the value of the variable A by the value of the variable B. Both variables A
and B must be of the same data type. The following are valid assignments:
x :=
Done
m :=
a :=
y + z;
:= (i >= 1) AND (i<100);
3.0 + SIN(n);
feld[i, j].content;
The assignment of arrays is not possible. The following assignment leads to a check
error:
VAR
Arr1 [1..10] OF BOOL;
Arr2 [1..10] OF BOOL;
END_VAR
266
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Statements
Arr2 := Arr1
Global variables can be accessed directly in statements, or via the process image. As
in other program editors, an @ must be placed in front of variable names to access
the process image:
A := SQRT(B);
@A := SQRT(@B);
(* direct access *)
(* access via process image *)
Constants in expressions are always calculated internally with the data type DINT.
If constants with data type UDINT are appropriate, an explicit type change must be
provided, e.g.
VAR
L_Int: UDINT;
END_VAR
L_Int := 10;
L_Int := L_Int * TO_UD(256345984);
Function blocks are called by an statement consisting of the name of the function
block followed by a list of value assignments in brackets.
Tag name
:=
Input
Value
=>
Output
Variable
);
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
267
Statements
The sequence of input and output assignments is not significant. When the function
block is called, assignments only have to be supplied to and retrieved from the
mandatory pins. Inputs are supplied by := and outputs are retrieved by =>, e.g.
VAR
LI347_LIN: LIN;
END_VAR
VAR_EXTERNAL
LI347_CH0: REAL;
LI347: REAL;
END_VAR
LI347_LIN (IN:= LI347_CH0, OUT=> LI347);
Each function block may only be used in one assignment in the ST program.
Optional function block pins can also be accessed outside the function block
assignment with
function block name.pin name .
VAR
FIC251: C_CU;
RATIO, BIAS: REAL;
END_VAR
VAR_EXTERNAL
FIC251_PV: REAL;
FIC251_OUT: REAL;
TIC251_OUT: REAL;
TRD3_ASP: REAL;
END_VAR
(* supplying inputs *)
FIC251.SP := TIC251_OUT * RATIO + BIAS;
(* calling the function block *)
FIC251(PV:= FIC251_PV, OUT=> FIC251_OUT);
(* sending outputs *)
TRD3_ASP := FIC251.ASP;
Conditional statements
268
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Statements
IF statement
Expression
THEN
Statement
ELSE
Statement
END_IF;
The result of the expression must be of the data type BOOL. If the result is TRUE,
the part following THEN is carried out, if it is not, the part following ELSE is carried
out. ELSE is optional. If this branch is not available, the IF statement has no effect,
i.e. it carries nothing out whatever. A couple of examples:
IF y<>0.0 THEN
z := x / y;
ELSE
z := 3.4e38;
END_IF;
IF p <> 0 THEN
a := SIN(b + c);
END_IF;
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
269
Statements
It is therefore specified that for each definition, ELSE refers to the most recent IF.
The statement shown above is therefore interpreted as follows:
IF e1 THEN
( IF e2 THEN
s1:=1;
ELSE
s1:=0;
END_IF; )
END_IF
CASE statement
A CASE consists of an expression (the selector) and a list of branches, which can be
of any length. Each of these branches is preceded by one or more constants or the
keyword ELSE. The selector must be an integer data type.
CASE
OF
Expression
Constant
..
Constant
Constant
ELSE
Statement
Statement
END_CASE;
Constants must not be defined more than once and must also conform to an integer
data type that is compatible with the selector type. A branch that is preceded by a
constant is carried out if the value of the constant is equal to that of the selector. The
270
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Statements
same applies if a range includes the value of the selector. If the value of the selector
does not agree with either a constant or a range, the branch following ELSE is
carried out. If no ELSE branch is defined, the program continues with the next
statement following the CASE statement.
A few examples:
CONST
plus:= 1;
minus:= 2;
times:= 3;
END_CONST
CASE operator OF
plus: a := b + c;
minus: a := b - c;
times: a := a * b;
END_CASE;
CASE state OF
0: display_text := O.K.;
1,5: display_text := Excessive temperature;
2 .. 4: display_text := Torque;
7: display_text := No feedback;
6, 8 .. 10: display_text := No auxiliary power;
ELSE display_text := Unknown error;
END_CASE;
Loops
Loops specify parts of programs that are repeated. If the number of repetitions is
known in advance, use of the FOR statement is recommended. If not, WHILE or
REPEAT should be used.
FOR statement
The FOR statement carries out a loop in which new values are assigned to a variable
(the controlled variable). The controlled variable must be an integer data type.
The definition of a loop with FOR includes identification of a starting and ending
value and a step size. The starting value, step size and ending value are expressions.
These expressions must be of an integer data type that is compatible with the type of
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
271
Statements
the controlled variables. Identification of the step size with BY is optional. If the step
size is not explicitly stated the loop is carried out with step size 1.
At the start of the loop the controlled variable is set to the starting value and each
time the loop is run through it changes by the step size until the ending value is
reached or exceeded. The expressions for the step size and ending value are
calculated once at the start of the loop and stored as constants for subsequent use in
the loop.
Every time the loop is run through the statement in the body of the loop is carried
out once.
FOR
:=
Controlled variable
End value
Statement
BY
Start value
Step size
TO
DO
END_FOR;
After the loop is completed the controlled variable has the value
ending value + step size.
This value must not exceed the maximum value that is defined by the data type of
the controlled variable.
A couple of examples:
Maximum := MAX_VAL;
FOR lw := 2 TO 63 DO
IF Data[lw] > Maximum THEN
Maximum := Data[lw];
MaxIdx := lw;
END_IF;
END_FOR;
Sum := 0;
FOR i := 10 TO 1 BY -1 DO
FOR j := 1 TO 2 DO
IF FLag THEN EXIT; END_IF;
Sum := Sum + j;
272
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Statements
END_FOR;
Sum := Sum + i;
END_FOR;
If the ending condition of a loop is already satisfied before it runs the first time (i.e.
ending value < starting value), the loop and all the statements it contains are
skipped. The following loop is skipped:
FOR lw := 2 TO 1 DO
s1:=5;
END_FOR;
REPEAT statement
Statement
UNTIL
Expression
END_REPEAT;
The statement is carried out repeatedly until the expression takes on the value TRUE.
The statement is carried out at least once because the expression is not evaluated
until UNTIL is reached. A couple of examples of the REPEAT statement:
j := -4;
REPEAT
j := j + 2;
UNTIL j > 60
END_REPEAT;
REPEAT
a := in1 + in2;
b := 2 * in1;
c := in1 * in2;
UNTIL EndCondition
END_REPEAT;
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
273
Statements
WHILE statement
The WHILE statement contains an expression whose truth value determines whether
or not the statement that follows DO is to be carried out again. The result of the
expression must be of the data type BOOL.
WHILE
Expression
DO
Statement
END_WHILE;
The expression is evaluated each time before the part of the program to be repeated
is carried out. If the value FALSE is returned before the statement is carried out for
the first time, the block that follows DO is not carried out at all. Otherwise it is
repeated until the expression takes on the value FALSE.
A couple of examples:
WHILE i > 0 DO
i := i - in1;
lw := lw+ 1;
END_WHILE;
i := StartValue;
WHILE Data[i] <> x DO
i := i + 1;
END_WHILE;
Index := i;
The essential difference between WHILE and REPEAT is that WHILE repeats a loop
for as long as the expression returns TRUE. REPEAT repeats the loop until the
expression takes on the value TRUE. Unlike WHILE, a REPEAT statement is run
through at least once.
Control statements
The statement EXIT offers the option of exiting a loop before the ending condition
is reached. If the EXIT statement is inside nested loops, only the loop level
concerned is exited with EXIT, e.g.
WHILE i > 0 DO
lw := lw+ 1;
IF lw > MAX_LW THEN
EXIT;
274
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
END_IF;
i := i - in1;
END_WHILE;
After the EXIT statement, the program continues with the statement that follows
END_WHILE.
The statement RETURN causes an ST program to be prematurely abandoned.
RETURN interrupts the processing of the current program and control of the program
flow is transferred to the next level up. That can be:
The number of local elements is limited to 65526 for each ST program. Local
elements are the local variables, every element of a structured variable and every
individual array element and intermediate store within the program.
Programming of loops
Every ST program is processed in the context of a user task. In other words, the ST
program is carried out once in the space of the task cycle time.
When loops are used, a part of the ST program, the statements within the loops, is
run through many times in a task cycle. This leads to increased runtime for the user
task. The time spent executing a loop should not exceed 5 ms.
When programming loops, there is a risk of creating endless loops. The following
loop is not ended:
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
275
REPEAT
i := i +1;
sEnd := FALSE;
UNTIL sEnd
END_REPEAT;
This loop is repeated continuously with the result that none of the other programs in
the user task can be processed.
Memory occupancy
Structured text is a high-level language that is translated into machine code by code
generation. The complex statements mean that the machine code produced is
substantially longer than the source text.
In multidimensional arrays the number of individual elements increases very
rapidly. The array
ARRAY 1 .. 100, 1 .. 100] OF REAL
276
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Examples
Examples
Simple control loop
The following example shows the programming of a simple control loop in ST. The
I/O signals are accessed using component names. The external set point is set as a
function of the global variable TIC2106_AUTO, which also determines the
operating mode. The global variables and I/O components are accessed via the
process image.
PROGRAM control loop
CONST
(* Definition of constants *)
MAN_SP := 20.0;
END_CONST
VAR
(* declaration of function blocks *)
FI2104: AI_TR;
FIC2104: C_CU;
FY2104: AO_TR;
(* declaration of local variables *)
rLocalVar1, rLocalVar2: REAL;
END_VAR
VAR_EXTERNAL
(* declaration of HW objects for I/O access *)
DAI01_2_0_3: DAI01;
DAO02_2_0_4: DAO01;
(* declaration of global variables *)
FIC2104_SETP: REAL;
TIC2106_AUTO: BOOL;
FIC2104_ALM: BOOL;
END_VAR
(* call input transformation *)
FI2104(IN:= @DAI01_2_0_3.Ch3, OUT=> rLocalVar1);
(* assign external set point *)
IF @TIC2106_AUTO THEN
FIC2104.SP := @FIC2104_SETP;
ELSE
FIC2104.SP := MAN_SP;
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
277
Examples
END_IF;
(* assign operating mode *)
FIC2104.MA := @TIC2106_AUTO;
FIC2104.MM := NOT(@TIC2106_AUTO);
(* call control function blocks *)
FIC2104(PV:= rLocalVar1, OUT=> rLocalVar2);
(* further use of outputs *)
@FIC2104_ALM := FIC2104.SL1 OR FIC2104.SL2 OR FIC2104.SL3;
(* call output transformation *)
FY2104(IN:= rLocalVar2, OUT=> @DAO02_2_0_4.Ch2);
END_PROGRAM
This ST program carries out the same functionality as the following FBD program:
to005.bmp
Linearization
278
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
13.0,
32.6,
47.0,
62.0,
78.4,
83.5,
92.0,
100.0,
Index : INT;
END_VAR
VAR_EXTERNAL
(* declaration
IN: REAL;
OUT: REAL;
END_VAR
Examples
24.4,
28.4,
16.0,
58.0,
82.0,
85.0,
63.7,
100.0;
(*
(*
(*
(*
(*
(*
(*
(*
X3, Y3 *)
X4, Y4 *)
X5, Y5 *)
X6, Y6 *)
X7, Y7 *)
X8, Y8 *)
X9, Y9 *)
X10, Y10 *)
of global variables *)
(* start of linearization *)
FOR Index:=1 TO 10 DO
IF IN <= Curve[Index,1] THEN
(* range found*)
IF Index = 1 THEN
(* low limit *)
OUT := Curve[Index,2];
EXIT;
(* exit FOR loop *)
END_IF;
(* calculate output from straight line equation *)
(* Y := (((Y2 - Y1) / (X2 - X1)) * (X - X1)) + Y1; *)
OUT := (((Curve[Index,2] - Curve[Index-1,2]) /
(Curve[Index,1] - Curve[Index-1,1])) *
(IN -Curve[Index-1,1])) + Curve[Index-1,2];
EXIT;
(* exit FOR loop *)
ELSE
IF Index = 10 THEN
(* high limit *)
OUT := Curve[Index,2];
EXIT;
END_IF;
END_IF;
END_FOR;
END_PROGRAM
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
279
Examples
In its running time the user-defined function block MIN_MAX determines the
maximum and the minimum of the sampled input signal IN. The maximum and
minimum can be reset via the RES input.
FUNCTION_BLOCK MIN_MAX
(* the following declarations originate from the interface- *)
(* definition of the user-defined function block *)
(* and cannot be altered in the ST program. *)
VAR_INPUT
IN : REAL;
RES : BOOL;
END_VAR
VAR_OUTPUT
MAX : REAL;
MIN : REAL;
END_VAR
VAR (* VAR_DPS *)
END_VAR
VAR (* PARA_DPS *)
END_VAR
(* PARA_VIS
ClassName : TEXT;
TagName : TEXT;
ShortText : TEXT;
LongText : TEXT;
SelState : BOOL;
END_VAR *)
(* End of interface definition *)
(* declarations *)
CONST
MAX_REAL := 1.0e38;
END_CONST
VAR
intMax : REAL := -MAX_REAL;
intMin : REAL := MAX_REAL;
END_VAR
(* program *)
280
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Editing an ST program
IF RES THEN
(* RES input is set *)
MAX := 0.0;
MIN := 0.0;
intMax := -MAX_REAL;
intMin := MAX_REAL;
ELSE
(* normal function *)
intMax := Max(IN, intMax);
intMin := Min(IN, intMin);
MAX := intMax;
MIN := intMin;
END_IF;
END_FUNCTION_BLOCK
Editing an ST program
Inserting ST elements
To simplify the processing of ST programs, ST elements with the basic syntax can
be inserted directly into the text.
ST Elements > Insert ST syntax
An IF statement is inserted into the ST program in the following form:
IF Expression THEN
ThenStatementList;
ELSE
ElseStatementList;
END_IF;
For the following ST elements the syntax can be inserted into the program:
VAR
VAR_EXTERNAL
CONST
Date & Time
Time
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
281
IF
CASE
FOR
WHILE
REPEAT
RETURN
EXIT
Assignment
Within an ST program, variables that have already been defined can be inserted
directly. If the cursor is in the VAR_EXTERNAL END_VAR block a variable
declaration is inserted. The variable name is inserted in the program code for
subsequent use in the program:
ST Elements > Select global variable or F2
> select an existing variable in the project from the list
New variables can also be created directly in an ST program:
Position the cursor in the VAR_EXTERNAL END_VAR block
> ST Elements > create global variable
The following dialog appears for inserting the variable into the variable list:
282
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
di0133us.bmp
Name
Data type
Identify the data type of the newly defined variable. The standard
data types and all user-defined data types can be selected from the
list.
Resource
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
283
With text declaration, the variable is not yet entered in the list of variables. The
variable still has to be instantiated for this to take place. See also Global variables on
page 260.
Insert function blocks
New function blocks can also be declared as text. The function block type must also
be entered as text, e.g.
VAR
TI203: AI_TR;
END_VAR
With text declaration, the function block is not yet entered in the tag list. It still has
to be instantiated for this to take place. See also Function blocks on page 262.
284
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Instantiate
After variables and function blocks have been textually defined (declared) they still
do not appear in the corresponding lists (variable list and tag list). A further step,
instantiate, is needed for this.
ST Elements > Instantiate
Instantiate links the parameter data for the function blocks to the ST program. After
the function block declaration has been deleted the parameter data is still stored in
the ST program and can be erased by instantiate.
The dialog mask summarizes the objects to be instantiated and/or those no longer
required:
to006us.bmp
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
285
parameter settings for these function blocks are still included in the
ST program and will be deleted when the instantiate is confirmed.
Info-Message
OK
Instantiate is carried out and the parameters for function blocks that
are no longer needed are deleted
There is read access and write access to variables. Variables to the left of the
assignment operator have write access. All applications to the right of the
assignment operator are read-only access, e.g.
var4 := var1 + SIN(var2 * var3);
(* var4 can be written to *)
(* var1, var2 and var3 are read from *)
Multiple write access to the same variable is allowed in an ST program and does not
lead to errors, e.g.
var1 := 5:
var2 := 3 * var1;
var1 := 42;
Process image
286
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
If a variable is read many times in an statement, the same type of access should
be used every time. In the statement
var1 := var2 + SIN(@var2);
inconsistent data supply of var2 may occur, as var2 is read from two different
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
287
END_CONST
VAR
B1, B2: BYTE;
S1, S2: STR8;
END_VAR
B1
(*
S2
(*
In ST programs the data types of functions are automatically adapted to suit the
transferred arguments. There is no need for the data type to be adapted explicitly to
the transferred arguments. If necessary, the data type of the argument can be
changed to suit by conversion functions, e.g.
VAR
i1, i2: INT
END_VAR
(* extract the root from an INT value *)
i2 := to_in(SQRT(to_re(i1)));
The possible data types for a function must be taken from the relevant function
description. See Engineering reference manual, Functions and function blocks.
If constants are transferred exclusively to a polymorphic function (with different
data types) as the argument, at least one explicit type change is needed, e.g.
CONST
CONST_VAL := 12;
END_CONST
VAR
i1: INT;
END_VAR
i1 := add(to_in(CONST_VAL), 42);
288
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
The number of arguments must not exceed the maximum input number. It is not
possible to set the input number explicitly in ST programs.
Certain function blocks need uniform sampling to fulfil their functionality, i.e. they
must be calculated at uniform intervals. Controller blocks are examples of these.
They must not be called within loops.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
289
A distinction is made between mandatory pins and optional pins for block inputs
and outputs. The two types of pin are represented in the same way in ST programs.
User-defined function blocks with identical input and output names cannot be
used in ST programs. See Limits of the system on page 275.
Supply to and retrieval from mandatory pins must take place with the block call. All
mandatory pins can be inserted directly behind the function block call.
Position the cursor on the function block call (tag name)
> ST Elements > insert mandatory parameter
A comma-delimited argument list with all mandatory pins is created, e.g.
TI104(IN:= (*REAL*), OUT=> (*REAL*));
The data type is given for each pin in the form of a comment. Inputs are supplied via
the assignment operator :=. They can be supplied directly with constants. Outputs
are written to variables with =>, e.g.
TI104(IN:= 45.0 (*REAL*), OUT=> out (*REAL*));
Supply to and retrieval from optional pins can take place with the block call or at
any point in the ST program. They can be inserted directly at the function block call.
Position the cursor on the function block call (tag name)
> ST Element > Insert a parameter
> Select input or output from the list
(Inputs and outputs are separated by a horizontal line).
An additional pin is appended to the comma-delimited argument list, e.g.
TI104(IN:= (*REAL*), OUT=> (*REAL*), STA=> (*INT*));
Inputs and outputs that support more than one data type have the comment text
(*Other*), e.g.
VAR
TIR104: TREND;
END_VAR
TIR104(IN1:= (*Other*),IN2:= (*Other*),IN3:= (*Other*));
290
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
In addition to block call, optional pins are supplied via assignments. The pin name is
then appended to the tag name with a dot separator, e.g.
(* Assignment of inputs for a PID controller *)
IF @TIC2106_AUTO THEN
FIC2104.SP := @FIC2104_SETP;
ELSE
FIC2104.SP := MAN_SP;
END_IF;
FIC2104.MA := @TIC2106_AUTO;
FIC2104.MM := NOT(@TIC2106_AUTO);
(* calling the controller block *)
FIC2104(PV:= @FIC2104_PV, OUT=> @FIC2104_OUT);
(* further use of outputs *)
@FIC2104_ALM := FIC2104.SL1 OR FIC2104.SL2 OR FIC2104.SL3;
The names of the function block pins are described in the Engineering reference
manual, Functions and function blocks. If the name of a function block pin is the
same as a keyword (see Special symbols and reserved words on page 253), an
underline must precede the pin name, e.g.
VAR
pin_dt: P_DT;
END_VAR
pin_dt(DAY := 18, MON := 6; YEA:= 2002, _DT=> dt1);
If an input is supplied before the block call and in the block call, the supply in the
block call has precedence, e.g.
VAR
ana_mon: M_ANA;
END_VAR
ana_mon.L1 := 12.0;
ana_mon(IN := In12, L1 := 42.0);
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
291
The limit value L1 in the function block has the value 42.0.
If the value of an output is used before the block call, the initial value or the value
from the most recent calculation cycle is transferred.
Function block pin and parameters
to014us.bmp
VAR
pin_ana: M_ANA;
END_VAR
pin_ana.L1 := Limit1;
pin_ana.L2 := Limit2;
pin_ana(IN := Input);
292
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Inputs that can also be configured as parameters have priority over the parameter
in the ST program.
Negating function block pins.
The values of the Boolean inputs and outputs of a function block can be inverted at
every point of use. The complement operator NOT must be used to do this, e.g.
FIC2104.MM := NOT(@TIC2106_AUTO);
All function blocks have non-negated input and output pins as default.
The function block terminal to be inverted must be of the data type BOOL (i.e.
binary).
Parameter definition of function blocks
In ST programs it is not possible to check function blocks for plausibility from the
parameter mask. Function blocks may only be checked in the context of the ST
program.
ST program > Check
Activation from the error list takes place as in other program editors.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
293
The interface definition of the user-defined function block (see Interface editor on
page 377) is adopted as a non-editable (grey) block in the ST program. The interface
cannot be modified in the ST program, this must always be done in the interface
editor. All variables defined in the interface that are loaded onto the process station
can be used in the ST program. See also Inputs and outputs on page 261.
294
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
to013.bmp
Function blocks that are used in user-defined function blocks must be declared in a
VAR END_VAR block. This ensures that they always have a name in the class
definition.
In the instance (Zoom to user FB) every embedded function block can be given an
individual name.
> Open parameter mask for the function block
> Enter tag name
This individual tag name is not displayed in the ST program.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
295
to007us.bmp
Bookmarks can be switched on and off. If there is no bookmark in the line the
switch sets it, if there is one it deletes it.
Set the cursor in the desired line
> Edit > Toggle bookmark
or
CTRL + F7
You can jump to a bookmark from any point in the ST program. Thereafter it jumps
to the next bookmark, and so on:
Jump forwards (towards the end of the program):
Edit > Goto next bookmark
or
F7
296
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Breakpoints
Breakpoints
Breakpoints are used for tracing faults in programs. See Section 11, Debugger.
Breakpoints are displayed as a brown circle in the mark column.
to008us.bmp
Breakpoints can be switched on and off. If there is no breakpoint in the line the
switch sets it, if there is one it deletes it.
Position the cursor on the desired line
> Edit > Toggle breakpoint
or
F9
Breakpoints that have been set can be disabled and enabled
Position the cursor on the desired line > context menu
> Disable breakpoint / Enable breakpoint
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
297
to007us.bmp
Find
The term entered is searched for in the ST program. Every find term
is saved in a list and can be recalled from the list later.
Replace
Replaces the find term with the term that has been entered. Every
replacement term is saved in a list and can be recalled from the list
later. If the field is empty, the find term is deleted.
298
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Goto line
Down
The find proceeds from the current position towards the end of
the program
Find next
Replace
The find term is replaced with the term in the Replace field.
Replace all
Close window
Ends the search and closes the Find & Replace dialog.
Goto line
In ST programs it is possible to jump to any line
Edit > Goto line
The current line is offered when the dialog is called.
to007us.bmp
Line number
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
299
Block operations
Block operations
Select program elements
to011.bmp
Inside a line, characters are marked. Otherwise complete lines are marked. After
marking, the desired operation can now be carried out. Example: > Edit > Cut.
Extending an area of marked text
Press the SHIFT key and hold it down > to select more characters or lines
The additional characters or lines are included in the marked area.
Deselect program elements
300
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Block operations
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
301
Block operations
same ST program the parameter data is retained but when the block is pasted into a
different one it loses its parameter data.
Paste
The following methods of pasting areas of text that have previously been copied or
cut are available:
Edit > Paste
or
CTRL + V
The copied or cut area of text is inserted at the current cursor position.
Write file
A marked area of text can be written to a file for exchange with other projects and
other editors.
Edit > Write file
A Unicode text file with the file type .TXT, which can be edited in any Unicodeenabled text editor, is created. The parameter data associated with function blocks is
not written to the text file. The file thus created can be used for program exchange
with other ST editors.
Read file
An ST program that has been created with another text editor can be read into the
ST editor.
Edit > Read file
The text file to be read in can be in Unicode or ASCII format. The file format is
automatically recognized when the file is read in. The contents of the text file are
inserted at the current cursor position.
302
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Export block
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
303
Adding blocks
Options > Define user menu
> Select block in right-hand window
> Add or double-click
The selected function block or the separating lines to Group blocks together are
added one at a time to the end of their own block menu.
di0128us.bmp
The left-hand list in the window shows all the blocks that are entered in this menu.
The right-hand list shows all the blocks that are available in the block libraries.
Horizontal lines can be inserted into the user menu to visually separate groups of
blocks.
Sorting blocks
Options > Define user menu> Select block in left-hand window
> Position the mouse between two block entries > single click
Deleting blocks
Options > Define user menu> Select block in left-hand list
> Delete
A block is removed from its own block menu
304
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Cross references
Cross references
Cross references can be selected directly from the ST editor:
Select a variable, I/O component or tag
Cross reference
or
F5
The window offers a number of sorting and filtering options and the window setting
is stored.
For tags, unlike variables, there is no definition of read or write access.
Show program
For a variable:
Call a program with prior selection of these variables, or call the
module with prior selection of the I/O component.
For a tag:
Call the program with prior selection of this tag, or call the module
in the hardware structure.
Show declaration
For a tag, the tag list is called, for a variable the variable list is
called. If an I/O component is used directly in the program, the I/O
editor of this component is opened.
Filter
A filter enables only those variables to be displayed for which readonly access or write-only access exists in the programs concerned.
After activation it is possible to branch to the programs that are listed as crossreferences. Activating the menu item Back! in the branched program returns the
user to the original editor.
Display next / previous cross reference
Cross reference > Find next or Find previous
The next or previous place where the selected variable is used within the current
program is displayed.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
305
306
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Back!
This leaves the ST program and calls the application from which the program editor
was last called (back one stage only).
Exit ST program
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
307
The tag list contains all the tags specified in the system.
If tag names that were given to function block calls in the ST program are deleted
from the tag list, then after returning from the tag list the default parameters for
the function block are set and the function block is no longer instantiated.
Create hard copy
308
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Project tree > Select program to be copied > Edit > Copy or
CTRL+C
> Select position to which program is to be copied
> Edit > Paste or CTRL+V
> Select above, below or at, depending on position chosen
> Provide program name.
The program is copied and assigned to a program list in the project under a new
unique name. The program is copied in its current configuration, including the
program header and comment. The function block tag names are copied with the
addition of a serial number. The function blocks are automatically instantiated, i.e.
the new tag names are entered in the tag list. Copying an ST program has no effect
on the declaration of variables. The copied program is identified as implausible. Its
version number is the date and time of the copying process.
Connecting programs
Programs are connected via variables or with the input and/or output boards.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
309
tl027us.bmp
The individual function blocks can be selected and parameterized. Operating modes
can also be modified from the commissioning mode.
On going through the text of the ST program the currently calculated values are
displayed.
If the text is found in the variable list or the local variables, the current value of the
variables is displayed. If global and local variables have the same name, the value of
the local variables is displayed.
If the text is a tag name, the processing state of the function block is displayed. The
value of function block pins cannot be displayed in the ST program.
310
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
Error tracing
As with other program editors, the value window and the trend window can be used
in ST programs. Local variables can be entered in both windows.
To support error tracing, ST programs have an additional window, the watch
window, for online data.
Select variable
> Window > Add value to watch window
The values in the watch window are not updated on each cycle. See also Section 11,
Debugger, Watch window on page 430.
Error tracing
The debugger can be used for ease of error tracing in ST programs. The debugger is
described in Section 11, Debugger.
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming
311
Error tracing
312
3BDD012504R0501_Eng_Manual_IEC61131-3_Programming