Converting Numerical Strings to Double Integers (DINT) in PCS 7 and SIMATIC BATCH
To convert numerical ASCII strings—such as 6-digit material codes (QMAT_SP) or 9-digit batch IDs (VBA_NAME) from SIMATIC BATCH IUNIT_BLOC—into 32-bit double integers (DINT) in SIMATIC PCS 7 V8.1, utilize standard library function FC37 (STRNG_DI) located in the STEP 7 Standard Library under IEC Functions -> IEC. In CFC, insert FC37, wire the STRING input, and connect the resulting DINT to your technological comparison logic. Alternatively, create a custom SCL function utilizing the native STRING_TO_DINT() type conversion operator with validation to ensure non-numeric characters do not cause binary conversion faults.
Table of Contents
- Executive Summary
- Architecture and Fundamental Mechanics
- Step-by-Step Implementation Procedure
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
Executive Summary
In recipe-driven batch manufacturing processes managed by SIMATIC BATCH and SIMATIC PCS 7 V8.1, technological blocks (such as IUNIT_BLOC) exchange operational metadata—including material codes, recipe version identifiers, and vessel allocation names—with the AS controller as alphanumeric STRING data types. Automation engineers frequently need to evaluate these material codes within CFC interlocking logic or route them into integer-based matrix tables (e.g., verifying that a 6-digit material number matches an authorized silo integer ID). Because CFC does not permit direct connection between a STRING and an integer (DINT/INT), engineers require robust data conversion functions. This article details the implementation of standard IEC function FC37 (STRNG_DI) and SCL conversion practices, ensuring reliable numerical parsing.
Architecture and Fundamental Mechanics
The data structures and mathematical boundaries of string-to-integer conversion in SIMATIC S7 operate through defined rules:
-
S7 STRING Memory Structure: In STEP 7, a
STRINGis stored as an array of characters prefixed by a 2-byte header:
- Byte 0:Max_Length(the maximum allocated string capacity, e.g., 254 bytes).
- Byte 1:Actual_Length(the current number of populated ASCII characters).
- Bytes 2+: The actual ASCII character bytes (e.g.,'1'=16#31,'2'=16#32).
If an external system or custom block overwrites string memory without updating theActual_Lengthbyte, standard conversion functions will fail to parse characters. -
Double Integer (DINT) Mathematical Limits: A 32-bit signed Double Integer (
DINT) spans values from $-2,147,483,648$ to $+2,147,483,647$:
- A 6-digit numerical string (e.g.,'123456', max999,999) requires $< 1$ million, fitting easily within a DINT.
- A 9-digit numerical string (e.g.,'123456789', max999,999,999) requires $< 1$ billion, safely below the $2.14$ billion limit.
- A 10-digit string can exceed $2,147,483,647$, triggering mathematical overflow. -
Mechanics of FC37 (STRNG_DI): Function
FC37traverses the string from left to right, evaluating ASCII digits0through9and optional leading signs (+or-). If an invalid non-numerical character (such as a space, letter, or punctuation mark) is encountered, FC37 assertsBIE = 0(binary result / ENO = FALSE) and outputs an invalid conversion result. -
Native SCL Operator (
STRING_TO_DINT): In S7-SCL, the native operatorSTRING_TO_DINT(str)executes an optimized type conversion at runtime, allowing engineers to embed character sanitization, leading zero stripping, and error fallback routines.
Step-by-Step Implementation Procedure
Implement string-to-DINT conversion using either standard CFC blocks or custom SCL logic:
Method 1: Using Standard IEC Function FC37 (STRNG_DI) in CFC
- In SIMATIC Manager, open the target CFC chart.
- In the CFC catalog, expand
Libraries -> Standard Library -> IEC Functions -> Blocks. - Locate
FC37(STRNG_DI) and drag it onto the CFC canvas. - Interconnect the string output from SIMATIC BATCH block
IUNIT_BLOC(e.g., pinQMAT_SPorVBA_NAME) toFC37.S. - Connect the output
FC37.RET_VAL(DINT) to your technological comparison block, multiplexer, or interlock input. - To protect against invalid strings, wire
FC37.ENOto an alarm or fallback permissive to ensure only valid conversions execute.
Method 2: Custom Validating SCL Function Block
- In SIMATIC Manager, open the S7 Program
Sourcesfolder. - Insert a new SCL source file and enter the following validated conversion block:
scl FUNCTION FC_BatchStrngToDint : DINT TITLE = 'Safe String to DINT Converter' VAR_INPUT InStr : STRING[16]; DefaultVal : DINT; END_VAR VAR_OUTPUT Valid : BOOL; END_VAR VAR_TEMP i : INT; Len : INT; END_VAR BEGIN Len := LEN(InStr); Valid := TRUE; IF Len = 0 OR Len > 10 THEN Valid := FALSE; ELSE FOR i := 1 TO Len DO IF InStr[i] < '0' OR InStr[i] > '9' THEN Valid := FALSE; END_IF; END_FOR; END_IF; IF Valid THEN FC_BatchStrngToDint := STRING_TO_DINT(InStr); ELSE FC_BatchStrngToDint := DefaultVal; END_IF; END_FUNCTION - Compile the SCL source and insert the resulting FC into your CFC chart.
Verification
Verify numerical conversion precision and error handling:
-
Valid Numerical Test: Inject a test string
'987654321'into the block input. Confirm online in CFC that the output reportsDINT#987654321andENO == TRUE. -
6-Digit Material Code Test: Inject a standard 6-digit material code
'012345'. Confirm that the block outputsDINT#12345with leading zeros parsed accurately. -
Non-Numeric Character Rejection: Inject an invalid string containing letters (e.g.,
'BATCH_901'). Confirm that the block assertsENO == FALSE(orValid == FALSE), rejecting the value and preventing invalid setpoint calculations.
Limitations and Operational Precautions
Never pass alphanumeric strings containing text characters (e.g., 'TANK_01') into FC37 without error validation; FC37 will fail silently or return unexpected zero values, asserting ENO = FALSE. If material codes in SIMATIC BATCH include letters (e.g., 'MAT-A12'), use string comparison functions (FC10 EQ_STRNG) rather than integer conversion.
Engineer FAQ
Where is FC37 located in STEP 7?
FC37 (STRNG_DI) is located in the Standard Library under IEC Functions -> Blocks in SIMATIC Manager.
What is the maximum number of digits that can convert to DINT?
A DINT can hold up to 9 digits unconditionally (max 999,999,999). 10-digit numbers can convert only if they do not exceed 2,147,483,647.
What happens if the input string contains spaces?
If an input string contains leading, trailing, or internal spaces, FC37 flags an error and de-asserts ENO (FALSE).
Can I convert a DINT back to a STRING?
Yes. Standard function FC16 (DI_STRNG) converts a 32-bit DINT into an ASCII STRING.
Why does IUNIT_BLOC use strings for material codes?
SIMATIC BATCH supports alphanumeric material names and recipe IDs in compliance with ISA-88 standards, which necessitates the use of STRING types.
Technical References
- SIMATIC System Software for S7-300/400 System and Standard Functions Manual
- SIMATIC BATCH Operating and Engineering Manual V8.1
- S7-SCL for S7-300/400 Programming Manual
Related Articles
- Configuring Equipment Phase Display Names Versus Type Names in SIMATIC BATCH
- Managing Unit Allocation Conflicts and Chained Batches in SIMATIC BATCH
- Programmatic Alarm Acknowledgment for PCS 7 ALARM_8P Blocks via S7 Protocol and Sharp7
Related Tags
- FC37
- STRNG_DI
- SIMATIC BATCH
- IUNIT_BLOC
- Data Conversion
- CFC