Using Expressions for Object Animation
Expressions can be used to perform logical or mathematical operations on HMI tag values, variables, etc. They are a combination of operands (variables, HMI tags, constants, etc.) that are linked by suitable operators (+ * -). Expressions allow you to compare data with other values or combine them with other values. You can use expressions, for example, to define the condition for animating an object based on the expression result.
An expression always returns a single value after its evaluation. For example, the expression MyVar > 10 evaluates to TRUE, if the value of the variable MyVar is greater 10.
Expressions are allowed anywhere in PLCnext Engineer where a variable or HMI tag is expected. They can be built from:
Operands are logically combined using operators. In an expression, the operator with the highest priority is executed first, followed by the operator with the next lower priority (see below for the evaluation order).
Expressions with reference to multi-element variable
Expressions can contain references to multi-element variables (arrays and structures). The data elements of an array are accessed by specifying the element number in square brackets following the variable name of the array. The data elements of a structure element are accessed by specifying the variable name of the structure followed by a dot and the name of the structure component in square brackets.
Examples
Expression with reference to array element: | graph[1] > 10
(Reference to element number 1 of the array graph. The expression evaluates to TRUE if the value of the array element 1 is greater 10.) |
Expression with reference to structure element: | Machine.[x_pos] < 5
(Reference to structure component x_pos of the structure Machine. The expression evaluates to TRUE if the value of the structure component is less than 5.) |
Partial access of specific parts of ANY_BIT variables used in expressions
The access to specific parts of an ANY_BIT variable (BYTE, WORD, DWORD, LWORD) included in expressions is supported. The partial access to a variable is programmed by adding a dot (.) to the variable name followed by the optional '%' sign, the size prefix and an integer number (from 0 to max) for the address within the variable.
Example: MyByteVar.%X3
Operators
The following operators can be used in an expression:
Arithmetic operators
Arithmetic operators perform a mathematical operation on numbers (constants or variables) and return a numeric value as result. The following arithmetic operators are supported:
Symbol | Operator |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division (see also the description below this table) |
MOD | Modulo (remainder) |
** | Exponent |
Rounding rules for integer divisions
The arithmetic operations only do signed floating point operations. There is no integer division, thus 3 divided by 2 yields 1.5. The data type resulting from a division ('/') or exponent ('**') expression is always 'LREAL'.
In case of a text dynamic, you can round the result by selecting the value '0 digits after decimal' from the 'Format' list box as shown in the following figure. If you leave the 'Format' option as blank, or allow for more digits to be shown, then you can see more accurately the result of the calculation.
Relational operators
Relational operators compare the values of two numeric values and return TRUE (1) or FALSE (0) as result. The following relational operators are supported:
Symbol | Operator |
= | Equal to |
<> | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
Logical operators
Logical operators combine Boolean values and return TRUE or FALSE as result. The following logical operators are supported:
Symbol | Operator |
NOT | Logical negation NOT |
AND | Logical conjunction AND |
OR | Logical disjunction OR |
XOR | Logical XOR (exclusive OR) |
Evaluation order of operators
Operators in parentheses are evaluated first. If no parentheses are used, the operators are evaluated based on the operator in the following order:
Symbol | Evaluation order |
( ) | 1 (highest priority) |
-, NOT | 2 |
** | 3 |
*, /, MOD | 4 |
+, - | 5 |
<, >, <=, >= | 6 |
=, <> | 7 |
AND | 8 |
XOR | 9 |
OR | 10 (lowest priority) |
Using string functions in expressions
PLCnext Engineer provides a number of built in functions that you can use in expressions for examining string values. String functions perform an operation on the string values passed as parameters and return a string or numeric value.
For each of the provided string functions, the strings passed as parameters may be an expression of the type STRING, a user-defined string type, or a parameter of type ANY_STRING. Numeric values passed as parameters may be any expression of the type ANY_NUM.
All string functions can be nested within each other. This means, the functions can be used as parameters of other functions. For example:
substring(TestString, indexOf(TestString,'_'))
returns the substring in TestString beginning at the position of the fist occurrence of the character "_" within TestString. If, for example, TestString contains the value 'abcd_12345', the substring '12345' is returned.
Application example
In the following example we use the substr() function to extract the number '12345' from the string 'FBName_12345' which is the initial value of the 'MyExampleFB' variable. The extracted number (string value) is used as name of a button inside the 'Details' page.
Figure (1) shows the declaration of the 'MyExampleFB' variable in the Data List of the controller.
Figure (2) shows the configuration of the button on the HMI page. The button has a 'Text' dynamic with the expression substr(MyExampleFB, 7) assigned to the 'Variable' property. The expression returns the characters from position 7 to the end of the string (the first position in the string is 0).
At runtime, the expression is evaluated and the result is displayed as button name (see Figure (3)).
Figure (1):
Figure (2):
Figure (3):
The following string functions are available:
concat()
Description | Combines two or more strings passed as parameters (up to 80) into one continuous string and returns the result. |
Syntax | concat(<text1>, <text2>, ..., <textN>)
Where:
<text1> ... <textN> are string values. |
Return value | Returns the string concatenated by the string values. |
Example | concat(TestString1, TestString2)
Where:
TestString1 contains "abc" and TestString2 contains "de"Result: "abcde" |
indexOf()
Description | Returns the first occurrence of the specified search value within the specified string. The string is searched from the beginning to the end. The first position in the string is 0. The optional numeric start value sets the position to start the search. If the search value is not found, the value -1 is returned. |
Syntax | indexOf(<string>, <searchvalue>, <start>)
Where:
<string> is the string to be searched.
<searchvalue> is the string value to search for.
<start> is the optional parameter that indicates the position to start the search. If omitted, the entire string is searched. |
Return value | Returns a numeric value. |
Example | indexOf(TestString1, TestString2)
Where:
TestString1 contains "abcde" and TestString2 contains "cd" Result: 2 (as the string value "cd" starts at position 2) |
lastIndexOf()
Description | Returns the last occurrence of the specified search value within the specified string. The string is searched from the end to the beginning. The first position in the string is 0. The optional numeric start value sets the position to start the search. If the search value is not found, the value -1 is returned. |
Syntax | lastIndexOf(<string>, <searchvalue>, <start>)
Where:
<string> is the string to be searched.
<searchvalue> is the string value to search for.
<start> is the optional parameter that indicates the position to start the search. If omitted, the entire string is searched. |
Return value | Returns a numeric value. |
Example | lastIndexOf(TestString1, TestString2)
Where:
TestString1 contains "abcdabcd" and TestString2 contains "b" Result: 5 (as the last index of the string value "b" starts at position 5) |
left()
Description | Returns the specified number of characters from the beginning of the string. If the number is longer than the length of
the string, the entire string is returned. If the number is less than zero, an empty string is returned. |
Syntax | left(<string>, <number>)
Where:
<string> is the string to extract characters from.
<number> is the number of characters to be extracted from the string. |
Return value | Returns a string value. |
Example | left(TestString, 3)
Where:
TestString contains "abcdabcd" Result: "abc" |
right()
Description | Returns the specified number of characters from the end of the string. If the number is longer than the length of
the string, the entire string is returned. If the number is less than zero, an empty string is returned. |
Syntax | right(<string>, <number>)
Where:
<string> is the string to extract characters from.
<number> is the number of characters to be extracted from the string. |
Return value | Returns a string value. |
Example | right(TestString, 3)
Where:
TestString contains "abcdabcd" Result: "bcd" |
substr()
Description | Returns a part of a string starting at the specified start position. The first position in the string is 0. The optional numeric length value sets the number of characters to extract. If the length value is omitted, the rest of the string is extracted. |
Syntax | substr(<string>, <start>, <length>)
Where:
<string> is the string to extract characters from.
<start> is the position from where to start the extraction. If the value is greater than or equal to the length of the string, an empty string is returned.
<length> is the optional parameter that indicates the number of characters to be extracted from the string. |
Return value | Returns a string value. |
Example | substr(TestString, 3, 2)
Where:
TestString contains "abcdabcd" Result: "da" |
substring()
Description | Returns a part of a string between the specified start and end positions. The first position in the string is 0. The optional numeric end value sets the position (up to, but not including) where to end the extraction. If the end value is omitted, the whole string is extracted. |
Syntax | substring(<string>, <start>, <end>)
Where:
<string> is the string to extract characters from.
<start> is the position from where to start the extraction.
<end> is the optional parameter that indicates the position (up to, but not including) where to end the extraction. |
Return value | Returns a string value. |
Example | substring(TestString, 2, 5)
Where:
TestString contains "abcdabcd" Result: "cda" |