Programming PLCs Using Structured Text
Programming PLCs Using Structured Text
Nieke Roos
Department of Computing Science
University of Nijmegen
Toernooiveld 1, NL-6525 ED, Nijmegen, The Netherlands
nieker@sci.kun.nl
Abstract
An ever increasing part of the industrial and safety-critical
processes is presently controlled by Programmable Logic
Controllers (PLCs). As its name already suggests, a PLC can be
and has to be programmed to perform its task. Since its birth, a
wide range of different PLCs has been put on the market that
makes use of an even wider range of programming languages.
Recently, the IEC 1131-3 standard has been published in an effort
to reduce chaos to order in the universe of PLC programming
languages.
This paper takes a closer look at one of the programming
languages defined by the IEC 1131-3 standard, namely the
Structured Text programming language. A comparison is made
between Structured Text and the language upon which it was
based, the Pascal programming language. The primary differences
that result from this comparison are then discussed and some
conclusions are drawn about the IEC 1131-3 standard.
1.1 Introduction
As the Industrial Revolution stormed the civilized world late last century man started to create
machines that could alleviate the hard labour some existing tasks brought with them. This very first
large-scale reflection of man’s desire to be able to automate was the incentive to an evolutionary
developement of industrially employed equipment. The first machines ran on steam, later on gasoline
was used as primary source of energy and at present the majority of the devices in the industry is
powered by electricity. At the same time the dimensions of these devices decreased from steam
engines the size of an average suburban dwelling to electrical appliances not bigger than a lunchbox
containing even smaller circuits and wiring.
The invention of the computer and its massive application following the Digital Revolution taking
place in the 1960s, gave birth to a new phenomenon: the art of programming. This revolutionary
ability to control the behaviour of an apparatus by precisely conveying the actions it has to perform
exerted enormous influences not only on the computer science domain but on other domains as well.
An important and interesting example of the influence programmability has had on other domains is
the Programmable Logic Controller (PLC), resulting from the synthesis of the electrical world and the
universe of computer science in an effort to combine this programmability with electrical circuitry.
PLC
Program
memory
Instruction
s
Processor
i1 o1
Μ Input Data Output Μ
…
Note that the before-mentioned task-loop usually is said to be executed not by the hardware but by
something that is a combination of hardware and software, called firmware. Firmware is software that
has been written onto read-only memory.
Until recently, this posed a huge problem. Because there was no such thing as a uniform language
for programming PLC’s, every manufacturer developed and applied a programming language of
his/her own. This caused a virtually endless series of different programming dialects to be created. For
a company new to the field to pick one to use in its designs was the same as looking for a needle in a
haystack. In 1993, however, the International Electrotechnical Commission (IEC) decided to facilitate
the needle finding by publishing the IEC 1131 International Standard for Programmable Controllers.
The third part of this standard, entitled “1993 Programmable languages” but commonly called IEC
1131-3, contains the definition of five languages for programming PLCs ([IEC]).
2 IEC 1131-3
The next section will take a closer look at one of these programming languages, the Structured Text
programming language.
3 Structured Text
The Structured Text programming language is a high level programming language. Because the
Structured Text language is based upon another high level programming language, namely Pascal,
both languages bear strong resemblance syntactically. This resemblance is used to clarify the
Structured Text syntax by comparing Pascal code to equivalent program pieces written in Structured
Text. To be able to write pieces of Structured Text, the language constructs and the data types that
these pieces are composed of have to be introduced first. This is done in the next two paragraphs.
3.1 The language constructs
One of the problems concerning Structured Text within the framework of the IEC 1131-3 standard is
determining exactly what is part of the language and what is not. The standard defines Structured Text
to be just the statements that can be used to construct function and function block algorithms and
program bodies. However, within the standard, elements common to all five programming languages
defined in the standard, such as elements that identify, for instance, a configuration, a resource or a
program and the declaration of variables, are written using a syntax that also resembles the Pascal
syntax in such a way that many of the people that have applied the standard have unjustly regarded
these common elements as being part of the Structured Text programming language as well, in spite of
the standard. Clearly, the standard isn’t as clear on this as one would like a standard to be.
A comprehensive context-free description of the syntax of the language constructs that, according
to the IEC 1131-3 standard, do belong to the Structured Text programming language is included in
appendix A.
3.2.1.4 Strings
IEC Data type Description Examples
STRING Character strings ‘Hello world’
‘’
Table 4: String data type
In the example above, the Point data type is a structured data type itself.
TYPE Color :
(Red, White, Blue);
END_TYPE
TYPE Angle :
INT(-180..+180);
END_TYPE
3.2.2.4 Array data types
An array data type is constructed by the keyword ARRAY, followed by a multi-dimensional vector of
ranges of indices, followed by the keyword OF, followed by the (elementary or derived) data type that
is going to be contained in the array. A multi-dimensional vector of ranges of inidices consists of a
comma-delimited list of ranges of indices, enclosed in brackets. A range of indices consists of a lower
and upper bound of the range, both integers, separated by two dots. For example:
TYPE Display :
ARRAY[1..768, 1..1024] OF Color;
END_TYPE
3.3 Usage
In order to clarify the Structured Text syntax, the next few paragraphs contain a comparison between
the following constructs that can be found in both the Structured Text programming language and
Pascal: assignments, function calls, conditional statement and iteration statements. Each time the
notable differences will be put forward.
3.3.1 Assignments
Structured Text Pascal
VarA := (VarB * 24 MOD 2 = 1) XOR VarA := (VarB * 24 MOD 2 = 1) XOR
(VarC <= VarD + 34); (VarC <= VarD + 34);
(* AnArray is an array containing (* AnArray is an array containing
twenty integers *) twenty integers *)
AnArray := 10(1), 5(2), 5(3); FOR ElementNr := 1 TO 10 DO
BEGIN
AnArray[ElementNr] := 1;
END;
FOR ElementNr := 11 TO 15 DO
BEGIN
AnArray[ElementNr] := 2;
END;
FOR ElementNr := 16 TO 20 DO
BEGIN
AnArray[ElementNr] := 3;
END;
VarA := AND(Var1, Var2, ..., VarN); VarA := Var1 AND Var2 AND ... AND
VarN;
Table 6: Assignments in Structured Text versus assignments in Pascal
Assignment-wise one major dissimilarity can be observed. Notice the ease with which an array can be
initialized in the Structured Text language. Also notice the effort it requires to do the same in Pascal!
Operator-wise there is another difference between the two languages. In the Structured Text
language, some of the operators are extensible, i.e. some operators can have any number of arguments
(of the same type).
Note that, strictly speaking, the FUNCTION … END_FUNCTION and the VAR … END_VAR
constructs mentioned in this paragraph do not belong to the Structured text programming language, as
discussed earlier.
The main difference between the Structured Text language and the Pascal language, when one looks at
the conditional statements in both languages, is the explicit use of the BEGIN and END keywords in
the Pascal code in cases that construct bodies contain two or more statements. Note the inconsistent
omission of these keywords in the ELSE part of the Pascal CASE construct.
Another, minor, difference is the necessary omission of the semicolon before the ELSE IF or
ELSE part of the IF construct in the Pascal programming language.
As was the case with conditional statements, the primary difference between the Structured Text
language and the Pascal language, when one looks at the iteration statements in both languages, again,
is the explicit use of the BEGIN and END keywords in the Pascal code in cases that construct bodies
contain two or more statements. Note another inconsistent omission of these keywords in the Pascal
REPEAT construct.
Another difference is the possibility of including a BY part in the FOR construct of the Structured
Text language, indicating the magnitude of the step taken every time a FOR loop is executed. The need
of a similar construct in the Pascal language can, awkwardly, be circumvented by slightly adjusting
the statements within the FOR construct, possibly combining this with the use of the DOWNTO
construct available in the Pascal language, as illustrated by the example above. This DOWNTO
construct in Pascal is equivalent to using the TO construct in combination with a BY –1 in the
Structured Text programming language.
However, the kind of identification that directly represented variables advocate is extremely
implementation-dependent and therefore should have no place in a standard such as IEC 1131-3 that
addresses implementation-independent issues.
This concept of default initial values allows parameters to be omitted from function calls, in which
case the unused input parameter will take the default value as given in the function type definition. An
example of this can be seen in paragraph 3.3.2, where the value 3.0 is assigned to Average2.
In the IEC 1131-3 standard, one particular example of the construction mentioned above can be
found to play a part in another phenomenon common to the Structured Text language, but unknown to
the Pascal programming language: bonus parameters for free. With every function, two bonus boolean
parameters are supplied gratuitously: an EN input parameter indicating whether the function body is
executed or not and an ENO output parameter indicating whether the execution of the function has
resulted in an error of some kind. In case the latter parameter indicates the occurrence of an error, the
function’s other result(s) is (are) undetermined.
3.4.5 Overloading
A fifth difference is the fact that the Pascal language does not support overloading, contrary to the
Structured Text programming language. Pascal does, however, support the concept of coercion. It is
because of this, and not because of the support of overloading, that it is possible in Pascal that the
square function, which, according to its definition, requires a real argument, can be supplied with an
integer argument.
Note that the Structured Text programming language does not allow one to create an overloaded
function oneself. The only overloaded functions existing within the Structured Text language are
functions offered by Structured Text itself.
3.4.6 Recursion
The sixth and final fundamental difference, and maybe the most important one, is the total absence of
the notion of recursion within the Structured Text programming language. The IEC 1131-3 standard
specifically prohibits functions, function blocks and programs to be recursive, that is, their invocation
should not cause the invocation of another function, function block or program of the same type. One
can only guess at the why and wherefore of this decision, because when it comes to explaining the
reasons of banning recursion from within the confines of the Structured Text language, the standard
remains silent. One possible reason could be that a program written in the language as defined by the
IEC 1131-3 standard can simply be viewed as one big collection of macro-expansions, thus rendering
the processing of a Structured Text program less complex. Introducing recursion into the game would
totally annul this view. The question is, however, whether the reduction in complexity is as significant
as the advocates of the omission of recursion claim it to be, compared to the increase entailed by the
introduction of other language elements such as the concept of overloading mentioned above. With all
the concepts that have been included in it, the Structured Text language has long outgrown the
characterising traits of a basic programming language and one could wonder why stop here and not
include recursion? Other possible reasons against allowing recursive Structured Text are the difficulty
of testing and the unpredictability of its real-time performance. These are both perfectly valid reasons,
but, strangely enough, none of them are used to give an explanation for the absence of recursion in
IEC 1131-3. In conclusion, one can say that the unaccounted omission of recursion definitely
constitutes one of the standard’s major flaws.
4 Conclusions
After a brief introduction to the universe of the Programmable Logic Controller, this paper discussed
one of five ways defined by the IEC 1131-3 standard to program this device: the Structured Text
programming language. A comparison has been made between Structured Text and the language upon
which it was based, the Pascal programming language. This comparison revealed a great similarity
existing between the two. Of the few distinctions that have been uncovered, the absence of the ability
to write recursive programs in the Structured Text language is the most remarkable one, especially
because of the undocumented nature of this omission, as a result of which the choice of what
programming constructs to include in the language and what not gives the impression of being a fairly
arbitrary one.
References
[HAB] Wolfgang A. Habing and Krzystof M. Sacha. Programmable Logic Controllers. Real
Time Systems: Implementation of Industrial Computerized Process Automation.
[LEW] R.W. Lewis. Programming Industrial Control Systems using IEC 1131-3.
[TOU] Konstantinos Tourlas. An assessment of the IEC 1131-3 Standard on Languages for
Programmable Controllers.
Appendix A:
The syntax of the Structured Text programming language
A.1 Expressions
Production rules:
Expression ::= XOR_Expression {'OR' XOR_Expression}
XOR_Expression ::= AND_Expression {'XOR' AND_Expression}
AND_Expression ::= Comparison {('&' | 'AND') Comparison}
Comparison ::= EquExpression { ('=' | '<>') EquExpression}
EquExpression ::= AddExpression
{ComparisonOperator AddExpression}
ComparisonOperator ::= '<' | '>' | '<=' | '>='
AddExpression ::= Term {AddOperator Term}
AddOperator ::= '+' | '-'
Term ::= PowerExpression
{MultiplyOperator PowerExpression}
MultiplyOperator ::= '*' | '/' | 'MOD'
PowerExpression ::= UnaryExpression {'**' UnaryExpression}
UnaryExpression ::= [UnaryOperator] PrimaryExpression
UnaryOperator ::= '-' | 'NOT'
PrimaryExpression ::= Constant | Variable | '(' Expression ')'
| FunctionName '(' [ST_FunctionInputs] ')'
ST_FunctionInputs ::= ST_FunctionInput { ',' ST_FunctionInput}
ST_FunctionInput ::= [VariableName ':='] Expression
Table 10: Production rules of expressions in Structured Text
A.2 Statements
Production rules:
StatementList ::= Statement ';' {Statement ';'}
Statement ::= NIL
| AssignmentStatement
| SubprogramControlStatement
| SelectionStatement
| IterationStatement
Table 11: Production rules of statements in Structured Text