"Invalid" in Expressions and Results
"Invalid" is a term used to signify "no possible result". For example, trying to divide a number by zero. If an I/O tag cannot connect to hardware, its value will be Invalid. Any variable that is declared but not initialized with a value, will start out as Invalid.
Invalid is not an error message. It's a perfectly normal value that you can expect to see frequently. It is a unique data type that is not TRUE or FALSE, numeric or text. VTScada will never write an Invalid to I/O. Invalids guard the system from performing control actions based upon erroneous or bad information. And any calculation that includes an invalid value produces an invalid result (with very few exceptions).
Your expressions need to be able to handle the situation when tags or calculations return a value of Invalid. As an example, suppose that you are trying to calculate a running total instead of using the VTScada Totalizer tag. If the tag value you are watching goes to Invalid, your calculation's result would become Invalid as well unless you have code to say otherwise. To avoid this situation, several functions are available:
PickValid()
This takes a list of parameters and returns the first one that has a valid value. In most expressions that include a tag value, you should protect against an Invalid result by wrapping it in a PickValid. For example:
PickValid(X, 0)
If X, whatever that is, has a valid value it will be used. If it doesn't, then 0 will be used.
Valid()
This tests whether a variable holds a valid value.
Valid(X)
If X has a valid value then this will return TRUE (1). Otherwise, FALSE (0).
True()
Returns a 1 or 0 depending on whether the parameter evaluates to TRUE or FALSE. Always returns 0 if the parameter is Invalid.
False()
Returns a 1 or 0 depending on whether the parameter evaluates to FALSE or TRUE. Like True(), this always returns 0 if the parameter is Invalid.
Using PickValid
When a VTScada application starts, it takes a few moments to establish communications and read tag values. This is true even when communicating with a simulator rather than hardware. The following illustrates the point and the importance of using PickValid when referring to a tag value in your expressions.
Preparation:
- Open the Application Configuration dialog, then Display tab of the Edit Properties page.
(This assumes that you are viewing properties using the basic mode rather than the advanced mode.) - Set the First page at startup property to use the Overview page.
- Apply the change, then close the Application Configuration dialog.
Now for the expression:
- Open the Tag Browser and navigate to Station 1\PLC1\Level.
- Create a new I/O and Calculations tag as a child of Level.
Name it Demo Calc and set the mode to String Calculation - On the Calculation tab, select the Expression option then click the Expression Editor button.
The expression editor dialog opens. - Type the following, exactly as shown:
PickValid([Level], "Not Valid") - Close the expression editor then close the tag properties dialog.
- Draw the calculation tag on the Overview page using the Draw Text widget.
The choice of widget is important. Do not substitute another. - Close the Idea Studio.
The text should show the current level of the tank. You might want to remove the questionable flag. - Stop and restart the application, watching the text carefully as soon as the page loads.
Bonus question: what would you have seen during startup if the expression used the tag name without wrapping it in a PickValid? If unsure, try it and find out.
Other situations that can generate an Invalid value:
Double Set:
You will see this error only when writing VTScada script modules, not when writing expressions in tags and widgets.
Do not attempt to set a variable's value in two or more lines of code (statements) within the same state, operating in steady-state mode. Typically, you would encounter this if editing the code for a page or widget.
In this situation it is uncertain which value is correct, so the variable will be set to invalid even if both values are the same. This is known as a "double set".
Array subscript out of range:
An array reference that has one of its subscripts out of range will have an invalid value. Some programmable controllers flag inputs when they are out of range - when these values are read by VTScada, they are declared to be invalid.
Invalid mathematic operations:
Division by 0, taking the square root or logarithm of a negative number, and other illegal mathematical operations result in invalid results.
On purpose:
A value can be purposely set to invalid by using the Invalid() function.