Object Transition
Symbol:
A transition is used to specify a condition, under which a subsequent step is to become active. A transition condition can have the value TRUE or FALSE. If it is TRUE, the next step is executed. A transition condition can be specified in the following two ways:
- (1) Direct („inline condition”): The standard transition name is replaced with the name of a Boolean variables, a Boolean address, a Boolean constant or a statement with Boolean result (e.g. (i<100) AND b). No programs, function blocks or assignments may be specified.
- (2) A separate transition or property object is used (“multi-use condition”): The standard transition name is replaced with the name of a transition or property object. These objects can be created via the command Add > Transition…, which can be found in the context menu (see section “Object Creating a transition”). This enables multiple use of transitions. Like an “inline condition”, the object may contain a Boolean variable, an address, a constant or a statement, and in addition it may contain multiple statements with any code.
Follow the instructions in section “Access to VAR_IN_OUT variables of the function block”.
Creating an object Transition
- 1. Select a function block or a program in the Solution Explorer in the PLC project tree.
- 2. In the context menu select the command Add > Transition…
- The Add Transition dialog opens.
- 3. Enter a name and select an implementation language.
- 4. Click on Open.
- The object is added to the PLC project tree and opens in the editor.
Dialog Add Transition
Name | Name of the transition |
Implementation language | Check box for the implementation language |
Calling a transition
Syntax:
In contrast to TwinCAT 2 PLC Control, transition condition is treated like a method call. The input is based on the following syntax:
<transition name> := <transition condition>
or
<transition condition>
If a transition contains multiple statements, you must assign the desired expression to the transition variable (first variant of the syntax).
Samples:
Call of transition Trans1 in an ST POU:
PRG_SFC.Trans1:
Trans1 := (nCount<=100);
PRG_SFC:
nCount := nCount+1;
IF Trans1 = TRUE THEN // IF nCount<=100 THEN …
nVar:=1;
ELSE
nVar:=2;
END_IF
Call of transition Trans1 in an SFC POU:
Access to VAR_IN_OUT variables of the function block in a method/transition/property
In principle, the VAR_IN_OUT variables of a function block can be accessed in a method, transition or property of the function block. Note the following for this type of access:
- If the body or an action of the function block is called from outside the FB, the compiler ensures that the VAR_IN_OUT variables of the function block are assigned with this call.
- This is not the case if a method, transition or property of the function block is called, since the VAR_IN_OUT variables of the FB cannot be assigned within a method, transition or property call. Therefore, access to the VAR_IN_OUT variables might occur by calling the method/transition/property before the VAR_IN_OUT variables are assigned to a valid reference. Since this would mean invalid access at runtime, accessing the VAR_IN_OUT variables of the FB in a method, transition or property is potentially risky.
Therefore, the following warning with ID C0371 is issued if the VAR_IN_OUT variables of the FB are accessed in a method, transition or property:
„Warning: Access to VAR_IN_OUT <Var> declared in <POU> from external context <Method/Transition/Property>”
An adequate response to this warning could be to check the VAR_IN_OUT variables within the method/transition/property before it is accessed. The operator __ISVALIDREF can be used for this check, to ascertain whether a reference refers to a valid value. If this check is enabled, it can be assumed that the user is aware of the risk that potentially exists when the VAR_IN_OUT variables of the FB are accessed in a method/transition/property. Checking the reference is regarded as adequate handling of this risk. The corresponding warning can therefore be suppressed via attribute 'warning disable'.
A sample implementation of a method is shown below.
Function block FB_Sample:
FUNCTION_BLOCK FB_Sample
VAR_IN_OUT
bInOut : BOOL;
END_Var
Methode FB_Sample.MyMethod:
METHOD MyMethod
VAR_INPUT
END_VAR
// The warning can be disabled here as the user is aware of the risk that the reference may not be valid by checking its validity
{warning disable C0371}
// Checking the VAR_IN_OUT reference, leave the current method in case of invalid reference
IF NOT __ISVALIDREF(bInOut) THEN
RETURN;
END_IF
// Access to VAR_IN_OUT reference (only if the reference was confirmed as valid before)
bInOut := NOT bInOut;
// The warning may be restored at the end of the access area
{warning restore C0371}
See also:
- Object-oriented programming
- Extending a function block
- Reference Programming: SFC elements step and transition