Structured Tex Programming Manual Logix 5000
Structured Tex Programming Manual Logix 5000
Activities including installation, adjustments, putting into service, use, assembly, disassembly, and maintenance are required to be carried out by suitably trained personnel in
accordance with applicable code of practice.
If this equipment is used in a manner not specified by the manufacturer, the protection provided by the equipment may be impaired.
In no event will Rockwell Automation, Inc. be responsible or liable for indirect or consequential damages resulting from the use or application of this equipment.
The examples and diagrams in this manual are included solely for illustrative purposes. Because of the many variables and requirements associated with any particular installation,
Rockwell Automation, Inc. cannot assume responsibility or liability for actual use based on the examples and diagrams.
No patent liability is assumed by Rockwell Automation, Inc. with respect to use of information, circuits, equipment, or software described in this manual.
Reproduction of the contents of this manual, in whole or in part, without written permission of Rockwell Automation, Inc., is prohibited.
Throughout this manual, when necessary, we use notes to make you aware of safety considerations.
WARNING: Identifies information about practices or circumstances that can cause an explosion in a hazardous environment, which may lead to personal injury or
death, property damage, or economic loss.
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.
These labels may also be on or inside the equipment to provide specific precautions.
SHOCK HAZARD: Labels may be on or inside the equipment, for example, a drive or motor, to alert people that dangerous voltage may be present.
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).
Tip: Identifies information that is useful and can help to make a process easier to do or easier to understand.
Rockwell Automation recognizes that some of the terms that are currently used in our industry and in this publication are not in alignment with the movement toward inclusive
language in technology. We are proactively collaborating with industry peers to find alternatives to such terms and making changes to our products and content. Please excuse the
use of such terms in our content while we implement these changes.
Grammatical and editorial style changes are not included in this summary.
Global changes
None in this release.
Change Topic
Corrected the examples in the CASE_OF statement section. CASE_OF on page 23
IF_THEN...............................................................................................................................................................................................................................................................................................21
CASE_OF............................................................................................................................................................................................................................................................................................. 23
FOR_DO...............................................................................................................................................................................................................................................................................................25
WHILE_DO.......................................................................................................................................................................................................................................................................................... 28
REPEAT_UNTIL.................................................................................................................................................................................................................................................................................. 30
This manual provides a programmer with details about the available General, Motion, Process, and Drives instruction set for a Logix-based controller.
If you design, program, or troubleshoot safety applications that use GuardLogix controllers, refer to the GuardLogix Safety Application Instruction Set Safety Reference Manual,
publication 1756-RM095.
This manual is one of a set of related manuals that show common procedures for programming and operating Logix 5000 controllers.
For a complete list of common procedures manuals, refer to the Logix 5000 Controllers Common Procedures Programming Manual, publication 1756-PM001.
The term Logix 5000 controller refers to any controller based on the Logix 5000 operating system. Rockwell Automation recognizes that some of the terms that are currently used
in our industry and in this publication are not in alignment with the movement toward inclusive language in technology. We are proactively collaborating with industry peers to find
alternatives to such terms and making changes to our products and content. Please excuse the use of such terms in our content while we implement these changes.
The Studio 5000 Automation Engineering & Design Environment® combines engineering and design elements into a common environment. The first element is the Studio 5000
Logix Designer® application. The Logix Designer application is the rebranding of RSLogix 5000® software and will continue to be the product to program Logix 5000™ controllers for
discrete, process, batch, motion, safety, and drive-based solutions.
The Studio 5000® environment is the foundation for the future of Rockwell Automation® engineering design tools and capabilities. The Studio 5000 environment is the one place for
design engineers to develop all elements of their control system.
Additional resources
These documents contain additional information concerning related Rockwell Automation products.
Resource Description
LOGIX 5000 Controllers Program Parameters Programming Manual publication 1756-PM021 Describes how to use program parameters when programming Logix 5000 controllers.
LOGIX 5000 Controllers General Instructions Reference Manual, publication 1756-RM003 Describes the available instructions for a Logix 5000 controller.
LOGIX 5000 Controllers Process and Drives Instructions Reference Manual, publication 1756-RM006 Describes how to program a Logix 5000 controller for process or drives applications.
LOGIX 5000 Controllers Motion Instruction Set Reference Manual, publication MOTION-RM002 Describes how to program a Logix 5000 controller for motion applications.
Product Certifications website, http://ab.rockwellautomation.com Provides declarations of conformity, certificates, and other certification details.
You can view or download publications at . To order paper copies of technical documentation, contact your local Rockwell Automation distributor or sales representative.
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.
You can view a full list of all open source software used in this product and their corresponding licenses by opening the index.html file located your product's OPENSOURCE folder on
your hard drive.
You may obtain Corresponding Source code for open source packages included in this product from their respective project web site(s). Alternatively, you may obtain complete
Corresponding Source code by contacting Rockwell Automation via the Contact form on the Rockwell Automation website: http://www.rockwellautomation.com/global/about-
us/contact/contact.page. Please include "Open Source" as part of the request text.
Structured text is not case sensitive. Structured text can contain these components.
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
only.
BOOL BOOL
SINT Numeric
INT
DINT
REAL
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.
Tip: I/O module data updates asynchronously to the execution of logic. If you reference an input multiple
times in your logic, the input could change state between separate references. If you need the input to
have the same state for each reference, buffer the input value and reference that buffer tag. For more
information, see Logix 5000 Controllers Common Procedures, publication 1756-PM001.
You can also use Input and Output program parameters which automatically buffer the data during the
Logix Designer application execution. See LOGIX 5000 Controllers Program Parameters Programming
Manual, publication 1756-PM021.
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
only.
BOOL BOOL
SINT Numeric
INT
DINT
REAL
To add or insert a string of characters to a string tag, use either of these ASCII string instructions:
1 Dollar sign $$
3 Line feed $L or $I
4 Newline $N or $n
6 Carriage return $R or $r
7 Tabulator $T or $t
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.
L LINT -9,223,372,036,854,775,808
to 9,223,372,036,854,775,808
U UDINT 0 to 4,294,967,295
UL ULINT 0 to 18,446,744,073,709,551,615
• Use any combination of upper-case and lower-case letter. For example, these variations of "AND" are
acceptable: AND, And, and.
• For more complex requirements, use parentheses to group expressions within expressions. This makes the
whole expression easier to read, and ensures that the expression executes in the desired sequence.
BOOL expression: An expression that produces the BOOL value of 1 (true) or 0 (false).
• A bool expression uses bool tags, relational operators, and logical operators to compare values or check if
conditions are true or false. For example, tag1>65.
• A simple bool expression can be a single BOOL tag.
• Typically, use bool expressions to condition the execution of other logic.
• A numeric expression uses arithmetic operators, arithmetic functions, and bitwise operators. For example,
tag1+5.
• Nest a numeric expression within a BOOL expression. For example, (tag1+5)>65.
If Use
Functions perform math operations. Specify a constant, a non-Boolean tag, or an expression for the function.
The table provides examples for using arithmetic operators and functions.
value1 operator value2 If gain_4 and gain_4_adj are DINT tags gain_4_adj := gain_4+15;
and your specification says:
‘Add 15 to gain_4 and store the result in
gain_4_adj’"
operator value1 If alarm and high_alarm are DINT tags alarm:= -high_alarm;
and your specification says:
‘Negate high_alarm and store the result in
alarm.’
value1 operator If adjustment and position are DINT tags position := adjustment + ABS((sensor1 +
(function((value2+value3)/2) and sensor1 and sensor2 are REAL tags sensor2)/2);
and your specification says: ‘Find the
absolute value of the average of sensor1
and sensor2, add the adjustment, and
store the result in position.’
True 1
False 0
value1 operator value2 If temp is a DINT tag and your IF temp<100 THEN...
specification says: ‘If temp is less than
100⋅ then…’
stringtag1 operator stringtag2 If bar_code and dest are string tags and IF bar_code=dest THEN...
your specification says: ‘If bar_code
equals dest then…’
stringtag1 operator 'character string If bar_code is a string tag and your IF bar_code=’Test PASSED’ THEN...
literal' specification says: ‘If bar_code equals
’Test PASSED’ then...’
char1 operator char2 If bar_code is a string tag and your IF bar_code.DATA[0]=65 THEN...
To enter an ASCII character directly into specification says: ‘If bar_code.DATA[0]
the expression, enter the decimal value of equals ’A’ then…’
the character.
bool_tag := bool_expressions If count and length are DINT tags, done is Done := (count >= length);
a BOOL tag, and your specification says:
‘If count is greater than or equal to length,
you are done counting.’
• When the two strings are sorted as in a telephone directory, the order of the strings determines which one is
greater.
true 1
false 0
logical OR OR BOOL
NOT BOOLtag If photoeye is a BOOL tag and your IF NOT photoeye THEN...
specification says: "If photoeye is off
then..."
expression1 & expression2 If photoeye is a BOOL tag, temp is a IF photoeye & (temp<100) THEN...
DINT tag, and your specification says: "If
photoeye is on and temp is less than 100
then..."
expression1 XOR expression2 If photoeye1 and photoeye2 are BOOL tags IF photoeye1 XOR photoeye2 THEN...
and your specification says: "If:
photoeye1 is on while photoeye2 is off or
photoeye1 is off while photoeye2 is on
then..."
BOOLtag := expression1 & expression2 If photoeye1 and photoeye2 are BOOL open := photoeye1 & photoeye2;
tags, open is a BOOL tag, and your
specification says: "If photoeye1 and
photoeye2 are both on, set open to true"
bitwise OR OR DINT
This is an example.
value1 operator value2 If input1, input2, and result1 are DINT tags result1 := input1 AND input2;
and your specification says: "Calculate
Order Operation
1 ()
2 function (...)
3 **
4 - (negate)
5 NOT
6 *,/,MOD
7 +,- (subtract)
8 <,<=,>,>=
9 =,<>
10 &,AND
11 XOR
12 OR
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
diagram instructions. In structured text, instructions execute when they are scanned unless pre-conditioning the
execution of the structured text instruction.
For example, the ABL instruction is a transitional instruction in ladder diagram. In this example, the ABL instruction
only executes on a scan when tag_xic transitions from cleared to set. The ABL instruction does not execute when
tag_xic stays set or when tag_xic clears.
END_IF;
The ABL instruction will execute every scan that tag_xic is set, not just when tag_xic transitions from cleared to set.
If you want the ABL instruction to execute only when tag_xic transitions from cleared to set, you have to condition the
structured text instruction. Use a one-shot to trigger execution.
osri_1.InputBit := tag_xic;
OSRI(osri_1);
IF (osri_1.OutputBit) THEN
ABL(0,serial_control);
END_IF;
• GOTO
• REPEAT
Logix Designer application will not let you use them as tag names or constructs.
IF_THEN
Use IF_THEN to complete an action when specific conditions occur.
Operands
IF bool_expression THEN
<statement>;
Description
The syntax is described in the table.
To select from several possible groups of statements, add one or more ELSIF statements.
The controller executes the first true IF or ELSIF and skips the rest of the ELSIFs and the ELSE.
To do something when all of the IF or ELSIF conditions are false, add an ELSE statement.
The table summarizes different combinations of IF, THEN, ELSIF, and ELSE.
Major/Minor Faults
None.
Examples
Example 1
IF…THEN
END_IF;
Example 2
IF_THEN_ELSE
light [:=] 1;
END_IF;
The [:=] tells the controller to clear light whenever the controller does the following :
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 sugar low limit switch = low (on) and sugar high limit switch = IF Sugar.Low & Sugar.High THEN
not high
(on) then
Until sugar high limit switch = high (off ) ELSIF NOT(Sugar.High) THEN
Sugar.Inlet := 0;
END_IF;
The [:=] tells the controller to clear Sugar.Inlet whenever the controller does the following :
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
END_IF;
CASE_OF
Use CASE_OF to select what to do based on a numerical value.
Operands
CASE numeric_expression OF
selector1: statement;
Structured Text
Operand Type Format Enter
Numeric_ SINT INT DINT REAL Tag expression Tag or expression that
expression evaluates to a number
(numeric expression)
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.
The CASE construct is similar to a switch statement in the C or C++ programming languages. With the CASE construct,
the controller executes only the statements that associated with the first matching selector value. Execution always
breaks after the statements of that selector and goes to the END_CASE statement.
Major/Minor Faults
None
Example
If you want this Enter this structured text
If recipe number = 4, 5, 6, or 7 then Ingredient A outlet 4 = open 4..7: Ingredient_A.Outlet_4 :=1; Ingredient_B.Outlet_2 :=1;
(1) Ingredient B outlet 2 = open (1)
END_CASE;
The [:=] tells the controller to also clear the outlet tags whenever the controller does the following:
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;
initial_ value SINT INT DINT Tag expression Immediate Must evaluate to a number
Specifies initial value for count
final_ value SINT INT DINT Tag expression Immediate Specifies final value for count,
which determines when to exit
the loop
increment SINT INT DINT Tag expression Immediate (Optional) amount to increment
count each time through the
loop
If you don’t specify an
increment, the count
increments by 1.
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.
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
Example 1
If performing the following, Enter this structured text
Example 2
If performing the following, Enter this structured text
for a specific product (use its bar code) and determine the End_if;
quantity that is in stock. End_for;
1. Get the size (number of items) of the Inventory array and
store the result in
2. Inventory_Items (DINT tag).
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
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.
Description
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
executes only the statements within the WHILE_DO loop. statement.
Fault Conditions
A major fault will occur if Fault type Fault code
Example 1
If performing the following, Enter this structured text
Example 2
If performing the following, Enter this structured text
Move ASCII characters from a SINT array into a string tag. (In a element_number := 0;
SINT array, each element holds one character.) Stop when you SIZE(SINT_array, 0, SINT_array_size);
reach the carriage return.
While SINT_array[element_number] <> 13 do
Initialize Element_number to 0.
String_tag.DATA[element_number] :=
Count the number of elements in SINT_array (array that
SINT_array[element_number];
contains the ASCII characters) and store the result in
SINT_array_size (DINT tag). element_number := element_number + 1;
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.)
REPEAT_UNTIL
Use the REPEAT_UNTIL loop to continue performing an action until conditions are true.
Operands
REPEAT
<statement>;
Structured Text
Operand Type Format Enter
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.
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.
To stop the loop before the conditions are false, use an EXIT statement.
Fault Conditions
A major fault will occur if Fault type Fault code
Example 1
If performing the following, Enter this structured text
Example 2
If performing the following, Enter this structured text
Move ASCII characters from a SINT array into a string tag. (In a element_number := 0;
SINT array, each element holds one character.) Stop when you SIZE(SINT_array, 0, SINT_array_size);
reach the carriage return.
Repeat
Initialize Element_number to 0.
String_tag.DATA[element_number] :=
Count the number of elements in SINT_array (array that
SINT_array[element_number];
contains the ASCII characters) and store the result in
SINT_array_size (DINT tag). element_number := element_number + 1;
Add 1 to element_number. This lets the controller check the next exit;
character in end_if;
• Comments let you use plain language to describe how your structured text works.
• Comments do not affect the execution of the structured text.
that spans more than one line (*start of comment. . .end of comment*)
/*start of comment. . .end of comment*/
For example:
Format Example
• If ... Then
• Elsif ... Then
• Case ... Of
• For ... Do
• While ... Do
• Repeat ... Until
• #region ... #endregion
When automatic outlining is enabled, the editor creates a collapsible outline segment when a line starts with
#region, if, while, repeat, for, and case, and ends with the matching end construct, such as #endregion or end_if.
View the contents of a collapsed segment without expanding it by hovering the cursor over the segment. To expand
and collapse all the segments in a routine at once, on the main menu, select Edit > Outlining > Expand all outlining
or Edit > Outlining > Collapse all outlining.
To navigate between associated matching keywords for the keyword at the cursor location, select Search >
Matching Keyword, or press Ctrl+].
Tip: The #region . . . #endregion construct does not execute, so it can precede an SBR
instruction at the beginning of a routine.
Tip: The #region . . . #endregion construct can be used as part of a statement within a built-in
construct, such as a CASE_OF statement. The following example shows the #region . . . #endregion
construct in a CASE_OF statement, with regions expanded and collapsed:
Technical Support Center Find help with how-to videos, FAQs, chat, user forums, and product notification updates. rok.auto/support
Local Technical Support Phone Numbers Locate the telephone number for your country. rok.auto/phonesupport
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 find rok.auto/pcdc
(PCDC) 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.
Rockwell Otomasyon Ticaret A.Ş. Kar Plaza İş Merkezi E Blok Kat:6 34752 İçerenköy, İstanbul, Tel: +90 (216) 5698400 EEE Yönetmeliğine Uygundur