The purpose of this tutorial is to explain how to convert integer type variables to time type in a MasterTool X project directly.
Components
Equipment: 1 computer/laptop.
Software: MasterTool X
Tutorial Sections
- ARCHITECTURE
- DEVELOPMENT
1. ARCHITECTURE
You will need to use a computer/laptop to run MasterTool X.
Figure 1. Computer running MasterTool X
2. DEVELOPMENT
In some situations, it is not necessary to use a specific conversion block. MasterTool IEC XE allows type conversion directly in the logic, using the standard conversion syntax, such as:
TO_INT
TO_TIME
TO_REAL
among others.
This approach makes the code simpler, cleaner, and more direct, avoiding the use of additional blocks just for conversion.
Applying direct conversion in logic
Conversion can be done directly at the point where the value will be used, such as in function block parameters.
In this example, a TON timer will be used, applying the conversion directly on the time parameter (PT).
Example of use with TON
Consider the following situation:
- The user has a variable of type REAL
- And wants to use it as time in the timer (PT), which requires type TIME
For this, direct conversion can be used:
PT := TO_TIME(TO_INT(tempo_real))
Figure 2. Setting the TON with direct conversion
Syntax explanation
Conversion is done in two steps:
- TO_INT(tempo_real)
- Converts the value from REAL to INT
- TO_TIME(...)
- Converts the integer value to TIME
This is necessary because the TIME type is based on integer values (milliseconds), so the intermediate conversion ensures compatibility.
Operation
Assuming:
tempo_real : REAL := 5000.0;
Applied to the TON:
PT := TO_TIME(TO_INT(tempo_real))
Result:
- 5000.0 (REAL) → 5000 (INT) → T#5s (TIME)
That is, the timer will consider 5 seconds.
Figure 3. Setting the TON with direct conversion
Advantages of direct conversion
- Cleaner and more compact code
- Avoids using additional blocks
- Easier reading and maintenance
- Allows dynamic conversions at runtime
Important considerations
- Ensure compatibility between data types
- Avoid loss of precision when converting REAL to INT
- Remember that the final TIME value will be interpreted in milliseconds
Practical application
This type of conversion is widely used in:
- Timers with variable adjustment
- Integration with HMIs (adjustable values)
- Dynamic process control
- Situations where time is not fixed (not using T# directly)
Figure 4. Operation of direct conversion
Comments
0 comments
Please sign in to leave a comment.