ST
(Structured Text) is a text-based high-level language for programming automation systems. Simple standard constructs enable fast and efficient programming. ST
uses many traditional qualities of high level languages, including variables, operators, functions, and elements for controlling the program flow.
What is Structured Text?
No other programming language can replace ST
. Every programming language has its advantages and disadvantages. The main advantage of ST
is that complex mathematical calculations can be programmed easily.
Structured Text is characterized by the following features:
ST
has been designed to work with the other PLC programming languages. ie: a ladder logic program can call a Structured Text
subroutine.
i := 0;
REPEAT
i := i + 1;
UNTIL i > 10;
END_REPEAT;
In IEC Editor variable declaration and program’s body are defined separately. You don’t have to write
PROGRAM <name>
andEND_PROGRAM
but only the source code.
When defining variables the declaration type must be specified. It is the scope of the variables. The VAR_INPUT
, VAR_OUTPUT
and VAR_IN_OUT
declarations are used for variables that are passed as arguments to the program or function.
Declaration | Description |
---|---|
VAR |
The general local variable declaration |
VAR_INPUT |
Defines a variable list for a function |
VAR_OUTPUT |
Defines output variables from a function |
VAR_IN_OUT |
Defines variable that are both inputs and outputs from a function |
VAR_TEMP |
Temporary variables |
VAR_GLOBAL |
Global variables defined in configuration |
END_VAR |
Marks the end of a variable declaration |
Table 3.1 - Variable scope declarations
The variable type keywords may be supplemented by RETAIN
keyword (ie: VAR_INPUT RETAIN
).
<Identifier> { AT <address> } : <type> { := <initialization> };
The parts in braces { } are optional.
The identifier
is the name of a variable. The items listed in the following each case must be regarded when defining an identifier.
It must not contain spaces or special characters.
It is not case-sensitive, which means that ie: VAR1
, Var1
and var1
are all the same variable.
The underscore character is recognized in identifiers (ie: A_BCD
and AB_CD
are considered two different identifiers
).
The length of the identifier
as well as the meaningful part of it, are unlimited.
The rules listed in the following text box concerning multiple use must be regarded
An identifier
must not be duplicate locally.
An identifier
must not be identical to any keyword.
Globally an identifier
can be used plurally, thus a local variable can have the same name as a global one. Within a POU in this case the local variable will have priority.
The variable can directly be linked to a definite address using the keyword AT <address>
.
A valid Data Type
, optionally extended by a := <initialization>
.
You can use standard data types, user-defined data types or instances of function blocks when programming. Each identifier is assigned to a Data Type
which dictates how much memory space will be reserved and what type of values it stores.
Standard IEC61131-3 data types are supported by Sirius ES - IEC EDITOR. See here the following.
BOOL
type variables may be given the values TRUE
(1
) and FALSE
(0
).
See below a list of all available INTEGER
data types. Each of the different number types covers a different range of values. The following range limitations apply to the INTEGER
data types:
Data Type | Lower value | Upper Value | Bit used |
---|---|---|---|
BYTE |
0 | 255 | 8 |
WORD |
0 | 65535 | 16 |
DWORD |
0 | 4294967295 | 32 |
LWORD |
0 | 264-1 | 64 |
SINT |
-128 | 127 | 8 |
USINT |
0 | 255 | 8 |
INT |
-32768 | 32767 | 16 |
UINT |
0 | 65535 | 16 |
DINT |
-2147483648 | 2147483647 | 32 |
UDINT |
0 | 4294967295 | 32 |
LINT |
-263 | 263-1 | 64 |
ULINT |
0 | 264-1 | 64 |
Table 3.2 INTEGER data types
As a result when larger types are converted to smaller types, information may be lost.
The data types REAL
and LREAL
are so-called floating-point types. They are required to represent rational numbers. 32 bits of memory space is reserved for REAL
and 64 bits for LREAL
.
Data Type | Lower value | Upper Value | Bit used |
---|---|---|---|
REAL |
1.401e-45 | 3.403e+38 | 32 |
LREAL |
2.2250738585072014e-308 | 1.7976931348623158e+308 | 64 |
Table 3.3 REAL - LREAL data types
A STRING
Data Type
variable can contain any string of characters. The size entry in the declaration determines the memory space to be reserved for the variable. It refers to the number of characters in the string and can be placed in parentheses or square brackets. If no size specification is given, the default size of 80 characters will be used.
The string length basically is not limited, but string functions only can process strings of 1 - 255 characters! If a variable is initialized with a string too long for the variables data-type, the string will be correspondingly cut from right to left.
In Sirius ES - IEC EDITOR size specification is not supported.
WSTRING
Data Type
is an extension to the IEC 61131-3 standard.
It differs from the standard STRING
type (ASCII) by getting interpreted in Unicode format and needing 2 bytes for each character and 2 bytes extra memory space (each only 1 in case of a STRING).
The data types TIME
, TIME_OF_DAY
(shortened TOD
), DATE
, and DATE_AND_TIME
(shortened DT
) are handled internally like DWORD
. Time is given in milliseconds in TIME
and TOD
. Time in TOD
begins at 12:00 A.M. Time is given in seconds in DATE
and DT
beginning with January 1, 1970 at 12:00 A.M.
TIME
constants are generally used to operate the standard timer modules. The time constant TIME
, has size of 32 Bit and matching the IEC 61131-3 standard.
Syntax for TIME
constant:
t#<time declaration>
Instead of t#
also the following can be used T#
, time
, TIME
.
TIME
declaration can include the following time units. These must be used in the following sequence, but it is not required to use all of them.
d
- daysh
- hoursm
- minutess
- secondsms
- millisecondsTIME1 := t#14ms;
Additionally to the standard data types the user can define special data types within a project.
These definitions are possible via creating New Data Type
objects in the Project
menu Data Type
node.
User Defined data types can be:
In Sirius ES - IEC Editor data types are not declared in text mode but with specific forms for each type.
Sirius ES - IEC Editor supports all IEC operators. Operators are used in a POU like functions.
ST
provides basic arithmetic operations for your operation:
Symbol | Arithmetic Operation | Example |
---|---|---|
:= |
Assignment | a := b; |
+ |
Addition | a := b + c; |
- |
Subtraction | a := b - c; |
* |
Multiplication | a := b * c; |
/ |
Division | a := 10 / 3; |
MOD |
Modulo (display division reminder) | a := 10 mod 3 |
Table 3.3 Arithmetic operators
The Data Type
is a very important factor. Note the following table:
Syntax | Data types | Result |
---|---|---|
r := 8 / 3 |
INT := INT / INT |
r = 2 |
r := 8 / 3 |
REAL := INT / INT |
r = 2.0 |
r := 8.0 / 3 |
REAL := REAL / INT |
r = 2.6667 |
r := 8.0 / 3 |
INT := REAL / INT |
ERROR* |
Table 3.4 Type casting
*ERROR: Compiler error message: Type mismatch: Cannot convert REAL to INT
. You can see the that the result is dependent of the syntax as well as the data types used.
In high level languages like ST
, simple constructs can be used to compare variables. These return either the value TRUE or FALSE.
Symbol | Logical comparison expression | Example |
---|---|---|
= |
Equal to | IF (a = b) THEN |
<> |
Not equal to | IF (a <> b) THEN |
> |
Greater than | IF (a > b) THEN |
>= |
Greater or equal to | IF (a >= b) THEN |
< |
Less than | IF (a < b) THEN |
<= |
Less or equal to | IF (a <= b) THEN |
Table 3.5 Comparison operators
The Data Type
is a very important factor. Note the following table:
The following boolean operators are available, matching the IEC1131-3 standard:
Allowed types: BOOL
, BYTE
, WORD
, DWORD
, LWORD
.
Symbol | Description | Example |
---|---|---|
AND |
Bitwise AND of bit operands. If the input bits each are 1, then the resulting bit will be 1, otherwise 0. |
var1 := 16#4D AND 16#7F; |
OR |
Bitwise OR of bit operands. If at least one of the input bits is 1, the resulting bit will be 1, otherwise 0. |
Var1 := 16#4D OR 16#7F; |
XOR |
Bitwise XOR operation of bit operands. If only one of the input bits is 1, the result bit will be 1; if both or none are 1, the resulting bit will be 0. |
Var1 := 16#4D XOR 16#7F; |
NOT |
Bitwise NOT operation of a bit operand. The resulting bit will be 1, if the corresponding input bit is 0 and vice versa. |
Var1 := NOT 16#7F; |
Table 3.6 Boolean operators
The following bit-shift operators, matching the IEC1131-e standard, are available:
Allowed data types: BYTE
, WORD
, DWORD
, LWORD
.
Symbol | Description |
---|---|
SHL |
Bitwise left-shift of an operand.r := SHL(in, n); in operand to be shifted to the left.n number of bits, by which in gets shifted to the left. |
SHR |
Bitwise right-shift of an operandr := SHR(in, n); in operand to be shifted to the right.n number of bits, by which in gets shifted to the right. |
ROL |
Bitwise rotation of an operand to the left.r := ROL(in, n); in will be shifted one bit position to the left n times while the bit that is furthest to the left will be reinserted from the right. |
ROR |
Bitwise rotation of an operand to the right.r := ROR(in, n); in will be shifted one bit position to the right n times while the bit that is furthest to the left will be reinserted from the left. |
Table 3.7 Bit-shift operators
All selection operations can also be performed with variables.
Symbol | Description | Example |
---|---|---|
SEL |
Binary Selection.OUT := SEL(G, IN0, IN1) G determines whether IN0 or IN1 is assigned to OUT . |
Var1 := SEL(TRUE, 3, 4); Result is 4 |
MAX |
Maximum function.OUT := MAX(IN0, IN1) Returns the greater of the two values. |
Var1 := MAX(30, 40); Result is 40 |
MIN |
Minimum function.OUT := MIN(IN0, IN1) Returns the lesser of the two values. |
Var1 := MIN(90, 30); Result is 30 |
LIMIT |
LimitingOUT := LIMIT(Min, IN, Max) |
Var1 := LIMIT(30, 90, 80); Result is 80 |
MUX |
MultiplexerOUT := MUX(K, IN0,...,INn) |
Var1:=MUX(1, 30, 40, 50); Result is 30 |
Table 3.8 Selection operators
An expression is a construct that returns a value after it has been evaluated. Expressions are composed of operators and operands. An operand can be a constant, a variable, a function call or another expression.
a := b + c;
x := (a - b + c) / 2;
result := SIN(a) * COS(b);
The assignment of a value to a variable through a result of an expression or a value. The assignment consists of a variable on the left side, which is designated to the result of a calculation on the right side by the assignment operator :=
. All assignments must be closed with a semicolon ;
.
a := 10;
b := a;
c := a * 2;
When the code line has been processed, the value of variable Var1
is twice as big as the value of variable Var2
.
Comments are sometimes left out, but are nevertheless an important component of the source code. They describe the code and make it more easy to read. Comments make it possible for you or others to read a program easily, even long after it was written. They are not compiled and have no influence over the execution of the program. Comments must be placed between a pair of parenthesis and asterix (* Comment *)
.
(* this is a comment line *)
(*
this is
a
multiple
line
comment
*)
The use of several operators in one line brings up the question of priority (order of execution). The execution is determined by priority. Expressions are executed starting with the operator of highest priority, followed by the next highest, and so on until the expression has been completely executed. Operators with the same priority are executed from left to right as they appear in the expression.
Operator | Symbol - Syntax | Priority |
---|---|---|
Parentheses | ( ... ) |
Highest |
Function call | FUCTION(X, Y) |
|
Exponent | ** |
|
Negation | NOT |
|
Multiplication Division Modulo |
* / MOD |
|
Addition Subtraction |
+ - |
|
Comparisons | < , > , <= , >= |
|
Equal to Not equal to |
= <> |
|
Boolean AND | AND |
|
Boolean XOR | XOR |
|
Boolean OR | OR |
Lowest |
Table 3.9 Comparison operators
The order of execution at runtime: see images below.
a := 6 + 7 * 5 - 3; (* Multiplications first, highest priority *)
a := 6 + 35 - 3; (* Then additions, from left to rigth *)
a := 41 - 3; (* Subtraction at the end *)
a := 38; (* Assignment is the final operation *)
Multiplication is executed first, then addition, and finally subtraction. The order of operations can be changed by putting higher priority operations in parentheses. This is shown in the next example.
As shown in the following figure, the use of parentheses influences the execution of the expression.
a := (6 + 7) * (5 - 3); (* Parenthesis first *)
a := 13 * 2; (* Then multiplications *)
a := 26; (* Assignment *)
The expression is executed from left to right. The operations in parentheses are executed first, then the multiplication, since the parentheses have higher priority. You can see that the parentheses lead to a different result.
The IF
statement is used to create decisions in the program.
You are already familiar with the comparison operators, and they can be used here.
There are several types of IF statements.
Decision | Syntax | Description |
---|---|---|
IF THEN |
IF (a > b) THEN |
Comparison |
result := 1; |
Statement(s) | |
ELSIF THEN |
ELSIF (a > c) THEN |
Comparison (optional) |
result := 2; |
Statement(s) | |
ELSE |
ELSE |
Above IF statements are not TRUE (optional) |
result := 3; |
Statement(s) | |
END_IF |
END_IF |
End if decision |
Table 3.10 IF
structure
The IF
statement is tested for the result TRUE
. If the result is FALSE
, the program advances to the line after the END_IF
statement. The function of the IF
statement can be a single comparison, but it can also be multiple comparisons connected by AND
, OR
, etc..
IF (a > b) THEN
c := 0;
END_IF;
IF (a > 0) AND (b > 0) THEN
c := 1;
END_IF;
The ELSE
statement is an extension of the simple IF
statement. Only one ELSE
statement can be used per IF
statement.
IF (a > b) THEN (* A condition *)
c := 0; (* A instruction *)
ELSE
c := 1; (* B instruction *)
END_IF;
If the IF
meets condition A, then the A instruction(s) are executed. If the IF
does not meet condition A, then the B instruction(s) are executed.
One or more ELSE_IF
statements allow you to test a number of conditions without creating a confusing software structure with many simple IF statements.
IF (a > b) THEN
r := 0;
ELSIF (a > c) THEN
r := 1;
ELSIF (a > d) THEN
r := 2;
ELSE
r := 3;
END_IF;
At runtime the decisions are processed from top to bottom. If the result of a decision is TRUE
, the corresponding statements are executed. Then the program continues from after the END_IF
. Only those decisions that corresponding to the first TRUE
decision are executed, even if subsequent decisions are also TRUE
. If none of the IF
or ELSE_IF
decisions is TRUE
, the statement in the ELSE
branch is executed.
A nested IF
statement is tested only if previous condidions have been met. Every IF
requires its own END_IF
so that the order of conditions is correct.
IF (a > b) THEN
IF (c < d) THEN
IF (e = f) THEN
g := 0;
END_IF;
END_IF;
END_IF;
It is helpful to indent every nested IF statement and the corresponding expressions. As many IF statements can be nested as needed. It is possible, however, that the compiler will run out of memory after the 40th level. Also, that kind of extensive nesting is evidence of poor programming style. It becomes nearly impossible to get a clear overview of the code. After three nesting levels, it is better to find another way to structure the program.
The CASE
statement compares a step variable with multiple values. If one of these comparisons is a match, the steps that compare to that step are executed. If none of the comparisons is a match, there is an ELSE branch similar to an IF
statement that is then executed. After the statements have been executed, the program continues from after the END_CASE
statement.
Keywords | Syntax | Description |
---|---|---|
CASE OF |
CASE var OF |
Begin of CASE statement |
1: result := 1; |
for var = 1 |
|
2, 5: result := 1; |
for var = 2 and var = 5 values |
|
6..10 result := 1; |
for var 6 to 10 values |
|
END_CASE |
END_CASE |
End of CASE statement |
Table 3.11 CASE structure
Only one step of the CASE
statement is processed per program cycle.
CASE (a) OF
0:
b := 0;
1..5:
b := 1;
6, 10:
b := 2;
7..9:
b := 3;
AND_CASE;
In many applications, it is necessary for sections of code to be executed multiple times during a cycle. This type of processing is also referred to as a loop. The code in the loop is executed until a defined termination condition is met.
Loops help make programs shorter and easier to follow. Program expandability is also an issue here. Loops can be nested.
Depending on the structure of a program, it is possible for an error in the program to cause the processing gets stuck repeating the loop until the time monitor in the central unit responds with an error. To prevent such endless loops from occurring, one should almost always include in the programming a way for the loop to be aborted after a defined number of repetitions or to run to a certain limit.
ST
offers several types of loops to choose from:
Limited:
Unlimited:
The FOR
statement is used to run a program section for a limited number of repetitions. For all other applications, WHILE
or REPEAT
loops are used.
Keywords | Syntax | Description |
---|---|---|
FOR TO BY DO |
FOR i := startVal TO endVal {BY stepVal} DO |
The step in { } is optional |
(\* do something \*) |
Loop body statement | |
END_FOR |
END_FOR |
End of FOR statement |
Table 3.12 FOR structure
The statements in the FOR
loop are repeated. At every repetition, the loop counter is raised by stepVal
. The two control variables startVal
and endVal
determine the start value and end value of the loop counter. After the end value is reached, the program continues from after the END_FOR
statement. The control variables must both be the same Data Type
and cannot be described by any of the statements in the loop body. The FOR
statement raises or lowers the loop counter until it reaches the end value. The step size is always 1, unless otherwise specified with BY
. The termination condition, the loop counter, is evaluated with every repetition of the loop.
FOR (i := 0) TO 100 BY 2 DO
a := a + 1;
END_FOR;
The WHILE
loop can be used in the same way as the FOR
loop, except that the condition can be any boolean expression. If the condition is met, then the loop is executed. The WHILE
loop is used to repeat statements as long as a particular condition remains TRUE
.
Keywords | Syntax | Description |
---|---|---|
WHILE DO |
WHILE i > endValue DO |
Boolean condition |
(\* do something \*) |
Loop body statement | |
i := i + 1; |
index increment | |
END_WHILE |
END_WHILE |
End of WHILE statement |
Table 3.13 WHILE structure
The instructions are executed repeatedly for as long as the condition returns TRUE
. If the condition returns FALSE
during the first evaluation, the instructions are never executed.
If the condition never assumes the value
FALSE
, the statements are repeated endlessly, resulting in a runtime error.
i := 0;
WHILE (i < 10) DO
a := a + 1;
i := i + 1; (* Index increment *)
END_WHILE;
The REPEAT
loop differs from the WHILE
loop in that the termination condition is only checked once the loop has been executed. This means that the loop runs at least once, regardless of the termination condition.
Keywords | Syntax | Description |
---|---|---|
REPEAT |
REPEAT |
Start loop |
(\* do something \*) |
Loop body statement | |
i := i + 1; |
index increment | |
UNTIL |
UNTIL i < endValue |
Termination condition |
END_REPEAT |
END_REPEAT |
End of REPEAT statement |
Table 3.14 REPEAT structure
The statements are executed repeatedly until the UNTIL
condition is TRUE
. If the UNTIL
condition is true from the beginning, the statements are only executed once.
If the
UNTIL
condition never assumes the valueTRUE
, the statements are repeated endlessly, resulting in a runtime error.
i := 0;
REPEAT
a := a + 1;
i := i - 1; (* index decrement *)
UNTIL (i = 0)
END_REPEAT;
The EXIT
statement can be used with all types of loops before their termination condition occurs.
i := 0;
WHILE (i < 10) DO
IF (a = b) THEN
EXIT;
END_IF;
a := a + 1;
i := i + 1;
END_WHILE;
In ST
a Function call can be used as an operand.
Result := FUNCTION(7) + 3;
The following numeric IEC operators, described by IEC 61131-3 standard, are available
Returns the absolute value of a number.
In and output can be of any numeric basic Data Type
.
r := ABS(-2); (* Result in r is 2 *)
Returns the square root of a number.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := SQRT(16); (* Result in r is 4 *)
Returns the natural logarithm of a number.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := LN(45); (* Result in r is 3.80666 *)
Returns the logarithm of a number in base 10.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := LOG(1000); (* Result in r is 3 *)
Numeric IEC operator: Returns the exponential function.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := EXP(2); (* Result in r is 7.389056099 *)
Numeric IEC operator: Exponentiation of a variable with another variable.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := EXPT(7, 2); (* Result in r is 49 *)
Returns the sine of a number. The value is calculated in arch minutes.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := SIN(0.5); (* Result in r is 0.479426 *)
Numeric IEC operator: Returns the cosine of number. The value is calculated in arch minutes.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := COS(0.5); (* Result in r is 0.877583 *)
Returns the tangent of a number. The value is calculated in arch minutes.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := TAN(0.5); (* Result in r is 0.546302 *)
Returns the arc sine (inverse function of sine) of a number. . The value is calculated in arch minutes.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := ASIN(0.5); (* Result in r is 0.523599 *)
Returns the arc cosine (inverse function of cosine) of a number. The value is calculated in arch minutes.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := ACOS(0.5); (* Result in r is 1.0472 *)
Returns the arc tangent (inverse function of tangent) of a number. The value is calculated in arch minutes.
The input variable can be of any numeric basic Data Type
, the output variable must be type REAL
or LREAL
.
r := ATAN(0.5); (* Result in r is 0.463648 *)
Its is forbidden to implicitly convert from a larger
type to a smaller
type (ie: from INT
to BYTE
or from DINT
to WORD
). Special type conversions are required if one wants to do this. One can basically convert from any elementary type to any other elementary type.
<TYPE1>_TO_<TYPE2>
r := BOOL_TO_INT(boolValue);
Please notice that at
<TYPE1>_TO_STRING
conversions the string is generated left-justified. If it is defined to short, it will be cut from the right side.
IEC Operator: Converting from type BOOL
to any other type.
Syntax:
BOOL_TO_<TYPE>
For number types the result is 1, when the operand is TRUE
and 0 when the operand is FALSE
.
For the STRING
type the result is 'TRUE', when the operand is TRUE
or 'FALSE' when the operand is FALSE
.
IEC Operator: Conversion from another variable type to BOOL
Syntax:
<TYPE>_TO_BOOL
The result is TRUE
when the operand is not equal to 0. The result is FALSE
when the operand is equal to 0.
The result is TRUE
for STRING
type variables when the operand is 'TRUE', otherwise the result is FALSE
.
Conversion from an integral number type to another number type. :
Syntax:
<INT Data Type>_TO_<INT Data Type>
When you perform a type conversion from a larger to a smaller type, you risk losing some information. If the number you are converting exceeds the range limit, the first bytes for the number will be ignored.
si := INT_TO_SINT(4223); (* Result is 127 *)
If you save the
INTEGER
4223 (16#107f represented hexadecimally) as aSINT
variable, it will appear as 127 (16#7f represented hexadecimally).
Conversion from the variable type REAL
or LREAL
to a different type:
The value will be rounded up or down to the nearest whole number and converted into the new variable type. Exceptions to this are the variable types STRING
, BOOL
, REAL
and LREAL
.
If a
REAL
orLREAL
is converted toSINT
,USINT
,INT
,UINT
,DINT
,UDINT
,LINT
orULINT
and the value of the real number is out of the range value of that integer, the result will be undefined and will depend on the target system. Even an exception is possible in this case! In order to get target-independent code, handle any range exceedance by the application. If the real/lreal number is within the integer value range, the conversion will work on all systems in the same way.
When you perform a type conversion from a larger to a smaller type, you risk losing some information.
i := REAL_TO_INT(1.5); (* Result is 2 *)
j := REAL_TO_INT(1.4); (* Result is 1 *)
i := REAL_TO_INT(-1.5); (* Result is -2 *)
j := REAL_TO_INT(-1.4); (* Result is -1 *)
Conversion from the variable type TIME
or TOD
(Time Of Day) to a different type.
Syntax:
<TIME TYPE>_TO_<TYPE>
The time will be stored internally in a DWORD
in milliseconds (beginning with 12:00 A.M.
for the TOD variable). This value will then be converted.
In case of type STRING the result is a time constant.
When you perform a type conversion from a larger to a smaller type, you risk losing some information.
str :=TIME_TO_STRING(T#12ms); (* Result is T#12ms *)
dw := TIME_TO_DWORD(T#5m); (* Result is 300000 *)
si := TOD_TO_SINT(TOD#00:00:00.012); (* Result is 12 *)
Conversion from the variable type DATE or DATE_AND_TIME to a different type.
Syntax:
<DATE TYPE>_TO_<TYPE>
The date will be stored internally in a DWORD
in seconds since Jan. 1, 1970
. This value will then be converted.
For STRING
type variables, the result is the date constant.
When you perform a type conversion from a larger to a smaller type, you risk losing some information.
b := DATE_TO_BOOL(D#1970-01-01); (* Result is FALSE *)
i := DATE_TO_INT(D#1970-01-15); (* Result is 29952 *)
byt := DT_TO_BYTE(DT#1970-01-15-05:05:05); (* Result is 129 *)
str := DT_TO_STRING(DT#1998-02-13-14:20); (* Result is ’DT#1998-02-13-14:20’ *)
Conversion from the variable type STRING
to a different type.
The conversion works according to the standard C compilation mechanism: STRING
to INT
and then INT
to BYTE
. The higher byte will be cut, thus only values of 0-255 result.
Syntax:
STRING_TO_<TYPE>
The operand from the STRING
type variable must contain a value that is valid in the target variable type, otherwise the result will be 0.
When you perform a type conversion from a larger to a smaller type, you risk losing some information.
b := STRING_TO_BOOL(’TRUE’); (* Result is TRUE *)
w := STRING_TO_WORD(’abc34’); (* Result is 0 *)
t := STRING_TO_TIME(’T#127ms’); (* Result is T#127ms *)
bv := STRING:TO_BYTE(’500’); (* Result is 244 *)
Conversion from REAL
to DINT
. The whole number portion of the value will be used.
diVar := TRUNC(1.9); (* Result is 1 *)
diVar := TRUNC(-1.4); (* Result is -1 *)
In ST
a function block is called a function block instance and the necessary transfer parameters are placed in parentheses.
Operations:
Before a function block is called, one must describe the variables that are to be used as input parameters. The code for calling a function block occupies one line. Then the outputs of the function block can be read.
CHECKERROR(errorId := 100, showError := true);
b := CHECKERROR.error;
First the function block name is entered, then the transfer parameters are assigned in parentheses, separated by commas. The code for calling a function block is closed with a semicolon.