Skip to main content
Notice

Structured Text works with select PLC CPUs. Ensure your PLC supports Structured Text.

Structure of ST Language

Identifiers

PLC Data Registers

In Structured Text programs, PLC data registers can be used directly. Data registers can be used on the right or left side of an expression. Additionally, they can be used in functions. It is similar to PLC data registers, but there is a prefix between the data register type and address, which indicates a BOOL and WORD.

PLC Data Registers
ItemDescription
None1-bit data register
X1-bit data register
WWORD (16-bit) data register
PLC Data Registers Example
ExampleResult
MX00 := 1
The value of M0.0 is 1 (turns M0.0 ON)
MX01:=1
The value of M0.1 is 1 (turns M0.1 ON)
DW100:=100
The value of D100 is 100
DW101:=0X100
The value of D101 is 0x100 (Hexadecimal)
  • Data register mapping:
    • X, Y, M, L, K, F: None, X, W
    • T, C, S: None, X
    • D: X, W
    • Z, TC, TS, CC, CS: W

Variables

A variable is a string made up of the accessible characters in a Structured Text program. There are two types of variables: Local Variable and Global Variable.

Local Variables are used within one Structured Text program. Global Variables can be used by any Structured Text program.

When registering Global Variables, it is possible to designate a specific PLC data register. Designating a specific PLC data register is known as memory allocation. If a PLC data register is not designated, internal memory will be allocated automatically.

Naming Rules

  • Characters allows: alphanumeric, Korean, and ' '.
  • Reserved WORDs cannot be used.
  • Maximum length of 32 bytes.
    • If using Korean, maximum 16 characters.
  • Variables are NOT case-sensitive.

Data Types

Variable Data Types
Name (Reserved)Data TypeBit Size
SINTShort Integer8
INTInteger16
DINTDouble Integer32
USINTUnsigned Short Integer8
UINTUnsigned Integer16
UDINTUnsigned Double Integer32
REALFloat/Real Number32
BOOLBoolean1
BYTEBit string length of 88
WORDBit string length of 1616
DWORDBit string length of 3232
ARRAYArray-
STRUCTStruct-

Expressions

An expression is a construct which is composed of operators and operands. The table below describes the list of operators and their priority.

Operators
OperationSymbolsPriority
Brackets()1
Parameter list of the functionFUNCTION_NAME(Param1, Param2, ...)2
Index**2
Singular Operator (Code)+, -3
ComplementNOT3
Multiplication*4
Division/4
ModularMOD4
Addition+5
Subtraction-5
Comparison<, >, <=, >=6
Equality=7
Inequality<>7
Logical Product&, AND8
Exclusive ORXOR9
Logical SumOR10
  • The operator with the higher priority will be operated first, and the operator with the next priority will be operated after.
    • This procedure will repeat until the calculation ends.
  • When operators with the same priority are used, the calculation is operated from left to right.

Arithmetic Operators

+ Operator

  • The + operator adds the values on the left and right side of the operator.
  • Format: Expression1 + Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Example
ExampleResult
DW500 := DW00 + 10;
D500 = D00 + 10
DW00 := 20;
DW01 := 30;
DW10 := DW00 + DW01;
D10 = 50

- Operator

  • The - operator subtracts the values on the left and right side of the operator.
  • Format: Expression1 - Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Example
ExampleResult
DW500 := 20 - 10;
D500 = 10
DW00 := 30;
DW01 := 20;
DW10 := DW00 - DW01;
D10 = 10

* Operator

  • The * operator multiplies the values on the left and right side of the operator.
  • Format: Expression1 * Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Example
ExampleResult
DW00 := 3;
DW01 := 2;
DW10 := DW00 * DW01;
D10 = 6

/ Operator

  • The / operator divides the values on the left and right side of the operator.
  • Format: Expression1 / Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Example
ExampleResult
DW00 := 10;
DW01 := 2;
DW10 := DW00 / DW01;
D10 = 5

Dividing integers outputs the resulting integer. For example, DW10 := 3 / 2 results in D10 = 1.

MOD Operator

  • The MOD operator divides the values on the left and right side of the operator and save the remainder.
  • Format: Expression1 + Expression2
    • Expression1 must be positive.
    • Expression2 CANNOT be 0.
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Example
ExampleResult
DW00 := 10;
DW01 := 3;
DW10 := DW00 MOD DW01;
D10 = 1
(10/3 = 1)

** Operator

  • The ** operator multiplies the values on the left side of the operator to the power of the value on the right side of the value.
  • Format: Expression1 ** Expression2
    • Expression1Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Example
ExampleResult
DW00 := 10;
DW01 := 3;
DW10 := DW00 ** DW01;
D10 = 1,000
(103 = 1,000)

Comparison Operators

= Operator

  • The = operator compares if two expressions are equal.
  • Format: Expression1 = Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
001
010
100
111
Example
ExampleResult
IF MX00 = 1 THEN
YX2A := 1;
END_IF;
If M0.0 = 1, then Y2.A = 1

<> Operator

  • The <> operator compares if two expressions are not equal.
  • Format: Expression1 ≠ Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
000
011
101
110
Example
ExampleResult
IF MX00 <> 1 THEN
YX2A := 1;
END_IF;
If M0.0 ≠ 1, then Y2.A = 1

< Operator

  • The < operator compares if the value on the left side is less than the value on the right side.
  • Format: Expression1 < Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
000
011
100
110
Example
ExampleResult
WHILE DW00 < 5 DO
DW00 := DW00 + 1;
DW10 := DW10 + DW00;
END_WHILE;
While D0 is less than 5, D0 = D0 + 1 and D10 = D10 + D0

> Operator

  • The > operator compares if the value on the left side is greater than the value on the right side.
  • Format: Expression1 > Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
000
010
101
110
Example
ExampleResult
WHILE DW00 < 10 DO
IF DW00 > 5 THEN
EXIT;
END_IF;
DW10 := DW10 + DW00;
DW00 := DW00 + 1;
END_WHILE;
While D0 is less than 10, D10 = D10 + D0 and D0 = D0 + 1. If D0 is greater than 5, exit the while loop

<= Operator

  • The <= operator compares if the value on the left side is less than or equal to the value on the right side.
  • Format: Expression1 ≤ Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
001
011
100
111
Example
ExampleResult
WHILE DW00 <= 5 DO
DW00 := DW00 + 1
DW10 := DW10 + DW00;
END_WHILE;
While D0 is less than or equal to 5, D0 = D0 + 1 and D10 = D10 + D0

>= Operator

  • The >= operator compares if the value on the left side is greater than or equal to the value on the right side.
  • Format: Expression1 ≥ Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
001
010
101
111
Example
ExampleResult
IF DW00 = 0 THEN
DW10 = 10;
END_IF;
WHILE DW10 >= 1 DO
DW00 := DW00 + DW10;
DW10 := DW10 - 1;
END_WHILE;
If D0 = 0, then D10 = 10. While D10 is greater than or equal to 1, D0 = D0 + D10 and D10 = D10 - 1

Logical Operators

AND (&) Operator

  • The AND or & operator compares the bits of two operands. If the correlated bits are both 1, the result bit is 1.
  • Format: Expression1 AND Expression2 or Expression1 & Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
000
010
100
111
Example
ExampleResult
MX00 := 1;
MX01 := 0;
MX10 := MX00 AND MX01;
M1.0 = 0
(M0.0 & M0.1 = 0)

OR Operator

  • The OR operator compares the bits of two operands. If the correlated bits are both 1, the result bit is 1.
  • Format: Expression1 OR Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
000
011
101
111
Example
ExampleResult
MX00 := 1;
MX01 := 0;
MX10 := MX00 OR MX01;
M1.0 = 1
(M0.0 OR M0.1 = 1)

XOR Operator

  • The XOR operator compares the bits of two operands. If one of the correlated bits are 1, the result bit is 1. If the correlated bits are both 1, the result is 0.
  • Format: Expression1 XOR Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Expression2Result
000
011
101
110
Example
ExampleResult
MX00 := 1;
MX01 := 1;
MX10 := MX00 XOR MX01;
M1.0 = 0
(M0.0 XOR M0.1 = 0)

NOT Operator

  • The NOT operator compares the bits of two operands. If the bit of the operand is 1, the result is 0. If the bit of the operand is 0, the result is 1.
  • Format: Expression1 XOR Expression2
  • In Expression1 and Expression2, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Truth Table
Expression1Result
01
10
Example
ExampleResult
MX00 := NOT 0;
M0.0 = 1

Statements

Assignment Statement

Assignment statements substitutes a value or a result from a calculation into the variable or PLC data register. The PLC data register where the constant, the value of a variable or the result of a calculation, has to be placed on the left side of the := operator. All assignment statements must end with ;.

  • Format: Identifier := Expression
    • Identifier can be a variable or PLC data register.
  • In Expression, a constant, PLC data register, and the expressions listed in the Operators table can be used.
Example
ExampleResult
MX00 := 1;
The value of M0.0 is 1
DW100 := 100;
The value of D100 is 100
DW500 := DW00 + DW01;
The value of D500 is D0 + D1

Branch Statement

IF Statement

IF statements perform the operations in the statement if the condition expression result is true.

If the IF statement's condition is false and an ELSEIF statement exists, the condition of the ELSEIF statement is checked.

If all conditions are false, none of the command statements are performed unless there an ELSE statement exists.

Format

IF conditional expression THEN
  logic
ELSEIF conditional expression THEN
  logic
ELSE
  logic
END_IF;

  • conditional expressions can be composed of comparison operators or logical operators.
  • There can be multiple logic statements.
  • Only 1 IF and ELSE statement each can exist. Multiple ELSEIF statements can exist.
Example
// If D1's value is 0, assign 1 to D100
IF DW01 = 0 THEN
DW100 := 1;
// If D1's value is 1, assign 2 to D100
ELSEIF DW01 = 1 THEN
DW100 := 2;
// If D1's value is less than 5, assign 3 to D100
ELSE IF DW01 < 5 THEN
DW100 := 3;
// If D1's value is not 0 or 1 and equal to or greater than 5, assign 0 to D100
ELSE
DW100 := 0;
END_IF;

CASE Statement

CASE statements branch the following operations according the expression's value.

Format

CASE expression OF
  integer option : logic
  integer option : logic
ELSE
  logic
END_CASE;

  • expression must be an integer.
  • integer option can be a singular integer, or multiple integers, or a range of integers.
Integer Option
Integer Option typesResult
1When the expression's value is 1
2, 3, 4When the expression's value is 2, 3, or 4
5..10When the expression's value is 5, 6, 7, 8, 9, or 10
1, 2..5, 10When the expression's value is 1, 2, 3, 4, 5, or 10
  • There can be multiple logic statements.
  • ELSE can only be used once or not all.
Example
CASE DW10 OF
//If D10's value is 4, assign 4 to D0
4:
DW00 := 4;
// If D10's value is between 5 and 6 or 7, assign 5 to D0
5..6, 7:
DW00 := 5;
// If D10's value is 4 or between 5 and 6 or not 7, assign 0 to D0
ELSE
DW := 0;
END_CASE;

Repetitive Statement

FOR Statement

FOR statements are performed to process the repetitive operations. This statement can only be used when the number of loops is definitive. If not, WHILE or REPEAT statements should be used.

If too many repetition statements are used, the scan time will increase and performance will decrease.

Format

FOR init expression TO expression BY expression DO
  logic
END_FOR;

  • init expression is used to initialize the repetitive variables with an assignment statement. On the left side of the := operator, the variable or PLC data register should exist. On the right side of the := operator, the integer to initialize the variable or PLC data register should exist.
  • The expression that follows TO is the final number of the repetitive variable. The integer value must be used, and the logic statements will be performed only when the repetitive variable's value is less than or equal to this expression.
  • The expression that follows BY is the number that increases every time a loop of logic statements is performed. This can be omitted or an integer can be located. The value increases by 1 as default.
  • There can be multiple logic statements.
Example
// This example adds all the values from 1 to 10
// Repetitive variable D0's increases 1 every loop
// The statement loops 10 times
// Adds the values from 1 to 10 and stores them in D100
// After the FOR statement ends, D100's value is 55
FOR DW00 := 1 TO 10 BY 1 DO
DW100 := DW100 + DW00;
END_FOR;

WHILE Statement

WHILE statements perform a repetitive operation. WHILE statements are not limited to the number of repetitions like FOR statements.

The logic statements in a WHILE statement will be performed while the conditional expression's result is true. It will be performed forever if the conditional expression's result never becomes false.

If too many repetition statements are used, the scan time will increase and performance will decrease.

Format

WHILE conditional expression DO
  logic
END_WHILE;

Example
// This example adds all the values from 0 to 5
// The WHILE statement is operated while D0's value is less than 5
// D10's value will be 15 after the WHILE statement terminates
WHILE DW00 < 5 DO
DW00 := DW00 + 1;
DW10 := DW10 + DW00;
END_WHILE;

The WHILE statement's conditional expression is always true. This runs forever.
In this case, the PLC might disconnect, error LEDs blinking, etc.
Refer here to resolve the issue.

WHILE DW00 >= 0 DO
DW00 := DW00 + 1;
DW10 := DW10 + DW00;
END_WHILE;

REPEAT Statement

REPEAT statements perform a repetitive operation. REPEAT statements are not limited to the number of repetitions like FOR statements. REPEAT statements loop performs while the conditional expression after UNTIL is false. The loop will perform forever if the conditional expression doesn't become true.

Unlike the WHILE statement, the REPEAT statement operates the logic statements once, even if the conditional expression is true.

If too many repetition statements are used, the scan time will increase and performance will decrease.

Format

REPEAT
  logic
...
UNTIL conditional expression
END_REPEAT;

  • logic statements are performed while conditional expression is false.
  • The REPEAT statements terminates when conditional expression is true.
  • There can be multiple logic statements.
Example
// This example adds all the values from 0 to 5
// If D0's value is not equal to 5, REPEAT statement is performed
// D10's value will be 15 when REPEAT statement terminates
REPEAT
DW00 := DW00 + 1;
DW10 := DW10 + DW00;
UNTIL DW00 = 5
END_REPEAT;

The conditional expression after UNTIL is always false.
The REPEAT statement runs forever.
In this case, the PLC might disconnect, error LEDs blinking, etc.
Refer here to resolve the issue.

REPEAT
DW00 := DW00 + 1;
DW10 := DW10 + DW00;
UNTIL DW00 < 0
END_REPEAT;

Other Statements

EXIT Statement

The EXIT statement is used to escape from repetitive statements before they end. When an EXIT statement is performed, the nearest repetitive statement is cancelled.

Example
// WHILE statement is performed while D0 is less than 10
// If D0 is greater than 5, the <b>logic</b> statement in the IF statement is performed
// This will assign 10 to D0 and exit the WHILE statement
WHILE DW00 < 10 DO
IF DW00 > 5 THEN
DW00 := 10;
EXIT;
END_IF;
DW10 := DW10 + DW00;
DW00 := DW00 + 1;
END_WHILE;

RETURN Statement

The RETURN statement is used to terminate the current process in the Structured Text program. When the RETURN statement is performed, the logic statements that follows the RETURN statement will be ignored, and the process will jump from the RETURN statement to the last part of the Structured Text program.

Example
// If D0's value is greater than 5, assign 10 to D0 and end
// The IF statement below satisfies the condition that D0 = D10
// It isn't performed because of the RETURN
IF DW00 > 5 THEN
DW00 := 10;
RETURN;
END_IF;
IF DW00 = 10 THEN
DW10 := 100;
END_IF;

Comment

Comments make the Structured Text program readable and easy to understand. The comments are not compiled, and they do not affect the program.

// comments the current line (* *) comments the current line and all text in between

Example
// The logic below is commented:
// MX01 := 1;
// The logic below is commented:
(*
MX01 := 1;
MX02 := 2;
MX03 := 3;
MX04 := 4;
*)

Application Instructions

The Structured Text language shares the application instructions with the Ladder Diagram program.

The input window of application instructions are identical to the Ladder Diagram program's. This can be open by typing F10 on the keyboard.
Enter the instruction and operand like the Ladder Diagram program and left-click OK. The instructions will automatically be converted into Structured Text format.

Auto-complete is supported.

Input Option

Open the Options window by left-click Tool and left-clicking Options.

Left-click ST Program and check Enter the full form when auto-completing Instruction.

This option automatically enters commas and parentheses that match the number of operands.