This document shows several programming examples in Structured Text ST language using CODESYS.
As one of the IEC-61131 PLC programming languages, Structured Text or simply ST is based on and resembles traditional programming languages such as Pascal or Java
Like all programming languages, Structured Text has advantages and disadvantages, but if we focus on the advantages here are some:
No prior PLC programming knowledge is needed
As Structured Text is similar to traditional high-level programming languages, it can be quite easy for many people who have no prior PLC programming knowledge but do have experience in traditional coding to learn and develop PLC projects.
Some programming software (including CODESYS) allow you to combine Ladder Diagram (LD) and Structured Text (ST) in the same project
ST is a lightweight programming language
Since Structured Text is by definition text-based, it means that it does not have a memory-hungry graphical interface, which allows for smaller processor memory and therefore lower costs.
ST is text-based
In most cases, you can also develop your structured text PLC programming project without using PLC programming software. Since it is text-based, you can write your project in a simple text file and copy and paste it into your PLC project when you are ready.
Examples shown are AND/OR, IF conditions, CASE, comparison elements, calculations, WHILE loop, REPEAT loop, , FOR loop, inserting TON timers
As a final example, it is shown how to perform a traffic light control using Structured Text Language (ST) in CODESYS
Structured Text examples in CODESYS
IF / ELSIF: start and stop a motor.
IF xStart AND NOT xStop THEN
xMotor := TRUE;
ELSIF xStop THEN
xMotor := FALSE;
END_IF; CASE: a simple step sequence.
CASE iStep OF
0: IF xStart THEN iStep := 10; END_IF;
10: xValveOpen := TRUE;
IF xLevelHigh THEN iStep := 20; END_IF;
20: xValveOpen := FALSE;
iStep := 0;
END_CASE; FOR loop: add up the values of an array.
rTotal := 0.0;
FOR i := 1 TO 10 DO
rTotal := rTotal + arValues[i];
END_FOR; TON timer: switch on a lamp 5 seconds after the sensor is active.
VAR
tonDelay : TON;
END_VAR
tonDelay(IN := xSensor, PT := T#5S);
xLamp := tonDelay.Q; Download the full CODESYS ST examples
The complete document, with AND/OR logic, comparisons, calculations, WHILE and REPEAT loops and a traffic light control in Structured Text, is available below:
Related articles
- Introduction to PLC programming in CODESYS V3
- CODESYS global and persistent variables
- CODESYS hardware requirements
- CODESYS manuals and examples
