Assignments in ST

Syntax
name_1 := name_2;
name_1 := literal;
reference_variable_1 := REF(name) | NULL;
reference_variable_1 := reference_variable_2;
interface_variable_1 := function_block_instance_1;
interface_variable_1 := interface_variable_2;
interface_variable_1 := NULL; 

Meaning

→assignment to →variables or →function block instances or assignment of return value
An assignment is possible, if the expressions on both sides of the assignment operator ":=" are based on the same data type or on data types that allows an implicit conversion according to the IEC-standard – if these data types are a derived data type or an elementary data type. If the data types are array data types, the expressions must be based on the same data type. In case of a structured data type, the expressions must be based on identical data types (see under " Accessing the structured data type and structure elements ").

In principle, assignments are possible within declaration of a →program, of a →function block, of a →function or of a →method. The assignment of a return value is only possible within a function or a method.

The expression on the right side of the assignment operator ":=" may be one of the following constructs (depending on the expression on the left side):

  • a declared variable (e.g. an internal variable, an output variable of a function block instance, a STRING variable), an array element or an element of a structured data type
    Examples: var2 , MyFunctionBlock.OUT, var3[1] or MyDataType.Elem1

  • a function block instance
    If you assign a function block instance to another function block instance, the instance data of the function block are assigned to the other function block instance.

  • a call of a function, e.g. MyFunction(); or a call of a method
    If the call contains an assignment to the input EN, the value for EN is decisive whether the call – and the assignment itself – is conducted. Here the explanation with examples:

    Code

    Meaning

    x1:=MyFunction(EN := FALSE);

    Because of EN : = FALSE , MyFunction is not called and there is no assignment to x1 .

    x2:=MyFunction(EN := TRUE);

    Because of EN := TRUE , the assignment to x2 is conducted, using the value of MyFunction .

    x3:=MyFunction(EN := myBoolVariable1);x4:=MyFunction(EN := myBoolVariable2);

    If myBoolVariable1 := FALSE, MyFunction is not called and there is no assignment to x3.
    If myBoolVariable2 := TRUE, the assignment to x4 is conducted, using the value of MyFunction.

  • a Boolean →literal

    Possible Boolean literals

    Equivalents for TRUE"

    Equivalents for "FALSE"

    BOOL#TRUE

    BOOL#FALSE

    BOOL#1

    BOOL#0

    TRUE

    FALSE

    1

    0

  • a →bit string literal

    Examples for bit string literals

    Description

    Examples

    integer

    0, 123_4, +986

    base 2 (binary literal)

    2#1111_1111 (255 decimal), 2#1110_0000 (224 decimal)

    base 8 (octal literal)

    8#377 (255 decimal), 8#340 (224 decimal)

    base 16 (hexadecimal literal)

    16#FF or 16#ff (255 decimal), 16#E0 or 16#e0 (224 decimal)

    above possibilities as typed literal of type BYTE, WORD, DWORD or LWORD
    (if the value is positioned within the range of values for the specific type)

    WORD#986, WORD#16#FF (255 decimal)

    BOOL is a bit string data type. But there are specific →Boolean literals.

  • an →integer literal

    Examples for integer literals

    Description

    Examples

    integer

    -12, 0, 123_4, +986

    base 2 (binary literal)

    2#1111_1111 (255 decimal), 2#1110_0000 (224 decimal)

    base 8 (octal literal)

    8#377 (255 decimal), 8#340 (224 decimal)

    base 16 (hexadecimal literal)

    16#FF or 16#ff (255 decimal), 16#E0 or 16#e0 (224 decimal)

    above possibilities as typed literal of type SINT, INT, DINT, LINT, USINT, UINT, UDINT, ULINT
    (if the value is positioned within the range of values for the specific type)

    INT#-12, INT#16#FF (255 decimal)

  • a →real literal of data type REAL or LREAL

    Examples for real literals

    Description

    Examples

    Real literal

    0, 0.0, 0.4560, 3.14159_26

    Real literal with exponent

    -1.34E-12, -1.34e-12
    1.0E+6, 1.0e+6, 1E+6
    1.234E6, 1.234e6

    above possibilities as typed literal of type REAL or LREAL
    (if the value is positioned within the range of values for the specific type)

    REAL#0.0, REAL#1.0E+6

  • a →time literal

    Examples for duration literals

    You must enter duration data, such as days (d), hours (h), minutes (m), seconds (s) and fractions of a second (ms for milliseconds, us for microseconds, ns for nanoseconds) or any combinations of them, as shown in the following table. You can separate the duration units by underscore character. Moreover, you can enter the units in upper- or lowercase letters, for example s or S for seconds.

    Description

    Examples

    Duration literals without underscore

    Short prefix

    T#14ms, T#14.7s, T#14.7m, T#14.7h, T#14.7d, T#14h12m, t#5d14h12m18s3.5ms

    Long prefix

    TIME#14ms, time#14h12m

    Duration literals with underscore

    Short prefix

    T#14h_12m, t#5d_14h_12m_18s_3.5ms

    Long prefix

    TIME#14h_12m, time#5d_14h_12m_18s_3.5ms

    Observe that your used →target system might influence the usage of duration literals in logi.CAD 3 (see "Properties and restrictions specific to the target system").

    Examples for date and time of day literals

    In case of date and time literal and time of day literal, the fractions of a second (entered after the .) are optional.

    Description

    Examples

    Date literal

    Short prefix

    D#1984-06-25

    Long prefix

    DATE#1984-06-25

    Date and time literal

    Short prefix

    DT#1984-06-25-15:36:55.360_227_400

    Long prefix

    DATE_AND_TIME#1984-06-25-15:36:55.360227400

    Time of day literal

    Short prefix

    TOD#15:36:55.36

    Long prefix

    TIME_OF_DAY#15:36:55.36

    Observe that your used →target system might influence the usage of these literals in logi.CAD 3 (see "Properties and restrictions specific to the target system").

  • a →character string literal

    Examples for character string literals

    Single-byte character strings consist of zero or more characters prefixed and terminated by the single quote character '.
    In contrast, logi.CAD 3 does not support double-byte character strings enclosed in the double quote character " for character string literals.

    Single-byte character strings are:

    Description

    Examples

    STRING literal

    'OK', 'ABCDEF'

    typed STRING literal

    STRING#'OK', STRING#'ABCDEF'

    CHAR literal

    'B', 'T'

    typed CHAR literal

    CHAR#'B', CHAR#'T'

    Three-character combination of the dollar sign ($) followed by two hexadecimal digits are interpreted in logi.CAD 3 as the corresponding character of the ANSI C character set.

    This enables you to enter special characters, such as umlauts, for single-byte character string. Currently, such special characters are displayed in logi.CAD 3 (e.g. in the der Values of Variable view) as they have been entered.
    Here a selection of special characters (a list of special characters and the corresponding hexadecimal digits is listed under https://www.ascii-code.com/):

    Decimal value

    Hexadecimal digit

    To enter

    Special character

    128

    80

    $80

    163

    A3

    $A3

    £

    169

    A9

    $A9

    ©

    174

    AE

    $AE

    ®

    177

    B1

    $B1

    ±

    178

    B2

    $B2

    ² (superscript "2")

    179

    B3

    $B3

    ³ (superscript "3")

    188

    BC

    $BC

    ¼

    189

    BD

    $BD

    ½

    190

    BE

    $BE

    ¾

    196

    C4

    $C4

    Ä

    214

    D6

    $D6

    Ö

    220

    DC

    $DC

    Ü

    223

    DF

    $DF

    ß

    228

    E4

    $E4

    ä

    246

    F6

    $F6

    ö

    252

    FC

    $FC

    ü

    If the character combination '$00' is assigned to a single character of a STRING variable, the character string of the STRING variable is terminated at this position. Such character strings are terminated in case of $00 in the Values of Variables view as well.
    However, the complete character string is copied in assignments. This makes it possible to have the Values of Variables view display single characters behind $00, if you access the assigned characters behind $00.

    Moreover, the following specifications are possible for single-byte character strings:

    Specification

    Meaning

    ''

    empty string

    ' '

    string with space character

    '$''

    string with single quote character '

    '"'

    string with double quote character "

    Moreover, the following specifications are possible for two-character combinations in single-byte character strings:

    Specification

    Meaning

    $'

    string with single quote character '

    $$

    string with dollar sign $

    $L oder $l

    string with line feed

    $N oder $n

    string with newline

    $P oder $p

    string with form feed (page)

    $R oder $r

    string with carriage return

    $T oder $t

    string with tabulator

  • an expression (consisting of operands in ST and operators in ST)
    Expressions without a concrete data type are typed. See "Typing of expressions" for explanations on this.

  • if the assignment is done to a declared →reference variable: REF(name) or NULL or another reference variable of the same data type
    Details: see under "Declaration of reference variables (incl. assignments to them)"

  • if the assignment is done to a declared variable that is based on an →interface: a function block instance implementing the same/derived interface, another variable based on the same/derived interface or NULL
    Details: see under "Declaration of variables based on an interface"

Example
PROGRAM TestAssignments
VAR
var1, var2, var3, var5, var7, var8 : INT;
var4 : BOOL;
var6 : TIME;
Inst1 : MyFB1;
Inst2 : MyFB1;
END_VAR
var1 := var2; (* The variable 'var2' is assigned to the variable 'var1'. *)
Inst1 := Inst2; (* The instance 'Inst2' is assigned to the instance 'Inst1'. *)
var3 := Simple1(); (* The function 'Simple1' is called and its value is assigned to the variable 'var3'. *)
var4 := TRUE; (* This line and the next ones illustrate the assignment of a literal to a variable. *)
var5 := 12;
var6 := T#3ms;
var7 := 5 * 3; (* This line and the next one illustrate the assignment of an expression to a variable. *)
var8 := var2 + 20;
END_PROGRAM
 
FUNCTION_BLOCK MyFB1
END_FUNCTION_BLOCK

Assigning return value within a function

If a return value is assigned within a function, you must define the data type of the return value for the function as well. Similar is valid for a method.

Example for function with return value
FUNCTION Simple1 : INT (* starting the declaration, naming data type INT *)
VAR
var1 : INT;
END_VAR
Simple1 := 1; (* assigning return value "1" *)
END_FUNCTION (* concluding the declaration *)