Structured Text PLC Programming: 7 Essential Coding Rules

Share:
PLC
Structured Text PLC Programming: 7 Essential Coding Rules

Ladder logic is great for interlocks, but maths, loops and recipes quickly turn into pages of rungs. A few lines of text based code can do the same job with far better clarity and reuse.

IEC 61131 3 IF and CASE FOR Loops Function Blocks

This language is the high level, Pascal like language of IEC 61131 3. It suits calculations, data handling and state machines that are awkward to draw in ladder logic.

Hello everyone, today we are going to learn structured text PLC programming, its syntax, statements and data types, and the coding rules that keep text based PLC programs easy to read and maintain.

What Is Structured Text?

Structured text is a high level textual PLC language defined in IEC 61131 3, with a syntax similar to Pascal. It is one of the five standard PLC programming languages, alongside ladder diagram, function block diagram, instruction list and sequential function chart.

PLC Academy notes that PLCopen maintains the language inside IEC 61131 3. Code runs top to bottom every scan, just like rungs in ladder.

Program flow of a text based PLC routine executing line by line each scan
Image credit: PLC Academy

Each statement ends with a semicolon, and assignments use the := operator. Execution still follows the PLC scan cycle, so outputs update after the program finishes.

Rockwell, Siemens SCL, Codesys, Beckhoff and Schneider all support the language with small dialect differences. Code written carefully can move between platforms with little rework.

Core Syntax and Statements

ElementExamplePurpose
AssignmentMotor := Start AND NOT Stop;Set a value
IFIF Level > 80.0 THEN Pump := TRUE; END_IF;Conditional logic
CASECASE Step OF 1: Fill := TRUE; END_CASE;State machines
FORFOR i := 1 TO 10 DO Sum := Sum + A[i]; END_FOR;Loops over arrays
WHILEWHILE x < 100 DO x := x * 2; END_WHILE;Conditional loop
Comment(* Tank fill logic *)Documentation

Comparison and maths operators replace separate instructions such as those in PLC compare instructions and PLC math instructions. One line can hold what needs several rungs.

CASE statements are ideal for sequences, because each step becomes a numbered branch. That makes a batch or machine sequence very easy to follow.

Data Types and Variables

PLC Academy lists 8 integer types from SINT to ULINT and 2 floating types, REAL and LREAL. See PLC data types for their ranges and memory use.

Declare variables with clear names and types in the variable table. Mixing INT and REAL without conversion is a common compile error.

Timers and Counters in Text Code

Timers are called as function block instances, for example TON_1(IN := Start, PT := T#5S);. The behaviour matches the TON, TOF and RTO timers used in ladder.

Counters work the same way, with CU, R and PV inputs, as explained in CTU and CTD counters. Call each instance only once per scan to avoid confusing results.

7 Essential Structured Text Coding Rules

1
Name Clearly
Use names like TankLevel, not N7_12.
2
One Job per Routine
Split code into functions and function blocks.
3
Comment Intent
Explain why, not just what.
4
Avoid Endless Loops
Bound every WHILE loop to protect the watchdog.
5
Use CASE for Sequences
Keep step logic visible and numbered.
6
Handle Bad Inputs
Check ranges before dividing or scaling.
7
Keep Ladder for Interlocks
Maintenance staff read simple ladder faster.

A long WHILE loop can exceed the scan time and fault the controller, explained in PLC watchdog timer. Always add an exit counter.

Many plants mix languages, with interlocks in ladder and calculations in text. Choose the language that makes each piece easiest to troubleshoot.

Worked Example: Analog Scaling

EU := (Raw minus RawMin) × (EUMax minus EUMin) ÷ (RawMax minus RawMin) + EUMin;

Example, 4 to 20 mA input on a 16 bit card:
RawMin = 6242, RawMax = 31208, Raw = 18725
EUMin = 0 bar, EUMax = 10 bar
EU = 12483 × 10 ÷ 24966 = 5.0 bar

The same formula is described in PLC analog scaling. In text form it fits on one readable line, and it can be wrapped in a reusable function.

Add a check for RawMax equal to RawMin, or the division will fault the processor.

Structured Text vs Ladder Logic

Text Language Strengths
  • Compact maths and data handling.
  • Easy loops and arrays.
  • Clean state machines with CASE.
  • Portable between vendors.
Ladder Strengths
  • Visual power flow for electricians.
  • Easy online monitoring of contacts.
  • Simple interlocks.
  • Familiar to maintenance teams.

Beginners usually start with ladder logic, then add text routines for calculations. Function block diagrams sit between the two, see function block programming.

Scaling Calculator

Raw Count to Engineering Units
Result
EU = 5.000

Use the result to check your code during simulation before downloading. Compare it with the value shown on the HMI.

PDH Online Structured Text Course PDF

PDF
Structured Text Programming, Course E334
PDH Online course by Anthony K. Ho covering syntax and examples

Studio 5000 ST Tutorial Video

Structured Text FAQ

What is structured text in a PLC?
It is the high level textual language of IEC 61131 3, similar to Pascal. It handles maths, loops and state machines more compactly than ladder rungs.
Which PLCs support it?
Rockwell, Siemens, Codesys based controllers, Beckhoff and Schneider all support it. Siemens calls its version SCL, and small dialect differences exist between vendors.
How does a CASE statement help?
It turns each step of a sequence into a numbered branch. Operators and engineers can then follow the machine state without reading dozens of rungs.
Can loops fault the PLC?
Yes, an unbounded WHILE or long FOR loop can exceed the watchdog time. Always limit loop counts and keep heavy work out of fast tasks.
How are timers used?
Timers are function block instances called with inputs such as IN and PT. They behave exactly like TON, TOF and RTO blocks in ladder logic.
Is it harder to troubleshoot?
It can be for technicians used to ladder, because contacts are not drawn. Good comments, clear names and online watch tables make fault finding much easier.
Should I replace ladder completely?
Usually not, since mixed programs work best. Keep interlocks in ladder and move calculations, data handling and sequences into text routines.

Related Articles

External References

What We Learn Today

  • Structured text is the Pascal like language of IEC 61131 3.
  • IF, CASE and loops make maths and sequences compact.
  • Clear names, bounded loops and comments keep code maintainable.
I hope you like above blog. There is no cost associated in sharing the article in your social media. Thanks for reading!! Happy Learning!!

Leave a Reply

Your email address will not be published. Required fields are marked *