The objective of this tutorial is to explain how to create a list of persistent 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
Persistent variables are used when you want to ensure that certain values are permanently preserved, even in critical situations such as:
- Complete PLC shutdown
- Downloading new code
- Project changes
- Application reset
They are ideal for data that cannot be lost under any circumstances, such as calibration parameters, recipes, operator settings, or critical production data.
Difference between RETAIN and PERSISTENT variables
| Variable type | Declaration block | Retains value after PLC shutdown | Retains value after code download | Can be changed at runtime | Where it can be created | Typical use |
| VAR (normal) | VAR | No | No | Yes | Local (UserPrg, POU) or Global (GVL) | Common logic, temporary signals |
| VAR RETAIN | VAR RETAIN VAR_GLOBAL RETAIN | Yes | No (can be lost) | Yes | Local or Global | States that need to survive power failure |
| VAR PERSISTENT | VAR_PERSISTENT | Yes | Yes | Yes | Only in PersistentVars | Critical parameters, calibration, recipes |
| VAR CONSTANT | VAR CONSTANT VAR_GLOBAL CONSTANT | Not applicable | Not applicable | No | Local or Global | Fixed values, limits, calculation |
Important notes
- Normal variable (VAR)
- Loses its value whenever the PLC restarts or the code is reloaded.
- It is the most used in everyday life.
- RETAIN Variable
- Maintains value during power outages.
- Can be reset on full downloads or structural changes.
- PERSISTENT Variable
- Maximum retention level.
- Always created in the PersistentVars object.
- Ideal for data that cannot be lost.
- CONSTANT Variable
- Fixed value defined in the declaration.
- Cannot be written or forced.
- Does not occupy dynamic runtime memory.
Where persistent variables can be created
Unlike RETAIN variables, persistent variables can only be created in a specific location in the project.
They cannot be declared:
- In UserPrg
- In subroutines
- In functional blocks
- In functions
- In common GVLs
How to create persistent variables (step by step)
- In the project tree, locate the Application item
- Right-click on Application
- Select Add object
- Choose PersistentVars
This object will be the only valid location for declaring persistent variables.
Figure 2. Creating the persistent variables menu
Declaration of persistent variables
Within the PersistentVars object, use the syntax:
VAR_GLOBAL PERSISTENT RETAIN
total_production: INT;
active_revenue: INT;
calibration_factor: REAL;
END_VARAll variables declared in this block:
- Are persistent
- Are automatically retained
- Maintain their values even after downloading new code
Figure 3. Creation of persistent variables
Use of persistent variables in the project
After being declared, persistent variables:
- Can be used anywhere in the code
- Function as global variables
- Do not require any additional configuration
- Access is direct by variable name.
Best practices in the use of persistent variables
- Use only for truly critical data
- Avoid storing temporary signals
- Use clear and descriptive names
- Document the reason for persistence
- Avoid excessive persistent variables (non-volatile memory is limited)
Conclusion
Persistent variables (PERSISTENT) guarantee the highest level of data preservation in the PLC.
They should be used judiciously, only when the loss of information is unacceptable, and always created in the correct object (PersistentVars), respecting good industrial programming practices.
Figure 4. Use of persistent variables in UserPrg
Comments
0 comments
Please sign in to leave a comment.