How to Implement REAL Truncation and Millisecond-to-Time Conversion Blocks in PCS 7 SCL
In SIMATIC PCS 7 CFC, standard conversion blocks like R_I (FC106) round floating-point numbers to the nearest integer (e.g., 2.87 rounds to 3) rather than truncating the fractional portion. To extract strictly the integer part (truncation, e.g., 2.87 to 2), or to convert elapsed milliseconds into a formatted time structure (hours, minutes, seconds), engineers develop custom Structured Control Language (SCL) blocks. In SCL, the native TRUNC function extracts the integer portion directly, while integer division and modulo (MOD) operators decompose millisecond counts into hours, minutes, and seconds for faceplate display.
Table of Contents
- Rounding vs Truncation Behavior in Standard STEP 7 / CFC Blocks
- Developing a Custom Truncation Function in SCL
- Developing a Millisecond-to-Time Decomposition Block in SCL
- Online Block Type Replacement Strategies Without Stopping the CPU
- Comparison of Floating-Point Conversion Operations in STEP 7
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
Rounding vs Truncation Behavior in Standard STEP 7 / CFC Blocks
In SIMATIC PCS 7 continuous control logic, mathematical operations often require converting floating-point measurements (REAL) into whole numbers (DINT or INT)—for example, indexing batch recipe steps, batch counting, or calculating integer hours of pump run-time.
When engineers use the standard CFC library block R_I (FC106 - Real to Integer):
* R_I executes the underlying STL instruction RND (IEEE 754 Round to Nearest Even Integer).
* A value of 2.87 is rounded up to 3.
* A value of 2.12 is rounded down to 2.
* In contrast, Truncation (TRUNC) discards the fractional decimal part entirely without rounding: 2.87 truncates to 2, and -2.87 truncates to -2.
* Because standard PCS 7 CFC libraries do not include an off-the-shelf truncation block, creating a custom SCL block is the established industry solution.
Developing a Custom Truncation Function in SCL
Structured Control Language (SCL) is the native high-level programming language for SIMATIC PCS 7. Below is the optimized source code for function FC1510 ("TruncReal"):
FUNCTION FC1510 : VOID
TITLE = 'Truncate REAL to Integer without Rounding'
VERSION : 0.1
AUTHOR : 'EngTeam'
FAMILY : 'MATH'
VAR_INPUT
In : REAL; // Floating-point input value
END_VAR
VAR_OUTPUT
Out_DInt : DINT; // Truncated Double Integer output
Out_Real : REAL; // Truncated REAL output (fractional part zeroed)
END_VAR
BEGIN
// TRUNC instruction extracts integer part
Out_DInt := TRUNC(In);
// Convert back to REAL if floating-point format is required downstream
Out_Real := DINT_TO_REAL(Out_DInt);
END_FUNCTION
This block compiles directly into the STEP 7 block folder and can be placed into any CFC chart.
Developing a Millisecond-to-Time Decomposition Block in SCL
When tracking elapsed batch timers or equipment running hours, timers output elapsed milliseconds as TIME or DINT. To display this on WinCC faceplates as formatted Hours, Minutes, and Seconds:
FUNCTION_BLOCK FB1511
TITLE = 'Decompose Milliseconds to Hours, Minutes, Seconds'
{ S7_m_c := 'true' }
VERSION : 0.1
FAMILY : 'CONVERT'
VAR_INPUT
ms_In { S7_m_c := 'true' } : DINT := 0; // Elapsed time in milliseconds
END_VAR
VAR_OUTPUT
Hours { S7_m_c := 'true' } : INT := 0; // Elapsed hours
Minutes { S7_m_c := 'true' } : INT := 0; // Elapsed minutes (0..59)
Seconds { S7_m_c := 'true' } : INT := 0; // Elapsed seconds (0..59)
END_VAR
VAR_TEMP
total_sec : DINT;
rem_sec : DINT;
END_VAR
BEGIN
// Convert total ms to total seconds
total_sec := ms_In / 1000;
// Calculate Hours
Hours := DINT_TO_INT(total_sec / 3600);
// Remaining seconds after extracting hours
rem_sec := total_sec MOD 3600;
// Calculate Minutes and Seconds
Minutes := DINT_TO_INT(rem_sec / 60);
Seconds := DINT_TO_INT(rem_sec MOD 60);
END_FUNCTION_BLOCK
By tagging outputs with S7_m_c := 'true', hours, minutes, and seconds are automatically compiled into WinCC OS tags for direct faceplate display.
Online Block Type Replacement Strategies Without Stopping the CPU
When deploying custom SCL blocks onto an active, running PCS 7 controller:
- Modifying an Existing FB Type:
- Modifying the interface of an FB already used in running charts changes the instance data block structure.
- Downloading a modified FB type requires an AS STOP unless the CPU is an S7-400H or CPU 410-5H operating under TCiR (Type Change in RUN).
- Safe Replacement Technique (No STOP):
1. If updating logic in a running plant, save the SCL source with a new name and unique block number (e.g., changeFB1511toFB1512- "MsToTime_v2").
2. Compile the new SCL source.
3. In CFC, insert the new block alongside the old block, transfer connections, and delete the old block.
4. Perform an online download:PLC > Download > Changes Only. The CPU downloads the new block seamlessly in RUN mode without process disruption.
Comparison of Floating-Point Conversion Operations in STEP 7
The table below compares the mathematical conversion operations supported in SIMATIC STEP 7 and SCL.
ROUND(In)RND2.873TRUNC(In)TRUNC2.872RND+RND+2.123RND-RND--2.12-3R_I)FC106RND2.873 (Rounded)Verification
To confirm that the custom SCL blocks operate accurately:
1. Open the CFC chart online in Test Mode.
2. In the TruncReal block, write test value 2.87 to In: confirm that Out_DInt reports 2 and Out_Real reports 2.0.
3. Write negative test value -5.91: confirm that Out_DInt reports -5.
4. In the MsToTime block, write 3723000 ms (1 hour, 2 minutes, 3 seconds): verify that Hours = 1, Minutes = 2, and Seconds = 3.
5. In WinCC Graphics Designer, connect an I/O field to the compiled tags and confirm that values render correctly on the operator faceplate.
Limitations and Operational Precautions
- Floating-point numbers exceeding the range of a 32-bit integer (
-2,147,483,648to+2,147,483,647) will cause mathematical overflow inTRUNC, setting the CPU overflow bit (OV). Ensure input limits are checked before truncation. - When compiling SCL sources, verify that the assigned FB/FC number does not collide with existing library blocks in the Master Data Library.
- Do not perform modulo operations with a divisor of zero; division by zero will cause an internal CPU fault (OB121).
- Always back up the Master Data Library before importing new SCL blocks into production projects.
Engineer FAQ
Why does FC106 (R_I) round instead of truncate?
FC106 was designed following standard IEC 61131 rules for real-to-integer conversion, which mandates standard rounding (0.5 and above rounds up). Truncation is a separate mathematical operation requiring the TRUNC instruction.
Can I create the truncation block in Statement List (STL) instead of SCL?
Yes. In STL, the code requires only three instructions: L #In; TRUNC; T #Out;. However, SCL is the standard language for PCS 7 because it provides stronger type safety, structured comments, and seamless integration with Master Data Libraries.
How does CPU 410-5H handle Type Change in RUN (TCiR)?
CPU 410-5H (firmware V8.1 and higher) supports TCiR, allowing engineers to download modified block interfaces in RUN mode without stopping the CPU, provided memory reserves were configured in the block properties.
Technical References
- SIMATIC S7-SCL for S7-300 and S7-400 Programming Manual
- Siemens Industry Online Support FAQ: SCL Programming and Conversion Functions in PCS 7
Related Articles
- Best Practices for SCL Function Block Development in PCS 7
- Hardware-in-RUN (H-CiR) and TCiR on SIMATIC CPU 410 Systems
- Reconciling Master Data Libraries and Updating Block Types
Related Tags
- SCL
- TRUNC
- R_I
- Data Conversion
- Math Utilities