ADC Community_02 (Automationdirect.com) asked a question.

Productivity PROtips: Structured Text Fundamentals

Structured Text Programming Fundamentals

Syntax Basics • Statements & Expressions • Control Flow • Best Practices

 

 

Overview

Structured Text (ST) is a high-level, text-based programming language available in Productivity Suite. It's similar to the IEC 61131-3 standard, but not identical, and it lets you write powerful expressions and logic using statements and operators instead of building the same logic out of ladder rungs.

 

This article walks through the fundamentals of ST programming: how to create an ST task, the core terminology, syntax rules, and the building blocks (constants, operators, conditionals, loops, arrays, and function calls) you'll use in almost every routine. For details on the ST Editor's tools, menus, and keyboard shortcuts, see the Related Topics at the end of this article.

 

Creating a Structured Text Task

New ST tasks are created from the Task Management panel a new ST task is created by default for new projects:

•     Right-click a task folder (such as Run Every Scan) in the Task Management panel.

•     Select New Task.

•     Enter a name for the task.

•     Under Task Type, choose Structured Text instead of Ladder.

•     Select OK.

 

Note: Once a task is created, it cannot be converted between Ladder and Structured Text. Choose the task type carefully before you start writing code.

 

Key Structured Text Terminology

Before writing any code, it helps to know a handful of terms that show up throughout ST programming:

 

imageimageFunction Block Example

A timer function block instance has four elements: ET (elapsed time), IN (timer enable input), PT (preset time), and Q (timer done, i.e. ET >= PT).

  1. timer1(IN := timer1_enable, PT := timer1_preset);

Note: A tag instance of a function block's tag structure must be created before that function block instance can be used in Structured Text.

 

Syntax Basics

ST syntax defines how code must be written so the CPU can understand and execute it — everything from keywords and punctuation to spacing conventions.

•     Every complete statement must end with a semicolon.

•     Control flow statements must include the full structure (for example, an IF needs its matching END_IF). A toolbar button has been provided in the ST editor that will insert full structures into the code.

•     A single statement or control flow structure can span multiple lines.

  1. IF
  2. Tag1 = 1
  3. THEN
  4. Tag2 := 5;
  5. END_IF

Tags such as system tags that contain characters ST doesn't normally allow (such as spaces) can still be used if you wrap them in curly braces and prefix them with a dollar sign. For example, to reference the "2 Second Bit" system tag:

  1. ${2 Second Bit}

Advanced functions must be preceded by an @ character. For example, @CALL runs a task in the Run When Called folder, and @UDI instantiates a User Defined Instruction. Both are configured through the Edit Instruction dialog (right-click, or press Shift + Space).

 

Constants

Structured Text supports the following constant types:

 

imageOperators and Precedence

Operators follow a defined order of precedence, from highest to lowest:

imageNote: Operators of equal precedence evaluate left to right. It's good practice to use parentheses to make sure operations happen in the order you intend.

 

Assignment Statements

Assignment uses the := operator to store the result of an expression in a tag.

  1. counter := counter + 1; //adds 1 to counter and assigns the new value to counter
  2. speed := RPM * 0.1047; //multiplies RPM by 0.1047 and assigns the result to speed

Note: Multiple assignments are allowed on the same line as long as the data types match. For example, int3 := int2 := int1 + 5; assigns the value of int1 + 5 to both int2 and int3.

 

Conditional Statements

IF Statement

  1. IF condition THEN
  2. statements;
  3. ELSIF condition THEN
  4. statements;
  5. ELSE
  6. statements;
  7. END_IF;

CASE Statement

  1. CASE variable OF
  2. 1: statement; // single condition
  3. 2, 3, 11: statement; // comma-separated condition list
  4. 5..10: statement; // condition range from 5 to 10
  5. ELSE
  6. statement;
  7. END_CASE;

Looping Statements

FOR Loop

  1. FOR i := 1 TO 10 BY 1 DO
  2. statements;
  3. END_FOR;

WHILE Loop

  1. WHILE condition DO
  2. statements;
  3. END_WHILE;

Note: Use the EXIT keyword inside a FOR, WHILE, or REPEAT loop to end its execution early.

 

Caution: Time-based functions such as TON, TOF, and TP may not behave as expected inside FOR, WHILE, or REPEAT loops.

 

Arrays

Structured Text uses bracket notation for array elements, and array indexing is 1-based (the first element is index 1, not 0), just like it is in ladder.

  1. 1D_array[1] := 100 //assigns 100 to the first element of 1D_array
  2. 2D_array[3,2] := 50 //assigns 50 to the element at row 3, column 2 of 2D_array

Formal parameter lists spell out which parameter each value belongs to, which makes longer or more complex function calls easier to read.

 

Tag Requirements

Tags used in ST code cannot:

•     Contain operator symbols (e.g. +, -, =, *, /, \, #, @, ^, %, <, >)

•     Contain spaces

•     Contain brackets [ ] or braces { }

•     Contain semicolons

•     Use reserved keywords (IF, THEN, PI, TRUE, etc.)

 

A tag must also exist in the Tag Database, or it can be created directly from the editor. Productivity Suite does not support local variable declaration in the ST code.

 

Note: Undefined tags appear underlined in red. Once syntax errors are corrected, clicking Validate (or pressing Shift + F8) opens the Define Tags dialog so you can create and define new tags.

 

Compile and Error Handling

•     Errors are underlined in red.

•     Navigation buttons let you move between errors.

•     Incorrect syntax or undefined tags will prevent a successful compile.

 

Note: Dividing by zero does not generate a compile error — the result will simply be zero (0).

Note: Structured Text can affect PLC scan time differently than equivalent Ladder logic. Conditional commands like IF, FOR, WHILE, REPEAT, and CASE may take more processing time than their ladder counterparts, especially for very simple logic.

 

Editor Preferences

The ST editor can be customized – text and background colors, font sizes, etc. – by going to the Tools > Options dialog and making any desired adjustments in the ‘Structured Text’ tab.

Best Practices

•     Use indentation for readability.

•     Group related logic into structured blocks.

•     Use meaningful tag names.

•     Comment complex logic sections.

•     Use formal parameter lists for clarity in complex instructions.

 

Quick Reference

For additional details, refer to the Productivity Suite Help topics below:

 

Related Topics

•     Structured Text Editor (topic P343)

•     Structured Text Programming Fundamentals (topic P346)

•     Structured Text Function List (topic P345)

•     Tool > Options (topic P082)