Developing SCL Custom Motor Blocks for SIMATIC PCS 7 - Byte-Swapping, Status DWORD Formatting, and APL Block Icon Integration

Developing SCL Custom Motor Blocks for SIMATIC PCS 7 - Byte-Swapping, Status DWORD Formatting, and APL Block Icon Integration

When developing custom function blocks in Structured Control Language (SCL) for SIMATIC PCS 7, mapping internal Boolean states to a status DWORD (such as Status1 or Status2) for WinCC visualization requires addressing the S7 Big-Endian to WinCC Little-Endian byte-swap architecture. In S7-400 controllers, the first byte of a DWORD (Byte 0) contains bits 24 to 31, while WinCC evaluates Bit 0 as Byte 0, Bit 0. To align SCL code cleanly without confusing bit offsets, engineers declare a custom User-Defined Data Type (UDT) with pre-swapped bit structures and overlay it onto the DWORD using the SCL 'AT' construct. Furthermore, the block icon's 'type' attribute must match the SCL block name prefixed with '@' (e.g. '@DriveMOTOR1D/1').

Table of Contents

Endianness and Byte-Swapping Architecture in S7-400 and WinCC

In SIMATIC PCS 7 automation architectures, communication between S7-400 controllers and WinCC operator stations bridges two fundamentally different memory architectures:
- S7-400 Controller (Big-Endian):
* In a 32-bit double word (DWORD), the most significant byte (MSB, Byte 0) is stored at the lowest memory address, and the least significant byte (LSB, Byte 3) is stored at the highest memory address.
* In standard STEP 7 notation, DWORD bits are numbered 0 (LSB) to 31 (MSB), meaning Byte 0 contains bits 24–31.
- WinCC OS Station / x86 Architecture (Little-Endian):
* Microsoft Windows and WinCC treat Byte 0 as the least significant byte.
* When WinCC reads an S7 DWORD via the SIMATIC S7 Protocol Suite, a byte reversal occurs across the 32-bit boundary.
* Consequently, if an SCL programmer sets Status1.Bit0 on the S7 CPU, WinCC interprets this as Bit 24 in its internal tag management!
- Engineering Complication: If an engineer attempts to compensate by hardcoding Bit 24 in SCL, the logic becomes unreadable in CFC test mode and breaks standard APL status word conventions.

The SCL 'AT' Construct and UDT-Based Status Word Formatting

To achieve clean, professional bit alignment where Bit 0 in SCL maps directly to Bit 0 in WinCC Extended Status Displays, use an overlay UDT:

  1. Define the Swapped UDT Structure:
    - Create a User-Defined Data Type (e.g. UDT 1850 named SBit32inDWORD):
    scl TYPE SBit32inDWORD STRUCT // Byte 3 in S7 (Maps to WinCC Bits 0 to 7) b0 : BOOL; b1 : BOOL; b2 : BOOL; b3 : BOOL; b4 : BOOL; b5 : BOOL; b6 : BOOL; b7 : BOOL; // Byte 2 in S7 (Maps to WinCC Bits 8 to 15) b8 : BOOL; b9 : BOOL; b10: BOOL; b11: BOOL; b12: BOOL; b13: BOOL; b14: BOOL; b15: BOOL; // Byte 1 in S7 (Maps to WinCC Bits 16 to 23) b16: BOOL; b17: BOOL; b18: BOOL; b19: BOOL; b20: BOOL; b21: BOOL; b22: BOOL; b23: BOOL; // Byte 0 in S7 (Maps to WinCC Bits 24 to 31) b24: BOOL; b25: BOOL; b26: BOOL; b27: BOOL; b28: BOOL; b29: BOOL; b30: BOOL; b31: BOOL; END_STRUCT END_TYPE

  2. Overlay in SCL Block Using 'AT' Construct:
    - In the SCL source file for the motor block (e.g. FUNCTION_BLOCK FB 1500 DriveMOTOR1D):
    ```scl
    VAR_OUTPUT
    Status1 { S7_m_c := 'true' } : DWORD := 16#00000000;
    END_VAR
    VAR_TEMP
    dwStatus1 : DWORD;
    ArrdwStatus1 AT dwStatus1 : SBit32inDWORD;
    END_VAR

    // Assignment in code:
    ArrdwStatus1.b0 := Occupied;
    ArrdwStatus1.b1 := FeedbackRun;
    ArrdwStatus1.b20 := MotorStopped;
    ArrdwStatus1.b23 := MotorRunning;

    // Transfer to output:
    Status1 := dwStatus1;
    `` - This approach ensures thatb0in SCL translates transparently intoBit 0` inside WinCC without manual bit shifting.

Configuring Custom Block Icons and Extended Status Displays

Linking the custom SCL block to WinCC Graphics:

  1. Setting Block Icon System Properties:
    - In Graphics Designer, open the template picture (e.g. @PCS7Typicals_Custom.pdl).
    - Create or customize the block icon.
    - Open Object Properties > System:

    • Set attribute type to @DriveMOTOR1D/1.
    • The @ prefix denotes automatic block icon generation by the Plant Hierarchy.
    • DriveMOTOR1D matches the SCL block family/name.
    • /1 designates block icon variation number 1.
  2. Connecting Properties to Extended Status Display:
    - Right-click the block icon and select Customized Object > Configuration Dialog.
    - Under Selected Properties, ensure external property Links > Status1 is mapped to the internal Symbol1.Status1 input.
    - If previous templates mapped to Status2, update the link to Status1 so that Symbol1 (Extended Status Display), Symbol2 (Operating Modes), and Lock icons all evaluate the active status output of the block.

S7 Controller vs WinCC OS Bit Addressing Alignment Table

The following table demonstrates the byte and bit correspondence between S7 controllers and WinCC:

Physical Data ByteS7-400 Big-Endian Bit RangeSCL Overlay Mapping (SBit32inDWORD)WinCC Little-Endian Bit PositionExtended Status Display ConfigurationByte 3Bits 0 to 7b0 to b7Bit 0 to Bit 7Select from status word 1, Bit 0–7Byte 2Bits 8 to 15b8 to b15Bit 8 to Bit 15Select from status word 1, Bit 8–15Byte 1Bits 16 to 23b16 to b23Bit 16 to Bit 23Select from status word 1, Bit 16–23Byte 0Bits 24 to 31b24 to b31Bit 24 to Bit 31Select from status word 1, Bit 24–31

Verification

To confirm that the custom SCL motor block and block icon function correctly:
1. Compile the SCL source in STEP 7 with zero errors or warnings.
2. Insert the new FB into a CFC chart, name the instance MTR_01, and assign an icon number (or leave blank to use /1).
3. Compile the OS and verify that WinCC Tag Management generates structure tag MTR_01 with member Status1.
4. In SIMATIC Manager, execute Plant Hierarchy > Create/Update Block Icons. Confirm that the block icon @DriveMOTOR1D/1 is automatically instantiated in the target process picture.
5. In WinCC Runtime, force MotorStopped = TRUE in CFC; verify the block icon immediately renders the stopped symbol (Index 1).
6. Force MotorRunning = TRUE; verify the block icon switches immediately to the running symbol (Index 8).

Limitations and Operational Precautions

When developing custom blocks in SCL:
1. Never Change Block Interface in RUN Without Reserve: Modifying VAR_INPUT or VAR_OUTPUT parameters in SCL alters the instance DB structure, preventing online delta downloads and forcing a complete CPU STOP. Use pre-allocated reserve parameters (Reserve_BOOL_1, etc.) for future expansion.
2. OCM Attribute Requirements: Always tag output status words with { S7_m_c := 'true' } and { S7_link := 'false' } in the SCL declaration. Omitting S7_m_c prevents the OS compiler from creating WinCC structure tags.
3. Avoid Complex Bit Shifting Loops: Using arithmetic multiplication (* 2) or manual bit-shift instructions in SCL to pack DWORDs consumes significant CPU cycle time. The AT overlay construct executes at zero runtime CPU cost because it maps memory directly at compile time.

Engineer FAQ

Why does WinCC show Bit 24 active when I set Bit 0 in SCL?

Because S7-400 controllers store double words in Big-Endian format while WinCC reads them in Little-Endian format. Byte 0 in S7 maps to the highest byte (bits 24-31) in WinCC unless an overlay UDT is used.

What does the 'AT' construct do in SCL?

The 'AT' keyword creates an alternate view of an existing memory variable. It allows the exact same memory area (e.g. a 32-bit DWORD) to be accessed simultaneously as a structured collection of 32 individual Boolean variables.

Why did 'Create/Update Block Icons' fail to place my custom icon?

Check the 'type' attribute of the block icon in your template picture. It must strictly match @<SCL_Block_Name>/<Number> (e.g. @DriveMOTOR1D/1). Ensure the template picture is named @PCS7Typicals.pdl or @PCS7Typicals_Custom.pdl.

Can I use standard APL faceplates with my custom SCL block?

Yes, provided your custom block outputs match the exact variable names (Status1, Status2, EventState) and bit structures expected by the standard APL faceplate scripts.

Technical References

Related Articles

Related Tags

  • PCS7_SCL
  • Custom_Blocks
  • Byte_Swap
  • Big_Endian
  • AT_Construct
  • Status_Word
  • Block_Icons
6 respuestas