Why Choose ST for Writing FB?
-
Clear Logic and Easy Maintenance The ST language expresses logic in text form, making it suitable for describing complex control processes. With a reasonable code structure and comments, developers can easily understand and maintain the code.
-
Modular Programming FB itself is a modular programming unit, and ST allows developers to define inputs, outputs, internal variables, and logic within the FB, achieving functional encapsulation. This modular design greatly enhances code reusability.
-
Efficient Handling of Complex Algorithms For scenarios requiring mathematical calculations, state machines, or multi-condition judgments, ST’s syntax support (such as IF-ELSE, CASE, FOR loops, etc.) enables developers to implement logic in a more intuitive way.
-
Cross-Platform Compatibility The ST language adheres to the IEC 61131-3 standard, providing good cross-platform compatibility, suitable for various PLC brands (such as Siemens, Rockwell, Schneider, etc.).
How to Write an Efficient FB Using ST?
To illustrate with a simple example: suppose we need to design an FB to control the start-stop logic of a motor, including runtime statistics and fault detection functionality.
Step 1: Define the Inputs and Outputs of the FB
In the FB, we first need to clarify the inputs, outputs, and internal variables. For example:
FUNCTION_BLOCK MotorControl
VAR_INPUT
Start: BOOL; // Start signal
Stop: BOOL; // Stop signal
Reset: BOOL; // Reset signal
END_VAR
VAR_OUTPUT
MotorOn: BOOL; // Motor running status
Fault: BOOL; // Fault status
RunTime: TIME; // Runtime
END_VAR
VAR
LastStart: BOOL; // Record last start status
Timer: TON; // Timer
FaultCounter: INT; // Fault count
END_VAR
Step 2: Write the ST Logic
In the main body of the FB, use ST language to implement the motor’s start-stop, timing, and fault detection logic:
IF Start AND NOT LastStart THEN
MotorOn := TRUE; // Start the motor
Timer.IN := TRUE; // Start the timer
END_IF;
IF Stop THEN
MotorOn := FALSE; // Stop the motor
Timer.IN := FALSE; // Stop the timer
END_IF;
IF Reset THEN
Timer.IN := FALSE; // Reset the timer
Timer.PT := T#0S; // Clear the timer
FaultCounter := 0; // Clear the fault count
END_IF;
// Fault detection: Trigger fault if runtime exceeds 10 minutes
IF Timer.Q THEN
Fault := TRUE;
FaultCounter := FaultCounter + 1;
END_IF;
RunTime := Timer.ET; // Update runtime
LastStart := Start; // Update start status
This code implements the following functionalities:
-
When the
Startsignal is triggered, the motor starts and begins timing. -
When the
Stopsignal is triggered, the motor stops, and timing pauses. -
When the
Resetsignal is triggered, the timer and fault count are cleared. -
If the runtime exceeds the set value (e.g., 10 minutes), a fault signal is triggered, and the fault count is recorded.
Step 3: Save and Compile
In the PLC programming environment (such as TIA Portal or Codesys), save and compile the FB, ensuring there are no syntax errors. The compiled FB can be called multiple times.
Using FBD to Call FB: An Intuitive and Efficient Implementation
FBD is a graphical programming language that expresses control flow intuitively by connecting function blocks graphically. Embedding the FB written in ST into FBD can fully leverage the advantages of both: ST provides powerful logic processing capabilities, while FBD makes the calling and debugging process more intuitive.
Step 1: Create an FBD Program
Create an FBD program block in the PLC programming software and drag the previously written MotorControl FB into the program editing area.
Step 2: Connect Inputs and Outputs
In FBD, the MotorControl FB will appear as a rectangular block with input and output pins. Developers need to connect the PLC’s input signals (such as buttons, sensors) to the FB’s input pins (Start, Stop, Reset), and connect the output pins (MotorOn, Fault, RunTime) to the corresponding output devices or variables.
For example:
-
Connect the physical input
I0.0(start button) toStart. -
Connect the physical input
I0.1(stop button) toStop. -
Connect
MotorOnto the physical outputQ0.0(motor relay). -
Connect
RunTimeto a global variable for displaying runtime.
Step 3: Multi-Instance Calling
Another advantage of FBD is the support for multi-instance calling of FBs. Suppose there are multiple motors in the factory, each requiring an independent MotorControl FB instance; simply drag the MotorControl FB into FBD multiple times and assign different variables for each instance. For example:
Motor1: MotorControl; // FB instance for Motor 1
Motor2: MotorControl; // FB instance for Motor 2
In FBD, connect the input and output signals of Motor1 and Motor2 to achieve multi-motor control.
Step 4: Debugging and Optimization
In FBD, you can use monitoring features to view the input and output status of the FB in real-time, intuitively checking whether the logic is correct. For example, observe whether RunTime accumulates correctly and whether Fault triggers under expected conditions. If issues are found, you can return to the ST code for modifications.
Advantages of Combining ST for Writing FB and FBD for Calling
Modularity and Reusability
FBs written in ST can be called multiple times, reducing repetitive coding work. For example, the same MotorControl FB can be used to control all motors in the factory, requiring only independent instances for each motor.
Intuitive Visualization
FBD presents signal flow graphically, allowing developers to quickly understand the logical structure of the entire control system, reducing debugging difficulty.
Efficient Team Collaboration
ST code is suitable for logic developers to write complex algorithms, while FBD is suitable for field engineers to quickly configure and debug. The combination of both can enhance team collaboration efficiency.
Easy to Expand
When new functionalities need to be added, simply modify the FB logic in ST without changing the calling structure in FBD. For example, speed control or temperature monitoring functionality can be added to the MotorControl FB.
30,000 people are using a PLC simulation software (tutorials available for download)
Tens of thousands are using circuit simulation software (available for download)
PLC physical simulation software (available for Quark download)
Siemens 200SMART physical simulation software (available for download)
Siemens 200SMART with human-machine simulation, 3 software packages (available for download)
Siemens 1500 physical simulation software (self-learning and downloadable)