Documente Academic
Documente Profesional
Documente Cultură
About expressions
Sometimes the data you gather from devices is meaningful only when you:
compare it to other values.
combine it with other values.
create a cause-effect relationship with other values.
Expressions allow you to create mathematical or logical combinations of data that return
more meaningful values. Depending on the components used in the expression, the value
returned can be in the form of a numeric value, a true/false value, or a text string.
Expression components
Expressions can be built from:
20-1
• • • • •
tag values.
tag placeholders.
constants.
arithmetic, relational, logical, and bitwise operators.
mathematical and security functions.
if-then-else logic.
Tags, arithmetic operators, bitwise operators, and mathematical functions such as SQRT
(square root) return numeric values.
Relational and logical operators return true/false values. The security function
CurrentUserHasCode(x) also returns a true/false value.
The security function CurrentUserName( ) returns a string value. The language function
CurrentLanguage( ) returns a string value.
Expressions that use if-then-else logic can return numeric values, true/false values, or text
strings, depending on how they are structured. These are called conditional expressions
because the result of the expression depends on whether the If statement is true or false.
When the If statement evaluates to true, the result is defined by the Then statement. When
the If statement is false, the result is defined by the Else statement.
The Objects 5 Screen Demo sample application contains many examples of expressions.
For example, see the alarm trigger expressions in the Alarm Setup editor.
20-2
• • • • •
20 • Using expressions
20-3
• • • • •
For details about using the options in the Expression editor, see Help.
Formatting expressions
You can format expressions so they are easier to read. However, do not let tag names,
function names, or function arguments span more than one line.
When formatting expressions, you can use line returns and multiple spaces.
Enclose strings in quotes. The string can contain any character, and can include spaces.
20-4
• • • • •
20 • Using expressions
if (tag1 > tag2) then 0 else if (tag1 > tag3) then 2 else 4
20-5
• • • • •
Constants
A constant can have any of the following formats:
integer (123)
floating-point (123.45)
string constant (“character string”)
Arithmetic operators
Arithmetic operators perform math on two or more numeric values and calculate the
result. The arithmetic operators are:
Example
Symbol Operator (For these examples, tag1 = 5 and tag2 = 7)
20-6
• • • • •
20 • Using expressions
Be sure that any tag value you use as a divisor cannot at some point have a value of zero.
Expressions that attempt to divide a number by zero produce an error at runtime.
String operands
The + operator can be used to join string operands. For example, the expression “hello” +
“world” returns: helloworld.
You cannot join string tags to analog tags, whether they are HMI or data server tags.
Relational operators
Relational operators compare two numeric or string values to provide a true or false result.
If the statement is true, a value of 1 is returned. If false, 0 is returned.
The relational operators are:
Logical operators
Logical operators determine the validity of one or more statements. There are three logical
operators: AND, OR, and NOT. The operators return a non-zero value if the expression is
true, or a 0 if the expression is false.
20-7
• • • • •
Any statement that evaluates to a non-zero value is regarded as true. For example, the
statement tag1 is false if the value of tag1 is 0, and true if tag1 has any other value.
The logical operators are:
Example
(For these examples,
Symbols Operator Action tag1 = 5 and tag2 = 7)
AND, && and Returns a 1 if the (tag1 < tag2) AND (tag1 == 5)
statements to the Both statements are true;
right and left of the returns a 1.
operator are both
true.
OR, || or Returns a 1 if either (tag1 > tag2) OR (tag1 == 5)
the statement to the tag1 == 5 is true;
left or right of the returns a 1.
operator is true.
NOT negation Reverses the NOT (tag1 < tag2)
logical value of the Although tag1 < tag2 is true,
statement it NOT reverses the logical value;
operates on. returns a 0.
The parentheses are essential in the above expressions. They determine the evaluation order of
the operators. For more information, see page 20-10.
Bitwise operators
Bitwise operators examine and manipulate individual bits within a value.
These operators are for integers only, not floating-point numbers. Do not use them with tags or
expressions that return floating-point values.
20-8
• • • • •
20 • Using expressions
20-9
• • • • •
20-10
• • • • •
20 • Using expressions
1 (highest) ()
2 NOT
~
3 *
/
MOD, %
**
AND, &&
&
>>
<<
4 +
-
OR, ||
|
^
5 (lowest) EQ, ==
NE, <>
LT, <
GT, >
LE, <=
GE, >=
20-11
• • • • •
20-12
• • • • •
20 • Using expressions
Mathematical functions
Use math functions to calculate the square root, log (natural or base 10), or trigonometry
ratios (in radians or degrees) of a tag.
20-13
• • • • •
Security functions
Use security functions to control access to your application.
These functions allow you to determine a user’s identity or security rights in order to limit
access to the application based on these criteria.
CurrentUserHasCode True (1) if any of the specified security codes have been assigned
(Security Code Letters) to the user; false (0) if not.
If checking multiple security codes, do not type a space between
the security code letters.
For example: CurrentUserHasCode (ABP) returns the value 1 if
the user has been assigned one or more of the specified codes.
CurrentUserName( ) A string containing the name of the current user.
This function is case sensitive. All RSView 3.x user names use
uppercase letters.
For more information about setting up security for your application, see Chapter 13.
For an example of using the CurrentUserHasCode(x) function, see page 13-13. For
examples of using the CurrentUserName( ) function, see page 13-24.
Language function
The language function shows you which language your application is currently using.
You can display the current language in a string display, or use it in expressions to
generate language-specific messages for your users.
The RFC1766 name is a standard way of representing a language using the format:
languagecode-Country/RegionCode
where languagecode is a lowercase two-letter code and Country/RegionCode is an
uppercase two-letter code.
For example, U.S. English is en-US.
20-14
• • • • •
20 • Using expressions
For more information about setting up languages for your application, see Chapter 23. For
a list of RFC1766 names, see Appendix F.
If-then-else
If-then-else expressions carry out an action conditionally or branch actions depending on
the statements in the expression. The if-then-else statements enable the expression to
perform different actions in different situations and to repeat activities until a condition
changes.
To build conditional expressions, use the relational operators and the logical operators for
the statement and values.
The if-then-else structure is:
if statement then value1 else value2
If the statement is true then the expression returns value1; if the statement is false then the
expression returns value2. If the result of the statement is a non-zero value, the statement
is true (and returns value1); if the result is 0, the statement is false (and returns value2).
The if-then-else structure is illustrated here.
20-15
• • • • •
Nested if-then-else
You can also nest an if-then-else structure inside the Then or Else part of an if-then-else
structure.
20-16
• • • • •
20 • Using expressions
20-17
• • • • •
20-18