0
0
FreertosHow-ToBeginner · 3 min read

How to Call Function Block in Structured Text | PLC Programming

In Structured Text, you call a function block by declaring an instance of it and then using that instance with input and output variables. Use the syntax InstanceName : FunctionBlockName; to declare and then call it by assigning inputs and reading outputs.
📐

Syntax

To call a function block in Structured Text, first declare an instance of the function block type. Then, use the instance name to call the block with input and output variables.

  • InstanceName: A variable name for the function block instance.
  • FunctionBlockName: The name of the function block type.
  • Use the instance to assign inputs and read outputs.
structured-text
VAR
  MyFB : FunctionBlockName;  // Declare instance
END_VAR

// Call function block
MyFB(Input1 := Value1, Input2 := Value2);

// Read output
OutputValue := MyFB.Output;
💻

Example

This example shows how to declare and call a function block named TimerFB that has an input Start and an output Done. It demonstrates calling the block and reading its output.

structured-text
FUNCTION_BLOCK TimerFB
VAR_INPUT
  Start : BOOL;
END_VAR
VAR_OUTPUT
  Done : BOOL;
END_VAR
VAR
  TimerCounter : INT;
END_VAR

// Simple timer logic
IF Start THEN
  TimerCounter := TimerCounter + 1;
  IF TimerCounter > 5 THEN
    Done := TRUE;
  ELSE
    Done := FALSE;
  END_IF
ELSE
  TimerCounter := 0;
  Done := FALSE;
END_IF
END_FUNCTION_BLOCK


PROGRAM PLC_PRG
VAR
  MyTimer : TimerFB;  // Declare instance
  TimerDone : BOOL;
  StartTimer : BOOL := TRUE;
END_VAR

// Call function block
MyTimer(Start := StartTimer);

// Read output
TimerDone := MyTimer.Done;
⚠️

Common Pitfalls

Common mistakes when calling function blocks in Structured Text include:

  • Not declaring an instance before calling the function block.
  • Trying to call the function block type directly instead of an instance.
  • Forgetting to assign input variables when calling the block.
  • Confusing function blocks with functions (function blocks maintain state, functions do not).
structured-text
(* Wrong: calling function block type directly *)
// FunctionBlockName(Start := TRUE);  // This is invalid

(* Right: declare instance and call it *)
VAR
  FBInstance : FunctionBlockName;
END_VAR

FBInstance(Start := TRUE);
📊

Quick Reference

Remember these key points when calling function blocks:

  • Always declare an instance variable of the function block type.
  • Call the function block by using the instance name with input assignments.
  • Access outputs through the instance's output variables.
  • Function blocks keep internal state, so the instance is important.

Key Takeaways

Declare an instance of the function block before calling it.
Call the function block using the instance name with inputs assigned.
Access outputs through the instance's output variables after calling.
Function blocks maintain state, so each instance is unique.
Never call the function block type directly without an instance.