Features, discussions, tips, tricks, questions, problems and feedback

GX Works Functions (FUN) vs Function Blocks (FB)

GX Works Functions (FUN) vs Function Blocks (FB)

A Comparison between functions and function blocks in GX Works.

Functions :

  • GX Works2 & 3 : Stateless routines that take input(s), process them, and return a single output. They execute once per call, don’t retain data between calls, and are ideal for simple, reusable calculations (e.g., math operations).

Function Blocks (FBs) :

  • GX Works2 : Reusable, stateful program blocks with multiple inputs/outputs, internal memory to retain data (e.g., timers, counters), and modular design for complex logic.
  • GX Works3 : Enhanced FBs with structured programming support, including Macro Type (inline, stateless) and Subroutine Type (modular, stateful), offering better encapsulation and reusability.

Functions are stateless routines that process inputs to produce a single output, ideal for simple, reusable calculations like data conversions, with enhanced structured data support in GX Works3. Function Blocks (FBs), however, are stateful, modular blocks handling multiple inputs/outputs and retaining data for complex logic like motor control.

In GX Works2, FBs are robust but lack advanced structuring, while GX Works3 introduces Macro Type (inline, stateless) and Subroutine Type (modular, stateful) FBs for greater flexibility. Functions suit quick tasks, whereas FBs excel in reusable, stateful control, with GX Works3 offering superior encapsulation and versatility.

Functions: Application Examples & Principles
Description: Functions are stateless, single-output routines that process inputs and return a result, executing fully per call. Ideal for simple, reusable calculations.

Application Examples:

  1. Unit Conversion:
    • Use Case: Convert a pressure reading from kPa to psi for display.
    • Example: A function takes a REAL input (kPa), applies psi = kPa * 0.145038, and returns the result.

  2. Bit Manipulation:
    • Use Case: Extract specific bits from a word for status checking.
    • Example: A function takes a WORD input and returns a BOOL for a specific bit position (e.g., bit 3 for fault status).

  3. Math Calculation:
    • Use Case: Calculate the average of multiple sensor readings.
    • Example: A function takes an array of INTs, sums them, divides by the count, and returns the average as a REAL.

Principles for Application:
• Simplicity: Use for straightforward calculations or logic that doesn’t need state retention (e.g., conversions, basic math).
• Reusability: Functions are perfect for tasks repeated across programs, like formatting data or simple checks.
• Efficiency: They execute quickly, minimizing PLC scan time for lightweight operations.
• GX Works3 Edge: Supports structured data types (e.g., REAL, ARRAY), making Functions more versatile than in GX Works2’s simpler framework.

Function Blocks (FBs): Application Examples & Principles

Description: FBs are stateful, modular blocks with multiple inputs/outputs, retaining data between calls. GX Works3 introduces Macro Type (inline, stateless) and Subroutine Type (modular, stateful) FBs.

Application Examples:

  1. Timer Control:
    • Use Case: Manage a delay-on timer for a pump startup sequence.
    • Example: An FB (Subroutine Type in GX Works3) takes a BOOL input (Start), tracks elapsed time internally, and outputs a BOOL (On) after a set delay.

  2. Counter Logic:
    • Use Case: Count product units on a conveyor and signal when a batch is complete.
    • Example: An FB takes a BOOL input (Pulse), increments an internal counter, and outputs a BOOL (BatchDone) when the target is reached.

  3. State Machine:
    • Use Case: Control a filling process with steps (Idle, Fill, Stop).
    • Example: A Macro Type FB in GX Works3 manages state transitions based on sensor inputs and outputs control signals for each step.

Principles for Application:
• Modularity: Use FBs for complex logic that benefits from encapsulation, like control sequences or stateful

processes.
• State Retention: Perfect for tasks requiring persistent data, such as timers, counters, or step-based logic.
• Scalability: FBs organize code for large projects, reducing ladder clutter and easing maintenance.
• GX Works3 Edge: Macro Type FBs are lightweight for inline, stateless logic, while Subroutine Type FBs excel for reusable, stateful modules, unlike GX Works2’s less differentiated FBs.

Practical Guidance
• Functions: Use for quick tasks like calculating averages or converting units. In GX Works3, leverage structured text for precise coding.
• FBs: Use for control logic like timers or state machines. In GX Works3, choose Macro Type for simple sequences and Subroutine Type for complex, reusable modules.
• Testing: Use GX Works3’s simulator to test Functions/FBs before deployment.

Where would one create Functions or Function Blocks?
In GX Works 3:

In GX Works 2: