Converting Boolean to Integer in SIMATIC PCS 7 CFC - Standard Multiplexer vs SCL Implementation

Converting Boolean to Integer in SIMATIC PCS 7 CFC - Standard Multiplexer vs SCL Implementation

In SIMATIC PCS 7 Continuous Function Chart (CFC) and STEP 7-400 engineering, converting a binary Boolean signal (BOOL) into an Integer (INT) where FALSE produces 0 and TRUE produces 1 can be accomplished using standard library blocks or a lightweight custom SCL function. In standard CFC without custom programming, the recommended approach is using the standard elementary multiplexer block MUX2_I (FB54 or FC multiplexer from CFC Library > Math or Standard Library) or selection block SEL_BO, where the Boolean signal acts as the select input switching between constant values 0 and 1. Alternatively, if S7-SCL authoring is preferred, the IEC standard conversion function BOOL_TO_INT() can be encapsulated inside an FC or FB.

Table of Contents

The Need for BOOL-to-INT Conversion in Process Logic

In SIMATIC PCS 7 automation architectures, engineers frequently encounter requirements to convert discrete binary conditions (BOOL) into integer representations (INT or DINT):
- Passing multi-state status words to third-party PLCs or remote telemetry units (RTUs) via Modbus RTU or Profibus.
- Driving integer selector pins on downstream mathematical blocks (e.g., gain scheduling multipliers).
- Accumulating active operational status counts (e.g., counting how many pumps are currently running out of a group).
- Interfacing with WinCC graphic object properties that require integer status codes for dynamic color evaluation.

Because CFC strictly enforces type safety, directly wiring a BOOL output pin to an INT input pin is prohibited by the graphical connection parser. A conversion entity must be placed between them.

Method 1: Standard CFC Multiplexer Block (MUX2_I / SEL_BO)

The most straightforward, built-in solution that requires zero custom code or compilation overhead is using the standard Multiplexer or Selection block:

  1. Locate MUX2_I or SEL_BO:
    - In CFC Editor, expand the catalog pane on the left.
    - Navigate to: Libraries > CFC Library > ELEMENTA > MUX2_I (or JUMPER > SEL_BO).
  2. Place Block on Chart: Drag and drop MUX2_I onto the active CFC sheet.
  3. Configure Parameter Inputs:
    - IN0: Set to 0 (value when Boolean is FALSE).
    - IN1: Set to 1 (value when Boolean is TRUE).
    - K (Selector pin): Interconnect the source BOOL signal to pin K.
  4. Connect Output:
    - The output pin OUT (type INT) delivers 0 when K = FALSE and 1 when K = TRUE.
  5. This method uses native Siemens-certified firmware primitives, ensuring deterministic execution with zero memory overhead.

Method 2: Standard Library Bit Processing Blocks (BITSUM / BO_W)

When converting or summing multiple Boolean signals into an integer:

  • BITSUM (FC99): Located in Standard Library > TI-S7 Converting Blocks. It counts the total number of set bits (1s) in a 32-bit double word. Passing a single boolean bit inside a word yields an integer output of 0 or 1.
  • BO_W (FC82): Located in the CFC Library. It packs up to 16 individual Boolean signals (IN0 through IN15) into a single 16-bit WORD. By interconnecting the single Boolean to IN0 and grounding the others, the output word can be cast or interpreted as an integer value of 0 or 1.

Method 3: Authoring a Reusable SCL Conversion Block

If your engineering standard requires a dedicated function block with custom naming and telemetry attributes:

  1. In SIMATIC Manager, navigate to S7 Program > Sources.
  2. Insert a new SCL source file named FC_BoolToInt.
  3. Open the source editor and paste the following code:
    ```pascal
    FUNCTION FC_BoolToInt : INT
    TITLE = 'Bool to Int Converter'
    VERSION : '1.0'
    AUTHOR : 'PCS7_ENG'

VAR_INPUT
InBool : BOOL;
END_VAR

BEGIN
// Convert Boolean to Integer using standard IEC conversion
FC_BoolToInt := BOOL_TO_INT(InBool);
END_FUNCTION
`` 4. Compile the SCL source (Ctrl+F9). 5. The compiled functionFC_BoolToIntis saved in the offline block folder and immediately available in the CFC block catalog underS7 Program > Blocks` for placement on any chart.

Technical Comparison: Boolean-to-Integer Conversion Methods

The following table compares approaches for converting Boolean signals to Integer in PCS 7:

Conversion MethodBlock TypeCPU Work MemoryCustom Code RequiredBest Suited ForMUX2_I (CFC Library)System PrimitiveNegligible (~30 bytes)NoneStandard single-bit conversion in CFCSEL_BO (CFC Library)System PrimitiveNegligible (~30 bytes)NoneSelecting alternate integer constantsCustom SCL (BOOL_TO_INT)User FC / FBLow (~50 bytes)Yes (SCL Source)Standardized corporate library templatesBITSUM (FC99)Standard FCLow (~80 bytes)NoneCounting multiple concurrent active bits

Verification

To confirm proper conversion behavior in runtime:
1. Online CFC Test Mode: Open the CFC chart in test mode (Ctrl+T); activate the yellow glasses icon.
2. State 0 Test: Force the input Boolean signal to FALSE; verify that the integer output displays 0.
3. State 1 Test: Force the input Boolean signal to TRUE; verify that the integer output displays 1.
4. Downstream Block Response: Confirm that downstream mathematical or selector blocks receive the integer value and execute without type mismatch errors.

Limitations and Operational Precautions

Observe the following engineering precautions:
- When using MUX2_I, ensure that IN0 and IN1 parameters are not left uninitialized or connected to floating variables.
- Custom SCL functions (FCs) do not retain internal states across cycles; if edge detection or signal latching is required, use a Function Block (FB) with an instance data block.
- In PCS 7 APL architectures, if working with structured variables (Struct containing Value and ST), ensure the signal status byte is propagated alongside the converted numeric integer.

Engineer FAQ

Can I convert a Boolean to a Double Integer (DINT)?

Yes. Use the multiplexer block MUX2_DI from the CFC Library, or in SCL execute DINT_VAL := BOOL_TO_DINT(InBool);.

Why doesn't CFC allow direct type-casting like in C programming?

CFC is an industrial automation programming language adhering to IEC 61131-3. Strict type checking prevents memory misinterpretation and unintended bit-shifting errors in safety-critical process automation.

What block number should I assign to a custom SCL conversion function?

Assign an FC number within the user-reserved range configured in SIMATIC Manager (e.g., FC500 or above), ensuring it does not collide with reserved Siemens library FC numbers.

Technical References

Related Articles

Related Tags

  • CFC
  • S7-SCL
  • Data Type Conversion
  • BOOL_TO_INT
  • Multiplexer
6 respuestas