• No results found

3.6 Value assignments and expressions

3.6.1 Value assignments

3.6.1.1 Syntax of the value assignment

A value assignment is used to assign the value of an expression to a variable. The previous value is overwritten. Before a value can be correctly assigned, a variable must be declared in the declaration section (see Syntax of variable declaration (Page 105)).

As shown in the following syntax diagram, the expression is evaluated on the right side of the assignment sign :=. The result is stored in the variable, whose name is on the left side of the assignment sign (target variable). All target variables supported from a formal viewpoint are shown in the figure.

([SUHVVLRQ 9DULDEOHRIWKH

HOHPHQWDU\GDWDW\SH

9DULDEOHRIWKH

HQXPHUDWRUGDWDW\SH

$UUD\YDULDEOH

6WUXFWXUHGYDULDEOH

$EVROXWH3,DFFHVV

([WHUQDOWDJ

'LUHFWELWDFFHVV

$FFHVVWR)%LQSXWSDUDPHWHUV 2XWSXWVRQO\

9DOXHDVVLJQPHQW XQIRUPDWWHG

!2QO\IRUDFWLYDWHG3HUPLWODQJXDJHH[WHQVLRQVFRPSLOHURSWLRQ

 

!

!

Figure 3-19 Syntax: Value assignments

The following contains explanations and examples for the left side of the value assignment:

● Value assignments with variables of an elementary data type (Page 114) ,

● Value assignments with variables of the derived enumerator data type (Page 117)

● Value assignments with absolute PI access (to addresses of the process image), see:

Absolute access to the fixed process image of the BackgroundTask (absolute PI access) (Page 221).

How the right side of a value assignment, i.e. an expression, is formed, is described in Expressions (Page 119).

3.6.1.2 Value assignments with variables of an elementary data type

An expression with an elementary data type (Page 90) can be assigned to a variable when one of the following conditions is fulfilled:

● Expression and target variable have the same data type.

Note the following information on the STRING data type (Page 114).

● The data type of the expression can be implicitly converted to the data type of the target variable (see Conversion of elementary data types (Page 141) and Functions for the conversion of numerical data types and bit data types in the SIMOTION Basic Functions Function Manual).

Examples

elemVar := 3*3;

elemVar := elemVar1;

See also

Value assignments with variables of a bit data type (Page 116)

3.6.1.3 Value assignments with variables of the STRING elementary data type

Assignments between variables of the STRING data type

There are no restrictions to assignments between variables of the STRING data type (character strings) that have been declared with different lengths. If the declared length of the target variable is shorter than the current length of the assigned character string, the character string is truncated to the length of the target variable.

Exception: The following applies for an in/out assignment (parameter transfer to an in/out parameter): The declared length of the assigned variable (actual parameter) must be greater than or equal to the declared length of the target variable (formal in/out parameter). See Parameter transfer to in/out parameters (Page 154).

See also Elementary data types (Page 90):

Examples:

Access to elements of a string

The individual elements of a string can be addressed in the same way as the elements of an array [1..n]. These elements are converted implicitly to the elementary data type BYTE. In this way assignments between string elements and variables of the BYTE data type are possible.

Examples:

byteVar := string20[5];

string20[10] := byteVar;

The following special cases have to be taken into account:

1. When assigning a variable of the BYTE data type to a string element (e.g. stringVar[n:] := byteVar):

– The string element to which the value is to be assigned lies outside of the declared length of the string:

The string remains unchanged, TSI#ERRNO is set to 1.

– The string element to which the value is to be assigned lies outside of the assigned length of the string (n > LEN(stringVar)), but within the declared length:

The length of the string is adjusted, the string elements between LEN(stringvar) and n are set to $00.

2. When assigning a string element to a variable of the BYTE data type (byteVar := stringVar[n:]):

– The string element to which the variable is to be assigned lies outside of the assigned length of the string (n > LEN(stringVar)):

The variable is set to 16#00, TSI#ERRNO to 2.

Editing strings

Various system functions are available for the editing of strings, such as the joining of strings, replacement and extraction of characters, see SIMOTION Basic Functions Function Manual.

Converting between numbers and strings

Various system functions are available for the conversion between variables of numeric data types and strings, see Elementary data type conversion (Page 141) and the SIMOTION Basic Functions Function Manual.

3.6.1.4 Value assignments with variables of a bit data type

Access to individual bits of a bit data type variable

You can also access the individual bits of a variable of data type BYTE, WORD or DWORD:

● With standard functions (see SIMOTION Basic Functions Function Manual):

You can read, write or invert any bit of a bit string with the functions _getBit, _setBit and _toggleBit.

You can specify the number of the bit via a variable.

● With direct bit access:

You can define the bit of the variable that you want to access as a constant, via a separate point behind the variable.

You can only specify the number of the bit via a constant.

To be able to use this option, you must activate the compiler option "Permit language extensions" (see Global compiler settings (Page 45) and Local compiler

settings (Page 46)).

&RQVWDQW 'DWDW\SH$1<B,17 6LPSOHYDULDEOH

$UUD\YDULDEOH

6WUXFWXUHGYDULDEOH

([WHUQDOWDJ

$FFHVVWR)%RXWSXWSDUDPHWHUV

$FFHVVWR)%LQSXWSDUDPHWHUV 'LUHFWELWDFFHVV IRUPDWWHG

3HUPLWWHGGDWDW\SHV

HDFK%<7(:25'':25'

2QO\IRUDFWLYDWHG3HUPLWODQJXDJHH[WHQVLRQVFRPSLOHURSWLRQ



Figure 3-20 Syntax: Direct bit access

Table 3-26 Example of direct bit access

// Only with compiler option "Permit language extensions"

FUNCTION f : VOID VAR CONSTANT

BIT_7 : INT := 7;

END_VAR VAR

dw : DWORD;

b: BOOL;

END_VAR

b := dw.BIT_7; // Access to bit 7 b := dw.3; // Access to bit 3 // b := dw.33; // Compilation error;

// Bit 33 not permitted.

END_FUNCTION

NOTICE

The access to bits of an I/O variable or system variable can be interrupted by other tasks.

There is therefore no guarantee of consistency.

Editing variables of the bit data types You can:

1. Combine several variables of the same data type into one variable of a higher-level data type (e.g. two variables of the BYTE data type into one of the WORD data type). Various system functions are available for this, e.g. WORD_FROM_2BYTE.

2. Split one variable into several variables of a lower-level data type (e.g. one variable of the DWORD data type into four of the BYTE data type). Various system functions are

available for this, e.g. DWORD_TO_4BYTE.

3. Rotate or shift the bits within a variable. The bit sting standard functions ROL, ROR, SHL and SHR are available for this.

These system functions and system function blocks are described in the SIMOTION Basic Functions Function Manual.

Logic operators

Variables of the bit data types can be combined with logic operators, see Logic expressions and bit-serial expressions (Page 127).

3.6.1.5 Value assignments with variables of the derived enumerator data type

Each expression and each variable of the derived enumerator data type (see also: Derived

3.6.1.6 Value assignments with variables of the derived ARRAY data type

An array consists of several dimensions and array elements, all of the same type (see also:

Derived data type ARRAY (Page 97)).

There are various ways to assign arrays to variables. You can assign complete arrays, individual elements, or parts of arrays:

● A complete array can be assigned to another array if both the data types of the

components and the array limits (the smallest and largest possible array indices) are the same. Valid assignments are:

array_1 := array_2;

● An individual array element is addressed by the array name followed by the index value in square brackets. An index must be an arithmetic expression of the data type SINT, USINT, INT, UINT or DINT.

elem1 := array [i];

array_1 [2] := array_2 [5];

array [j] := 14;

● A value assignment for a valid subarray can be obtained by omitting a pair of square brackets for each dimension of the array, starting at the right. This addresses a partial area of the array whose number of dimensions is equal to the number of remaining indices (see example below).

Consequently, you can reference rows and individual components within a matrix but not closed columns (closed in the sense of from...to). Valid assignments are:

matrix1[i] := matrix2[k];

array1 := matrix2 [k];

3.6.1.7 Value assignments with variables of the derived STRUCT data type

Variables of a user-defined data type that contain STRUCT data type specifications are called structured variables (see also Derived data type STRUCT (structure) (Page 100)).

They can either represent a complete structure or a component of this structure.

Valid parameters for a structure variable are:

struct1 //Identifier for a structure

struct1.elem1 //Identifier for a structure component struct1.array1 //Identifier of a simple array

//within a structure

struct1.array1[5] //Identifier of an array component

There are two ways to assign structures to variables. You can reference complete structures or structure components:

● A complete structure can only be assigned to another structure if the data type and the name of both structure components match.

A valid assignment is:

struct1 := struct2;

● You can assign a type-compatible variable, a type-compatible expression or another structure component to each structure component.

Valid assignments are:

struct1.elem1 := Var1;

struct1.elem1 := 20;

struct1.elem1 := struct2.elem1;

struct1.array1 := struct2.array1;

struct1.array1[10] := 100;

Note

You also use structured variables in the FBInstanceName.OutputParameter format, e.g.

myCircle.circumference to access the output variables of a function block, i.e. the result of the function block. For more information about function blocks, see explanations in Defining functions (Page 148) and Defining function blocks (Page 149).

A further application of structured variables is to access TO variables and the variables of the basic system.

3.6.2 Expressions

An expression represents a value that is calculated when the program is compiled or

executed. It consists of operands (e.g. constants, variables or function values) and operators (e.g. *, /, +, -).

The data types of the operands and the operators involved determine the expression type.

ST uses the following types of expression:

● Arithmetic expressions

● Relational expressions

● Logic expressions

3.6.2.1 Result of an expression

The result of an expression can be:

● Assigned to a variable

● Used as a condition for a control statement

● Used as a parameter for a function or function block call.

Note

Expressions containing only the following elements can be used for variable initialization and index specification in ARRAY declarations (for initialization expressions – see Figure Syntax: Constant expression in Initialization of variables or data types (Page 107)):

• Constants

• Basic arithmetic operations

• Logic and relational operations

• Bit string standard functions

3.6.2.2 Interpretation order of an expression

The interpretation order of an expression depends on the following:

● The priority of the operators used,

● The left-to-right rule,

● The use of parentheses (for operators of the same priority).

Expressions are processed according to specific rules:

● Operators are executed according to priority (see table in Priority of operators (Page 129)).

● Operators of the same priority are executed from left to right.

● A minus symbol in front of an identifier denotes multiplication by -1.

● An arithmetic operator cannot be followed immediately by another.

The expression a * -b is therefore invalid, but a * (-b ) is allowed.

● Parentheses override the operator priority order, i.e. parentheses have the highest priority.

● Expressions in parentheses are treated as individual operands and are always evaluated first.

● The number of opening parentheses must equal the number of closing parentheses.

● Arithmetic operations cannot be used on characters or logic data. For this reason, expressions such as (n<=0) + (n<0) are invalid.

3.6.3 Operands

Definition

Operands are objects which can be used to formulate expressions. Operands can be represented by the syntax diagram:

9DULDEOHRIWKHHOHPHQWDU\GDWDW\SH

9DULDEOHRIWKHHQXPHUDWRUGDWDW\SH

$UUD\YDULDEOH

6WUXFWXUHGYDULDEOH

$EVROXWH3,DFFHVV ,QSXWVDQGRXWSXWV

&RQVWDQW

)&FDOO

$FFHVVWR)%RXWSXWSDUDPHWHUV

$FFHVVWR)%LQSXWSDUDPHWHUV

'LUHFWELWDFFHVV ([WHUQDOWDJ 2SHUDQG XQIRUPDWWHG

!2QO\IRUDFWLYDWHG3HUPLWODQJXDJHH[WHQVLRQVFRPSLOHURSWLRQ

!

!

Figure 3-21 Syntax: Operand

Table 3-28 Examples of operands intVar

5

%I4.0 PI