The objective of this tutorial is to explain how to create a list of retentive variables in a MasterTool X project.
Components
Equipment: 1 computer/notebook.
Software: MasterTool X
Tutorial Sections
- ARCHITECTURE
- DEVELOPMENT
1. ARCHITECTURE
A computer/notebook will be required to use MasterTool X.
Figure 1. Computer running MasterTool X
2. DEVELOPMENT
Retentive variables are used when you want a value not to be lost in situations such as:
- PLC shutdown
- Power outage
- Controlled CPU reset
That is, the variable value is kept in non-volatile memory and restored when the controller resumes operation.
How Retain Variables Work
- The value is automatically saved by the PLC
- Upon restart, the previous value is restored
- No additional code is needed to maintain the value
- Retention depends on the controller's resources
Excessive use of retainable variables can impact available memory.
When to use retainable variables
Use RETAIN variables for:
- Production counters
- Operating states
- Operator-adjusted parameters
- Essential process history
Avoid using RETAIN for:
- Temporary variables
- Instantaneous signals
- Logic that doesn't need history
Where can retain variables be created?
Retainable variables can be declared in any project scope, including:
- Global variables (GVL)
- Local variables (UserPrg, subroutines, functional blocks, or functions)
The most important point is not where the variable is located, but the correct declaration syntax.
Syntax for Retentive Global Variables
To create retentive global variables, use the block:
VAR_GLOBAL RETAIN
total_counter : DINT;
system_state : BOOL;
END_VARAll variables declared within this block will retain their values.
Figure 2. Creation of global and retentive variables
Syntax for retentive local variables
In UserPrg, subroutines, functional blocks, or functions:
VAR RETAIN
operation_time : TIME;
execution_cycles : DINT;
END_VARThese variables will retain their values even after shutdown or reset.
Figure 3. Creation of local and retentive variables
Best practices
- Use RETAIN only when necessary
- Clearly name retentive variables
- Document usage with comments
- Prefer GVL for shared retentive data
Conclusion
Retentive variables (RETAIN) are fundamental to ensuring process continuity and reliability, allowing important information to be preserved even after terminations, provided they are used judiciously and with good organization.
Comments
0 comments
Please sign in to leave a comment.