Logix 5000 Controllers Structured Text: Programming Manual
Logix 5000 Controllers Structured Text: Programming Manual
Logix 5000 Controllers Structured Text: Programming Manual
Structured Text
1756 ControlLogix, 1756 GuardLogix, 1769 CompactLogix,
1769 Compact GuardLogix, 1789 SoftLogix, 5069
CompactLogix, 5069 Compact GuardLogix, Studio 5000
Logix Emulate
ATTENTION: Identifies information about practices or circumstances that can lead to personal injury or death, property damage, or economic loss.
Attentions help you identify a hazard, avoid a hazard, and recognize the consequence.
IMPORTANT Identifies information that is critical for successful application and understanding of the product.
BURN HAZARD: Labels may be on or inside the equipment, for example, a drive or motor, to alert people that surfaces may reach dangerous
temperatures.
ARC FLASH HAZARD: Labels may be on or inside the equipment, for example, a motor control center, to alert people to potential Arc Flash. Arc Flash
will cause severe injury or death. Wear proper Personal Protective Equipment (PPE). Follow ALL Regulatory requirements for safe work practices and
for Personal Protective Equipment (PPE).
Global changes
This table identifies changes that apply to all information about a subject in
the manual and the reason for the change. For example, the addition of new
supported hardware, a software design change, or additional reference
material would result in changes to all of the topics that deal with that subject.
Change Topic
Updated Legal notices. Legal notices on page 8
Chapter 1
Program Structured Text Structured Text Syntax.............................................................................. 11
Structured Text Components: Assignments...................................... 12
Specify a non-retentive assignment ............................................. 13
Assign an ASCII character to a string data member ................... 14
Character string literals ................................................................ 14
Structured Text Components: Expressions ....................................... 15
Use arithmetic operators and functions ...................................... 16
Use relational operators ................................................................ 17
Use logical operators ..................................................................... 19
Use bitwise operators .................................................................... 19
Determine the order of execution ............................................... 20
Structured Text Components: Instructions...................................... 20
Structured Text Components: Constructs ......................................... 21
IF_THEN .............................................................................................. 22
CASE_OF.............................................................................................. 25
FOR_DO ............................................................................................... 27
WHILE_DO ......................................................................................... 29
REPEAT_UNTIL ................................................................................... 31
Structured Text Components: Comments ......................................... 34
Index
Legal notices Rockwell Automation publishes legal notices, such as privacy policies, license
agreements, trademark disclosures, and other terms and conditions on
the Legal Notices page of the Rockwell Automation website.
Structured Text Syntax Structured text is a textual programming language that uses statements to
define what to execute.
• Structured text is not case sensitive.
• Use tabs and carriage returns (separate lines) to make your structured
text easier to read. They have no effect on the execution of the
structured text.
Structured text is not case sensitive. Structured text can contain these
components.
Term Definition Examples
Assignment Use an assignment statement to assign values to tags. The := operator is the tag := expression;
assignment operator.
Terminate the assignment with a semi colon ‘;.’
Expression An expression is part of a complete assignment or construct statement. An
expression evaluates to a number (numerical expression), a String (string
expression), or to a true or false state (BOOL expression)
Tag Expression A named area of the memory where data is stored (BOOL, SINT, INT, DINT, REAL, value1
String).
Immediate A constant value 4
Expression
Operators A symbol or mnemonic that specifies an operation within an expression. tag1 + tag2
Expression tag1 >= value1
Function When executed, a function yields one value. Use parentheses to contain the function(tag1)
Expression operand of a function.
Even though their syntax is similar, functions differ from instructions in that
functions can be used only in expressions. Instructions cannot be used in
expressions.
Instruction An instruction is a standalone statement. instruction();
An instruction uses parentheses to contain its operands.
Depending on the instruction, there can be zero, one, or multiple operands. instruction(operand);
When executed, an instruction yields one or more values that are part of a data
structure. Terminate the instruction with a semi colon(;). instruction(operand1, operand2,operand3);
Even though their syntax is similar, instructions differ from functions in that
instructions cannot be used in expressions. Functions can be used only in
expressions.
Construct A conditional statement used to trigger structured text code (that is, other IF...THEN CASE FOR...DO WHILE...DO
statements). Terminate the construct with a semi colon (;). REPEAT...UNTIL
EXIT
See also
Structured Text Components: Assignments on page 12
Structured Text Components: Expressions on page 15
Structured Text Components: Instructions on page 20
Structured Text Components: Constructs on page 21
Structured Text Components: Comments on page 34
Structured Text Use an assignment to change the value stored within a tag. An assignment
has this syntax:
Components: Assignments
tag := expression;
where:
Component Description
Tag Represents the tag that is getting the new value; the tag must be a BOOL, SINT, INT,
DINT, STRING, or REAL.
Tip: The STRING tag is applicable to CompactLogix 5380, CompactLogix 5480,
ControlLogix 5580, Compact GuardLogix 5380, and GuardLogix 5580 controllers only.
:= Is the assignment symbol
Expression Represents the new value to assign to the tag
If tag is this data type Use this type of expression
BOOL BOOL
SINT Numeric
INT
DINT
REAL
STRING String type, including string tag and string
(CompactLogix 5380, CompactLogix literal
5480, ControlLogix 5580, Compact (CompactLogix 5380, CompactLogix 5480,
GuardLogix 5380, and GuardLogix ControlLogix 5580, Compact GuardLogix 5380,
5580 controllers only). and GuardLogix 5580 controllers only).
; Ends the assignment
The tag retains the assigned value until another assignment changes the
value.
The expression can be simple, such as an immediate value or another tag
name, or the expression can be complex and include several operators and
functions, or both. Refer to Expressions for more information.
See also
Assign an ASCII character to a string data member on page 14
Specify a non-retentive assignment on page 13
Structured Text Components: Expressions on page 15
Character string literals on page 14
Specify a non-retentive The non-retentive assignment is different from the regular assignment
described above in that the tag in a non-retentive assignment is reset to zero
assignment each time the controller:
• Enters the Run mode
• Leaves the step of an SFC if you configure the SFC for Automatic reset.
This applies only if you embed the assignment in the action of the step
or use the action to call a structured text routine by using a JSR
instruction.
A non-retentive assignment has this syntax:
tag [:=] expression ;
where:
Component Description
tag Represents the tag that is getting the new value; the tag must be a BOOL,
SINT, INT, DINT, STRING, or REAL.
Tip: The STRING tag is applicable to CompactLogix 5380, CompactLogix
5480, ControlLogix 5580, Compact GuardLogix 5380, and GuardLogix 5580
controllers only.
[:=] Is the non-retentive assignment symbol.
expression Represents the new value to assign to the tag.
If tag is this data type Use this type of expression
BOOL BOOL
SINT Numeric
INT
DINT
REAL
See also
Assign an ASCII character to a string data member on page 14
Structured Text Components: Assignments on page 12
To add or insert a string of characters to a string tag, use either of these ASCII
string instructions:
To Use this instruction
Add characters to the end of a string CONCAT
Insert characters into a string INSERT
See also
Structured Text Components: Expressions on page 15
Character string literals on page 14
Character string literals Character string literals include single byte or double byte encoded
characters. A single-byte string literal is a sequence of zero or more characters
that are prefixed and terminated by the single quote character ('). In single
byte character strings, the three-character combination of the dollar sign ($)
followed by two hexadecimal digits is interpreted as the hexadecimal
representation of the eight-bit character code as shown in the following table.
Tip: Character string literals are only applicable to the CompactLogix 5380, CompactLogix 5480,
ControlLogix 5580, Compact GuardLogix 5380, and GuardLogix 5580 controllers.
Studio 5000 only supports single byte characters.
Tip: The newline character provides an implementation-independent means of defining the end of
a line of data for both physical and file I/O; for printing, the effect is that of ending a line of data
and resuming printing at the beginning of the next line.
The $' combination is only valid inside single quoted string literals.
See also
Structured Text Components: Assignments on page 12
See also
Use arithmetic operators and functions on page 16
Use relational operators on page 17
Use logical operators on page 19
Use bitwise operators on page 19
Use arithmetic operators Combine multiple operators and functions in arithmetic expressions.
The table provides examples for using arithmetic operators and functions.
Use this format Example
For this situation Write
value1 operator value2 If gain_4 and gain_4_adj are DINT tags and your specification says: gain_4_adj := gain_4+15;
‘Add 15 to gain_4 and store the result in gain_4_adj’"
operator value1 If alarm and high_alarm are DINT tags and your specification says: alarm:= -high_alarm;
‘Negate high_alarm and store the result in alarm.’
function(numeric_expression) If overtravel and overtravel_POS are DINT tags and your overtravel_POS := ABS(overtravel);
specification says: ‘Calculate the absolute value of overtravel and
store the result in overtravel_POS.’
value1 operator (function((value2+value3)/2) If adjustment and position are DINT tags and sensor1 and sensor2 position := adjustment + ABS((sensor1
are REAL tags and your specification says: ‘Find the absolute value +
of the average of sensor1 and sensor2, add the adjustment, and sensor2)/2);
store the result in position.’
See also
Structured Text Components: Expressions on page 15
Use relational operators Relational operators compare two values or strings to provide a true or false
result. The result of a relational operation is a BOOL value.
If the comparison is The result is
True 1
False 0
See also
Structured Text Components: Expressions on page 15
Use logical operators Use logical operators to verify if multiple conditions are true or false. The
result of a logical operation is a BOOL value.
If the comparison is The result is
true 1
false 0
See also
Structured Text Components: Expressions on page 15
Use bitwise operators Bitwise operators manipulate the bits within a value based on two values.
The following provides an overview of the bitwise operators.
For Use this operator Optimal data type
bitwise AND &, AND DINT
bitwise OR OR DINT
bitwise exclusive OR XOR DINT
bitwise complement NOT DINT
See also
Structured Text Components: Expressions on page 15
Determine the order The operations written into an expression perform in a prescribed order.
See also
Structured Text Components: Expressions on page 15
Structured Text Structured text statements can also be instructions. A structured text
instruction executes each time it is scanned. A structured text instruction
Components: Instructions within a construct executes every time the conditions of the construct are
true. If the conditions of the construct are false, the statements within the
construct are not scanned. There is no rung-condition or state transition that
triggers execution.
This differs from function block instructions that use EnableIn to trigger
execution. Structured text instructions execute as if EnableIn is always set.
This also differs from ladder diagram instructions that use rung-condition-in
to trigger execution. Some ladder diagram instructions only execute when
rung- condition-in toggles from false to true. These are transitional ladder
IF (osri_1.OutputBit) THEN
ABL(0,serial_control);
END_IF;
Components: Constructs If
Doing something if or when specific conditions occur
Use this construct
IF. . . THEN
Selecting what to do based on a numerical value CASE. . . OF
Doing something a specific number of times before doing anything else FOR. . . DO
Continuing doing something when certain conditions are true WHILE. . . DO
Continuing doing something until a condition is true REPEAT. . . UNTIL
See also
IF_THEN on page 22
CASE_OF on page 25
FOR_DO on page 27
WHILE_DO on page 29
REPEAT_UNTIL on page 31
Operands
IF bool_expression THEN
<statement>;
Operand Type Format Enter
Bool_ BOOL Tag expression BOOL tag or expression that evaluates to a BOOL
expression value
(BOOL expression)
Description
The syntax is described in the table.
Major/Minor Faults
None.
Examples
Example 1
IF…THEN
If performing this Enter this structured text
IF rejects > 3 then IF rejects > 3 THEN
Example 2
IF_THEN_ELSE
If performing this Enter this structured text
If conveyor direction contact = forward (1) then IF conveyor_direction THEN
light = off light := 0;
Otherwise light = on ELSE
light [:=] 1;
END_IF;
The [:=] tells the controller to clear light whenever the controller does the
following :
Enters the RUN mode.
Leaves the step of an SFC if you configure the SFC for Automatic reset. (This
applies only if you embed the assignment in the action of the step or use the
action to call a structured text routine via a JSR instruction.)
Example 3
IF…THEN…ELSIF
If performing this Enter this structured text
If sugar low limit switch = low (on) and sugar high limit switch = not IF Sugar.Low & Sugar.High THEN
high
(on) then
inlet valve = open (on) Sugar.Inlet [:=] 1;
Until sugar high limit switch = high (off ) ELSIF NOT(Sugar.High) THEN
Sugar.Inlet := 0;
The [:=] tells the controller to clear Sugar.Inlet whenever the controller does
the following :
Enters the RUN mode.
Leaves the step of an SFC if you configure the SFC for Automatic reset. (This
applies only if you embed the assignment in the action of the step or use the
action to call a structured text routine via a JSR instruction.)
Example 4
IF…THEN…ELSIF…ELSE
If performing this Enter this structured text
If tank temperature > 100 IF tank.temp > 200 THEN
then pump = slow pump.fast :=1; pump.slow :=0; pump.off :=0;
If tank temperature > 200 ELSIF tank.temp > 100 THEN
then pump = fast pump.fast :=0; pump.slow :=1; pump.off :=0;
Otherwise pump = off ELSE
pump.fast :=0; pump.slow :=0; pump.off :=1;
END_IF;
Operands
CASE numeric_expression OF
selector1: statement;
selectorN: statement; ELSE
Structured Text
Operand Type Format Enter
Numeric_ SINT INT DINT Tag expression Tag or expression that evaluates to a
expression REAL number
(numeric expression)
Selector SINT INT DINT Immediate Same type as numeric_expression
REAL
IMPORTANT If using REAL values, use a range of values for a selector because a REAL value is
more likely to be within a range of values than an exact match of one, specific value.
Description
The syntax is described in the table.
Major/Minor Faults
None
Example
If you want this Enter this structured text
If recipe number = 1 then Ingredient A outlet 1 = open (1) Ingredient B CASE recipe_number OF
outlet 4 = open (1) 1:
Ingredient_A.Outlet_1 :=1; Ingredient_B.Outlet_4 :=1;
If recipe number = 2 or 3 then 2,3:
Ingredient_A.Outlet_4 :=1; Ingredient_B.Outlet_2 :=1;
Ingredient A outlet 4 = open (1)
Ingredient B outlet 2 = open (1)
If recipe number = 4, 5, 6, or 7 then Ingredient A outlet 4 = open (1) 4 to 7: Ingredient_A.Outlet_4 :=1; Ingredient_B.Outlet_2 :=1;
Ingredient B outlet 2 = open (1)
If recipe number = 8, 11, 12, or 13 then Ingredient A outlet 1 = open (1) 8,11…13
Ingredient B outlet 4 = open (1) Ingredient_A.Outlet_1 :=1; Ingredient_B.Outlet_4 :=1;
Otherwise all outlets = closed (0) ELSE
Ingredient_A.Outlet_1 [:=]0; Ingredient_A.Outlet_4 [:=]0; Ingredient_B.Outlet_2 [:=]0;
Ingredient_B.Outlet_4 [:=]0;
END_CASE;
The [:=] tells the controller to also clear the outlet tags whenever the controller
does the following:
Enters the RUN mode.
Leaves the step of an SFC if configuring the SFC for Automatic reset. This
applies only embedding the assignment in the action of the step or using the
action to call a structured text routine via a JSR instruction.
FOR_DO Use the FOR_DO loop to perform an action a number of times before doing
anything else.
When enabled, the FOR instruction repeatedly executes the Routine until the
Index value exceeds the Terminal value. The step value can be positive or
negative. If it is negative, the loop ends when the index is less than the
terminal value.. If it is positive, the loop ends when the index is greater than
the terminal value.
Each time the FOR instruction executes the routine, it adds the Step size to
the Index.
Do not loop too many times in a single scan. An excessive number of
repetitions causes the controller watchdog to timeout and causes a major
fault.
Operands
FOR count:= initial_value TO
final_value BY increment DO
<statement>;
END_FOR;
IMPORTANT Do not iterate within the loop too many times in a single scan.
The controller does not execute other statements in the routine until it completes the
loop.
A major fault occurs when completing the loop takes longer than the watchdog timer
for the task.
Consider using a different construct, such as IF_THEN.
Description
The syntax is described in the table.
This diagrams illustrates how a FOR_DO loop executes, and how an EXIT
statement leaves the loop early.
The FOR_DO loop executes a specific number of times. To stop the loop before the count reaches the last value, use
an EXIT statement.
Major/Minor Faults
A major fault will occur if Fault type Fault code
The construct loops too long. 6 1
Example 1
If performing the following, Enter this structured text
Clear bits 0…31 in an array of BOOLs: For subscript:=0 to 31 by 1 do
Initialize the subscript tag to 0. array[subscript] := 0;
Clear i . For example, when subscript = 5, clear array[5]. End_for;
Add 1 to subscript.
If subscript is ≤ to 31, repeat 2 and 3.
Otherwise, stop.
Example 2
If performing the following, Enter this structured text
A user-defined data type (structure) stores the following information SIZE(Inventory,0,Inventory_Items);
about an item in your inventory: For position:=0 to Inventory_Items - 1 do
• Barcode ID of the item (String data type) If Barcode = Inventory[position].ID then
• Quantity in stock of the item (DINT data type)
Quantity := Inventory[position].Qty;
An array of the above structure contains an element for each different
Exit;
item in your inventory. You want to search the array for a specific
End_if;
product (use its bar code) and determine the quantity that is in stock.
1. Get the size (number of items) of the Inventory array and store the End_for;
result in
2. Inventory_Items (DINT tag).
Initialize the position tag to 0.
3. If Barcode matches the ID of an item in the array, then:
Set the Quantity tag = Inventory[position].Qty. This produces the quantity
in stock of the item.
Stop.
Barcode is a string tag that stores the bar code of the item for which you
are searching. For example, when
position = 5, compare Barcode to Inventory[5].ID.
4. Add 1 to position.
5. If position is ≤ to (Inventory_Items -1), repeat 3 and 4. Since element
numbers start at 0, the last element is 1 less than the number of
elements in the array.
Otherwise, stop.
WHILE_DO Use the WHILE_DO loop to continue performing an action while certain
conditions are true.
Operands
WHILE bool_expression DO
<statement>;
Structured Text
Operand Type Format Description
bool_expression BOOL tag BOOL tag or expression that
expression evaluates to a BOOL value
IMPORTANT Do not iterate within the loop too many times in a single scan.
The controller does not execute any other statements in the routine until it completes
the loop.
A major fault occurs when completing the loop takes longer than the watchdog timer
for the task.
Consider using a different construct, such as IF_THEN.
Description
The syntax is:
The following diagrams illustrate how a WHILE_DO loop executes, and how
an EXIT statement leaves the loop early.
While the bool_expression is true, the controller To stop the loop before the conditions are true, use an EXIT statement.
executes only the statements within the WHILE_DO loop.
Fault Conditions
A major fault will occur if Fault type Fault code
the construct loops too long 6 1
Example 1
If performing the following, Enter this structured text
The WHILE_DO loop evaluates its conditions first. If the conditions are pos := 0;
true, the controller then executes the statements within the loop. While ((pos <= 100) & structarray[pos].value <> targetvalue)) do
This differs from the REPEAT_UNTIL loop because the REPEAT_UNTIL loop pos := pos + 2;
executes the statements in the construct and then determines if the
String_tag.DATA[pos] := SINT_array[pos];
conditions are true before executing the statements again. The
end_while;
statements in a REPEAT_UNTIL loop are always executed at least once.
The statements in a WHILE_DO loop might never be executed.
Example 2
If performing the following, Enter this structured text
Move ASCII characters from a SINT array into a string tag. (In a SINT array, element_number := 0;
each element holds one character.) Stop when you reach the carriage SIZE(SINT_array, 0, SINT_array_size);
return. While SINT_array[element_number] <> 13 do
Initialize Element_number to 0. String_tag.DATA[element_number] := SINT_array[element_number];
Count the number of elements in SINT_array (array that contains the ASCII
element_number := element_number + 1;
characters) and store the result in SINT_array_size (DINT tag).
String_tag.LEN := element_number;
If the character at SINT_array[element_number] = 13 (decimal value of
the carriage return), then stop. If element_number = SINT_array_size then
Set String_tag[element_number] = the character at exit;
SINT_array[element_number]. end_if;
Add 1 to element_number. This lets the controller check the next end_while;
character in SINT_array.
Set the Length member of String_tag = element_number. (This records
the number of characters in String_tag so far.)
If element_number = SINT_array_size, then stop. (You are at the end of
the array and it does not contain a carriage return.)
Operands
REPEAT
<statement>;
Structured Text
Operand Type Format Enter
bool_ BOOL Tag expression BOOL tag or expression that evaluates to a BOOL
expression value
(BOOL expression)
IMPORTANT Do not iterate within the loop too many times in a single scan.
The controller does not execute other statements in the routine until it completes the
loop.
A major fault occurs when completing the loop takes longer than the watchdog timer
for the task.
Consider using a different construct, such as IF_THEN.
Description
The syntax is:
The following diagrams show how a REPEAT_UNTIL loop executes and how
an EXIT statement leaves the loop early.
While the bool_expression is false, the controller executes only the statements
within the REPEAT_UNTIL loop.
Fault Conditions
A major fault will occur if Fault type Fault code
Example 1
If performing the following, Enter this structured text
The REPEAT_UNTIL loop executes the statements in the construct and pos := -1;
then determines if the conditions are true before executing the REPEAT
statements again. This differs from the WHILE_DO loop because the pos := pos + 2;
WHILE_DO The WHILE_DO loop evaluates its conditions first.
UNTIL ((pos = 101) OR (structarray[pos].value = targetvalue))
If the conditions are true, the controller then executes the statements
end_repeat;
within the loop. The statements in a REPEAT_UNTIL loop are always
executed at least once. The statements in a WHILE_DO loop might never
be executed.
Example 2
If performing the following, Enter this structured text
Move ASCII characters from a SINT array into a string tag. (In a SINT array, element_number := 0;
each element holds one character.) Stop when you reach the carriage SIZE(SINT_array, 0, SINT_array_size);
return. Repeat
Initialize Element_number to 0. String_tag.DATA[element_number] := SINT_array[element_number];
Count the number of elements in SINT_array (array that contains the ASCII
element_number := element_number + 1;
characters) and store the result in SINT_array_size (DINT tag).
String_tag.LEN := element_number;
If element_number = SINT_array_size then
Structured Text To make your structured text easier to interpret, add comments to it.
Components: Comments • Comments let you use plain language to describe how your structured
text works.
• Comments do not affect the execution of the structured text.
For example:
Format Example
//comment At the beginning of a line
//Check conveyor belt direction
IF conveyor_direction THEN...
At the end of a line
ELSE //If conveyor isn’t moving, set alarm light
light := 1;
END_IF;
Index W
WHILE?DO 29
A
arithmetic operators 16
ASCII character 14
assign ASCII character 14
B
bitwise operators 19
C
CASE 25
comments 34
E
evaluation in structured text 17
evaluation of strings 17
F
FOR?DO 27
functions 16
I
IF...THEN 22
L
logical operators 19
N
non-retentive 13
non-retentive assignment 13
R
relational operators 17
REPEAT?UNTIL 31
S
structured text 13, 16, 17, 19, 20, 34
structured text assignment 14
Structured Text Components
Literature Library Find installation instructions, manuals, brochures, and technical data publications. rok.auto/literature
Product Compatibility and Download Center Get help determining how products interact, check features and capabilities, and rok.auto/pcdc
(PCDC) find associated firmware.
Documentation feedback
Your comments help us serve your documentation needs better. If you have any suggestions on how to improve our content, complete the form
at rok.auto/docfeedback.
Rockwell Automation maintains current product environmental information on its website at rok.auto/pec.
Allen-Bradley, expanding human possibility, Logix, Rockwell Automation, and Rockwell Software are trademarks of Rockwell Automation, Inc.
Trademarks not belonging to Rockwell Automation are property of their respective companies.
Rockwell Otomayson Ticaret A.Ş. Kar Plaza İş Merkezi E Blok Kat:6 34752, İçerenkÖy, İstanbul, Tel: +90 (216) 5698400 EEE YÖnetmeliğine Uygundur