Chapter 10 Structured Texti
Chapter 10 Structured Texti
Chapter 10 Structured Texti
TABLE OF CONTENTS
Chapter 10 - 1
T530-10 Structured Text - RevG
10.1.1 Objectives
On completion of this chapter you will be able to:
• Describe the Structured Text Language rules
• Write simple application code in ST
• Use Functions and Function Blocks in ST
10.1.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Chapter 10 - 2
Compact Control Builder Training
Chapter 10 - 3
T530-10 Structured Text - RevG
Declarations pane
Code pane
Messages pane
The code pane is used for writing code in Structured Text. It is a simple text editor.
Tabs and spaces are used to structure the code for easy reading.
The code pane may be divided into several ‘tabs’. Each tab is also referred to as Code
Blocks. When the code is compiled the execution order of the code is firstly in tab
order (left to right) and then from top to bottom inside the tabs.
Instances of variables and function blocks are declared in the declaration pane.
Chapter 10 - 4
Compact Control Builder Training
Chapter 10 - 5
T530-10 Structured Text - RevG
10.3.3 Operators
Below is a list of the most commonly used operators:
Boolean Operators
Operator Description
NOT Negates the Boolean value (1/0, on/off or True/False).
AND Boolean AND.
& Boolean AND. See AND.
XOR Boolean XOR.
OR Boolean OR.
Arithmetic Operators
Operator Description
** Exponential, i.e. raising to the power.
* Multiplication.
/ Division.
+ Addition.
- Subtraction.
MOD Modulus.
Relational Operators
Operator Description
< Less than.
> Greater than.
<= Less than or equal to.
>= Greater than or equal to.
= Equal to.
<> Not equal to.
Chapter 10 - 6
Compact Control Builder Training
10.3.4 Precedence
The priority of operators decides the order of evaluation of an expression. Below is a
summary of available operators, in descending priority:
If you are unsure of the evaluation order then use parenthesis to force evaluation of
sub-expressions.
Chapter 10 - 7
T530-10 Structured Text - RevG
Chapter 10 - 8
Compact Control Builder Training
IFs may be nested to many levels. A better structure to be used instead of nesting is
the CASE structure. This is very useful when many conditions need evaluation; there
are several forms:
CASE…OF
Operator Description
CASE Integer Expression OF A statement is executed depending on the
Integer Literal1 : Statement(s); value of an integer variable or an integer
expression.
END_CASE;
The <integer literal> is one or several integer
values or one or several ranges of values.
CASE Integer Expression OF In this example, three values are tested and
Integer Literal1 : Statement(s); the appropriate statements) executed. One
variation is to permit a range of values to be
Integer Literal2 : Statement(s); tested rather than an exact single value.
Integer Literal3 : Statement(s); If none of the test literals match the result of
ELSE the expression then the statement(s) in the
ELSE clause will be executed. If no ELSE
Statement(s);
exists, none of the statements will be
END_CASE; executed.
Operator Description
FOR i := 0 to 15 DO The FOR statement is used to allow a statement (or
Statement(s); statements) to be executed repeatedly for a given
number of times. The counter used in the repetition
END_FOR; process can be used in the statements.
In the example, the statements between the FOR
and END_FOR will be executed 16 times.
WHILE Level > 80.0 DO The WHILE statement is used in order to allow a
Statement(s); statement (or statements) to be executed repeatedly
while a certain condition is True.
END_WHILE;
This separates it from the FOR statement. It has
some similarities with the REPEAT statement.
REPEAT The REPEAT statement is used in order to allow a
Statement(s); statement (or statements) to be executed repeatedly
until a certain condition is True.
UNTIL Boolean Expression
Note that the test to exit the loop is placed at the end,
END_REPEAT; so a minimum of one execution of the statement(s)
will occur even if the expression is true at the time
the loop is entered.
EXIT Use the EXIT statement whenever you want to
terminate a loop immediately and continue execution
from the first line after the iteration statement.
Chapter 10 - 9
T530-10 Structured Text - RevG
10.4.1 Functions
Functions are called inside expressions with the following syntax:
Var := FunctionName(Parameter(s))
In the example below the square root of a flow signal is calculated by using the Sqrt()
function.
The above timer function block is the same as the following in FBD form:
Function blocks are declared in a similar way to variables, by giving them a name (an
instance name). This name is then used to call them in the code. In the ST editor you
must declare function blocks explicitly in the “Function Block” declaration tab in the
editor by giving a name and a type:
Chapter 10 - 10
Compact Control Builder Training
1. Mark the “Function Blocks” tab in the declarations pane of the POU.
2. Type in an instance name for the function block in the “Name” column.
3. Type in the required function block type in the “Function Block Type” column.
Place the cursor in the field and press ‘Ctrl + J’ to see a list of all available
function block types.
Chapter 10 - 11
T530-10 Structured Text - RevG
Four variables which will be used to connect to the function block have also been
declared in the “Variables” declaration tab:
1. Call the function block instance by typing its instance name in the code pane
followed by an opening parenthesis ‘(‘
The system will then offer a dialogue for you to make connections.
2. Fill in the parameters that are to be connected to the function block instance in the
parameter column by any of the following methods:
Type the name of the variable to be connected directly into the “Parameter” field.
(After a few characters the system will try to help you finish by supplying first
matching variable name).
Chapter 10 - 12
Compact Control Builder Training
3. Use the “Insert from List” method by clicking on the Insert Variable … icon in the
menu bar or use the “Insert Path from Tree” Icon to browse the application for the
variable:
Chapter 10 - 13
T530-10 Structured Text - RevG
Double clicking on the error message line places the cursor at the end of the statement
with the problem: Usually a missing semi-colon (;)at the end of a statement.
Note also that some other items are classed as ‘variables’ by the system:
- Instance names for function blocks
- Instance names for control modules
- Sequence Step names
- Code Block names
- Sequence Transition names
- Parameters (in function block types and control module types)
This means that within any POU all of the above must have unique names.
Chapter 10 - 14
Compact Control Builder Training
2. A key word has been used - both words “On” and “Off” are reserved.
The programmer has declared the function block with the name MixTimer but has
referenced MuxTimer in the code.
Chapter 10 - 15
T530-10 Structured Text - RevG
In this case the programmer has attempted to add a “real” and a “dint” to get a “dint”
result. (You can’t multiply apples with pears!)
Chapter 10 - 16
Compact Control Builder Training
3. To get automatic declaration, mark the first error, click right and select “Declare
Variable”:
The system will copy the name into the declaration pane and also as an added
bonus will fill in the type if it can deduce it from the sense of the statement.
Chapter 10 - 17
T530-10 Structured Text - RevG
Chapter 10 - 18
Compact Control Builder Training
4. Select the “Function Diagram” in “Structured Text” tab and press then “OK”.
Chapter 10 - 19
T530-10 Structured Text - RevG
Chapter 10 - 20
Compact Control Builder Training
10.1.1 Description
Program the outflow valves in the Structured Text language. Instantiate and use
function blocks in the ST editor.
We will be using the same ValveUni function block as we did for the tank inlet valves.
Declare global and local variables similar to the ones for the inlet valves.
10.1.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Exercise 10.1 - 1
J410-10 Exercise 10.1 - RevA
F Create a new code block “Outlet_Valves” and set the language to Structured Text.
F Declare global variables for the Order value and the valve Open and Closed feedback
switches as was done for the inlet valves. Check, Save, and Close the Sxx_ReactorApp
editor.
Exercise 10.1 - 2
Compact Control Builder Training
F If you use the Oslo_Sim skip this part. Connect the variables to the corresponding
channel on the I/O modules.
• The variables gV2_Ord and gV6_Ord are connected to the first DO801 module on
the PROFIBUS. The command to open V2 is wired to channel 4 and the V6 open
command is wired to channel 2.
• The valve position feedback signals are all connected to the DI801 module located
on the PROFIBUS as follows:
Exercise 10.1 - 3
J410-10 Exercise 10.1 - RevA
F Connect the local and global variables for V2 to the Sxx_V2 function block.
Exercise 10.1 - 4
Compact Control Builder Training
F Copy Sxx_V2, paste it into the code block. Rename it to Sxx_V6 and replace the
variables for V6. Declare the new associated local variables for Sxx_V6
F Download the application to your controller and take Control Builder to the on-line
mode.
F Operate the outlet valves, V2 and V6 using Control Builder interaction windows.
Observe the reactor tank level.
Exercise 10.1 - 5
J410-10 Exercise 10.1 - RevA
Exercise 10.1 - 6
Compact Control Builder Training
10.1.1 Description
Program the outflow valves in the Structured Text language. Instantiate and use
function blocks in the ST editor.
We will be using the same ValveUni function block as we did for the tank inlet valves.
Declare global and local variables similar to the ones for the inlet valves.
10.1.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Solution 10.1 - 1
T530-10 Solution 10.1 - RevG
F Create a new code block “Outlet_Valves” and set the language to Structured Text
1. Right-click the code block tabs at the bottom of the window and select “Insert…”
Name the new code block “Outlet_Valves” and select the “Structured Text (ST)”
radio button..
2. Result
Solution 10.1 - 2
Compact Control Builder Training
F Declare global variables for the Order value and the valve Open and Closed feedback
switches as was done for the inlet valves. Check, Save, and Close the Sxx_ReactorApp
editor.
1. You can, of course, manually type the information for global variables. We will
use the Find & Replace functionality. Cell, row, and column selection in the
declaration pane of POU editors is similar to Microsoft Excel. Select the six rows
in the Sxx_ReactorApp editor that hold the V1 and V5 variables and copy them
Solution 10.1 - 3
T530-10 Solution 10.1 - RevG
3. With the newly pasted cells still highlighted, select “Edit” -> “Replace…” from
the editor’s menu bar
4. In the “Replace” dialog, replace all occurrences of “V1” with “V2”. Click
“Replace All”.
5. Click “OK”
6. Perform the same procedure to replace “V5” with “V6”. Close the “Replace”
dialog window.
Solution 10.1 - 4
Compact Control Builder Training
Solution 10.1 - 5
T530-10 Solution 10.1 - RevG
4. The valve position feedback signals are all connected to the DI801 module located
on the PROFIBUS as follows:
Solution 10.1 - 6
Compact Control Builder Training
2. You will notice that the instance “Sxx_V2” has been added in Control Builder M
under the Tank program
Solution 10.1 - 7
T530-10 Solution 10.1 - RevG
Solution 10.1 - 8
Compact Control Builder Training
F Connect the local and global variables for V2 to the Sxx_V2 function block.
1. Left click in the white Code pane select keyboard Ctrl J. Select Sxx_V2 with enter,
or write Sxx_V2 as a text.Type in the (
Solution 10.1 - 9
T530-10 Solution 10.1 - RevG
3. Connect the Parameter to the variables. Type Ctrl j and type V2 in the box. Or use
arrow down
Solution 10.1 - 10
Compact Control Builder Training
F Copy Sxx_V2, paste it into the code block and rename it to Sxx_V6 and replace the
variables for V6. Declare the new associated local variables for Sxx_V6
1. Use Replace
Solution 10.1 - 11
T530-10 Solution 10.1 - RevG
2. Declare a new Sxx_V6 Function Block the same way as you did for Sxx_V2.
Solution 10.1 - 12
Compact Control Builder Training
4. Select and highlight the Sxx_V2 Function Block call in the code pane, right-click,
and select “Copy”.
5. Place cursor at the end of the existing code in the code pane, Keyboard Shift Enter
to insert two soft carriage return right-click and select “Paste”.
6. Change the pasted text to call the Sxx_V6 function block and change the variable
names for all V6 variables. Use Highlight “Edit” and select replace.
NOTE! Only replace one at a time an follow how far the tool
make changes. Replace all will replace outside the
highlighted zone
7. The text replaced to
Solution 10.1 - 13
T530-10 Solution 10.1 - RevG
Solution 10.1 - 14
Compact Control Builder Training
F Download the application to your controller and take Control Builder to the on-line
mode.
F Operate the outlet valves, V2 and V6 using Control Builder interaction windows.
Observe the reactor tank level.
Solution 10.1 - 15
Compact Control Builder Training
10.2.1 Description
Connect additional parameters to already existing function blocks. These connections
will prevent the Tank Inlet valve V1 and Tank Outlet valve V2 from being open
simultaneously.
10.2.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Exercise 10.2 - 1
T530-10 Exercise 10.2 - RevG
F Modify the code in your Tank program to prevent tank inlet valve V1 from being
opened if the tank outlet valve V2 is open.
F Place a comment on the Sxx_V1 function block to indicate that it is interlocked with
V2. Also place a page comment.
Exercise 10.2 - 2
Compact Control Builder Training
F Modify the code in your Tank program to prevent tank inlet valve V2 from being
opened if the tank outlet valve V1 is open, and place a comment in the code indicating
the interlock.
Exercise 10.2 - 3
T530-10 Exercise 10.2 - RevG
F Download the application to the controller and place Control Builder in on-line mode.
F Open the interaction windows for V1 and V2. Attempt to open both valves. Observe
the Interlock indication in the windows.
F When finished testing, close the interaction windows and take Control Builder to the
off-line mode.
Exercise 10.2 - 4
Compact Control Builder Training
10.2.1 Description
Connect additional parameters to already existing function blocks. These connections
will prevent the Tank Inlet valve V1 and Tank Outlet valve V2 from being open
simultaneously.
10.2.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Solution 10.2 - 1
T530-10 Solution 10.2 - RevG
2. When the “Ilock1” parameter is true and the valve is closed, it will be prevented
from opening. When the “Ilock0” parameter is true and the valve is open, it will
be prevented from closing
F Modify the code in your Tank program to prevent tank inlet valve V1 from being
opened if the tank outlet valve V2 is open.
Solution 10.2 - 2
Compact Control Builder Training
F Place a comment on the Sxx_V1 function block to indicate that it is interlocked with
V2.
1. Right-click the Sxx_V1 Function Block on the “Inlet_Valves” code block and
select “Edit Comment”.
3. The comment is placed above the Function Block with green text
Solution 10.2 - 3
T530-10 Solution 10.2 - RevG
4. Select the left Code pane and select “Edit Page Comment”
Solution 10.2 - 4
Compact Control Builder Training
F Modify the code in your Tank program to prevent tank inlet valve V2 from being
opened if the tank outlet valve V1 is open, and place a comment in the code indicating
the interlock.
1. In the code block for the “Outlet_Valves” connect the Ilock1 parameter to the
gV1_Open.Value variable. You may type this in manually or right-click in the
Sxx_V2 Function Block and select “Edit Parameters”.
2. Select the gV1_Open variable select with a “.” (dot) the component Value,
gV1_Open.Value
Solution 10.2 - 5
T530-10 Solution 10.2 - RevG
3. Result
Solution 10.2 - 6
Compact Control Builder Training
F Download the application to the controller and place Control Builder in on-line mode.
1. Open the interaction windows for V1 and V2.
2. Attempt to open both valves. Observe the Interlock indication in the windows.
F When finished testing, close the interaction windows and take Control Builder to the
off-line mode.
Solution 10.2 - 7
Compact Control Builder Training
10.3.1 Description
Write some very basic and simple logic in Structured Text programming the
functionality for tank temperature control.
Tank temperature is to be maintained at a certain temperature set point and control
band (called “hysteresis”) to prevent constant cycling of the heater and cooler.
10.3.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Exercise 10.3 - 1
T530-10 Exercise 10.3 - RevG
F Connect gTemp to the AI810 module on the PROFIBUS. Set the input range to 0..10V
and the scaling to 0-180DegC.
Exercise 10.3 - 2
Compact Control Builder Training
F Insert a new Structured Text code block name “Temp_Control” in program Tank and
write the necessary logic to control the heater and the cooler. The requirements that
the logic must meet are:
1. If the Start_TempControl variable is not true, automatic control of the Heater and
the Cooler is disabled.
2. Heating or Cooling will occur automatically if the measured tank temperature is
outside of the control range. The control range is determined by a user-changeable
setpoint and control band.
Exercise 10.3 - 3
T530-10 Exercise 10.3 - RevG
F Download to the controller. Open the Sxx_ReactorApp on-line editor and the Tank
program editor. Manipulate the variables SP_Temp, Temp_Hysteresis, and
Start_TempControl to test your logic.
F Check and make sure the heater and cooler turn both ON and OFF.
Exercise 10.3 - 4
Compact Control Builder Training
10.3.1 Description
Write some very basic and simple logic in Structured Text programming the
functionality for tank temperature control.
Tank temperature is to be maintained at a certain temperature set point and control
band (called “hysteresis”) to prevent constant cycling of the heater and cooler.
10.3.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Solution 10.3 - 1
T530-10 Solution 10.3 - RevG
Solution 10.3 - 2
Compact Control Builder Training
Solution 10.3 - 3
T530-10 Solution 10.3 - RevG
F Insert a new Structured Text code block name “Temp_Control” in program Tank and
write the necessary logic to control the heater and the cooler. The requirements that
the logic must meet are:
Solution 10.3 - 4
Compact Control Builder Training
1. If the Start_TempControl variable is not true, automatic control of the Heater and
the Cooler is disabled.
Solution 10.3 - 5
T530-10 Solution 10.3 - RevG
F Download to the controller. Open the Sxx_ReactorApp on-line editor and the Tank
program editor. Manipulate the variables SP_Temp, Temp_Hysteresis, and
Start_TempControl to test your logic.
Solution 10.3 - 6
Compact Control Builder Training
F Check and make sure the heater and cooler turn both ON and OFF.
1. In Online mode or Test mode set up the value True for variable
Start_TempControl
2. Set up the force and enter for example 98 DegC and check the
If then else statement
Solution 10.3 - 7
T530-10 Solution 10.3 - RevG
3. Set up the force and enter for example 118 DegC and check the
If then else statement
Solution 10.3 - 8
Compact Control Builder Training
10.4.1 Description
Add new logic to existing “Temp_Control” code block to ensure the heater shuts off
anytime the temperate is above 130 Degrees C.
10.4.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Exercise 10.4 - 1
T530-10 Exercise 10.4 - RevG
F Write additional logic to stop the heater if the actual temp is > 130°C. Create a local
variable called Temp_High to indicate when tank temperature is high.
F Turn automatic temperature control on (set Start_TempControl to True) and set the
tank temperature control setpoint SP_Temp to 140 degrees. Observe the heater
energize and the tank temperature rise. The heater should stop when the tank
temperature reaches 130 degrees.
Exercise 10.4 - 2
Compact Control Builder Training
10.4.1 Description
Add new logic to existing “Temp_Control” code block to ensure the heater shuts off
anytime the temperate is above 130 Degrees C.
10.4.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity
Solution 10.4 - 1
T530-10 Solution 10.4 - RevG
F Write additional logic to stop the heater if the actual temp is > 130°C.
1. Create a local variable called Temp_High to indicate when tank temperature is
high.
Solution 10.4 - 2
Compact Control Builder Training
F Turn automatic temperature control on (set Start_TempControl to True) and set the
tank temperature control setpoint SP_Temp to 140 degrees. Observe the heater
energize and the tank temperature rise. The heater should stop when the tank
temperature reaches 130 degrees.
Solution 10.4 - 3