Expression Binding
Binding Properties to the Outcome of an Expression
An expression binding is one of the most powerful kinds of property bindings. It uses a simple expression language to calculate a value. This expression can involve lots of dynamic data, such as other properties, Tag values, results of Python scripts, queries, and so on. You can use expressions for many different purposes. Anytime information needs to be massaged, manipulated, extracted, combined, split, and so on - think expressions!
For a complete list of the Expression Functions, see the Expression Functions page in the Appendix.
Example 1
You have a button that starts a batch, but you only want to let it be pressed after the operator has entered a scale weight. Bind the button's enabled property to:
{Root Container.EntryArea.WeightBox.doubleValue} >
0.0
Example 2
You want to display a process's current state, translating a code from the PLC to a human-readable string, use of these two expressions (they're equivalent)
if
({CurrentProcessState}
=
0
,
"Not Running"
,
if
({CurrentProcessState}
=
1
,
"Warmup phase - please wait"
,
if
({CurrentProcessState}
=
2
,
"Running"
,
"UNKNOWN STATE"
)))
switch ({CurrentProcessState},
0
,
1
,
2
,
"Not Running"
,
"Warmup phase - please wait"
,
"Running"
,
"UNKNOWN STATE"
)
See also: Expression Overview and Syntax
Expression Function Examples
Concat Strings
You can use an expression binding to concatenate strings resulting in a new string that reflects the concatenation of different strings.
The function starts with CONCAT() and inside the function is a list of strings. They can be manually typed like "42" or they can come from Tags or properties.
Example 1
concat(
"The answer is: "
,
"42"
)
//returns "The answer is: 42"
Example 2
You have a Date, and need to extract the year, and concatenate the word "Vintage" to the end for a label display. Bind a label's text property to:
dateExtract({Root Container.VintageDate},
'year'
)
+
' Vintage'
Celsius to Fahrenheit
Use an expression binding to convert a temperature from Celsius to Fahrenheit. This is an example of how expression binding can handle calculations.
{celsiusTemp} *
9
/
5
+
32
The reference to the Celsius temperature can come from a property or a tag. As the property or the tag changes, so does the expression binding.
Format Date
You can format a date in an expression binding by using the dateFormat and now functions.
To make a label that updates to show the current time:
-
Drag a Label component onto the window.
-
Select the label's Text property binding icon and select Expression binding.
-
Enter the following code into the expression, and click OK.
dateFormat(now(
1000
),
"MMM d, yyyy hh:mm:s a"
)
The dateFormat function takes two arguments. The first argument is any date type variable. This can include another function (like now) that returns a date type. The second argument refers to the date format that you want returned. The now function returns the current time and in this case it will update every second.
For more information on the date formatting expression, see the Appendix.
Date Manipulations
You can manipulate dates in expression bindings such as a date addition/subtraction with the dateArithmetic function. This is important when you want to use the expression bindings to select a date that is offset by a certain amount.
The following example will return the time 15 minutes ago by using the dateArithmetic expression function:
dateArithmetic(now(), -
15
,
"minute"
)
Bit Functions
You can use various bit functions in expression bindings like getBit to return individual bits of a word.
Example 1
Assuming a tag path 'Folder/BitTag', the following would return the binary representation of the tag's value at the 0 position
getBit({Folder
/
BitTag},
0
)
For more details on the getBit function please see the getBit page in the Appendix.
Example 2
You have 3 bits in a PLC, only one of which will be on at a time. You want to turn these three bits into a single integer (0,1,2) to drive a component's Styles. Bind a custom integer property to:
binEnum({MyTags
/
Bit1}, {MyTags
/
Bit2}, {MyTags
/
Bit3})
Switch
You can use the switch function in expression bindings to evaluate conditional statements. This function acts like the switch statement in C-like programming languages. It takes the value argument and compares it to each of the case1 through caseN expressions.
The following example returns the string "Running" when it is given the value of 1. Its options are 0,1, and 2. And when comparing the value to the options the switch statement returns one of the corresponding results. If a result cannot be found, a fail-over option is returned.
switch
(
1
,
0
,
1
,
2
,
"Off"
,
"Running"
,
"Fault"
,
forceQuality(
"!BAD STATE!"
,
0
))
Checking Conditions
You can use expression bindings to return true or false based on different conditions.
Example 1
Consider the following expression that references the Tag with a path of 'Folder/Machine State':
{Folder
/
Machine State}
=
0
The above expression simply tests the value of the Machine State tag. If the value of the Machine State Tag is ever equal to 0 then the above expression would return true. In every case where Machine State is not equal to 0, then the expression would return false.
Example 2
It is possible to check for multiple conditions in the same expression. If you have two boolean tags and you only want the expression to return true if both tags are true then the binding would look like:
{boolTag1}
=
True
&& {boolTag2}
=
True
Similar Topics ...