This video is brought to you by us, RealPars!
Join the top 1% of PLC programmers.
Learn from the world’s best so you can join their rank.
Head on over to realpars.com and start learning now.
In this video, we are going to discuss the basics of PLC programming
utilizing Structured Text.
As one of the IEC-61131 PLC programming languages,
Structured Text or just “ST” is based on and resembles
traditional programming languages like Python or Java.
Rather than being visual or graphics-based
like ladder logic or Function Block Diagram,
Structured Text is just that, text!
Like all programming languages, Structured Text has advantages and disadvantages,
and there are also reasons you as a programmer
might choose to use Structured Text in your next PLC programming project,
so let's get right into it.
Since Structured Text is similar to traditional High-Level programming languages,
it can be fairly easy for many people
who may not have a background in PLC programming
but have experience in traditional coding to learn and develop PLC projects.
In most cases, you can also develop your Structured Text PLC programming project
without using the PLC programming software.
Since it is text-based, you can write your project in a simple text file
and copy and paste it into your PLC project when you are ready.
This also makes editing your project easier when you are debugging.
While this feature of Structured Text is useful,
remember that without your programming software like TIA Portal or RSLogix,
you cannot compile and debug your program.
Most PLC manufacturers support different programming languages in their PLC’s
and this is because there are many different reasons that
a programmer will choose a particular programming language.
These reasons can be as simple as a client request for a specific programming language
all the way to the size of the programming project.
Since Structured Text is by definition, text-based,
it means that it does not have the memory-intensive graphical interface
which can allow for a smaller processor memory and therefore a reduced cost.
This makes Structured Text an ideal candidate for larger PLC programs
where controller memory is at a premium.
Some other times you might want to use Structured Text
are when you are deploying the same PLC program over and over,
such as in a packaged machine that is sent to a customer for a turn-key start-up.
In these cases,
your PLC program file can be stored offline in a text file or word document
and can be easily sent as an email attachment
for someone to download into the PLC and deploy.
The syntax of Structured Text follows some basic rules.
First, all statements in Structured Text will end with a semicolon.
A routine will close out with an End_If statement.
Spaces and tabs are not required,
but a good programmer will still use them for readability.
Also, Structured Text is not case sensitive, but if you are assigning a variable,
known as a “tag” or a “symbol” such as “control valve 1”,
then using camelCase ControlValve1 is a good practice to get into.
Another very common and useful syntax of Structured Text is the use of a comment.
As a beginning programmer,
the use of comments is essential to creating code that can be read later on.
To add a comment in Structured Text
you will start with a left parenthesis followed by an asterisk.
To close your comment,
you will then add another asterisk followed by a right parenthesis.
So, if you are going to add “This line of code will close control valve 1”
it will look like this:
Structured Text also uses operators to manipulate data.
Some examples of operators are the logical operators such as AND, OR, and NOT.
A logical operator is used to compare Boolean data and create logic from it.
An example of a logical operator in Structured Text looks like this:
In this case, if the control valve 1 is NOT closed,
the program would execute based on
any following instructions that follow the instruction.
Here is another example:
In this case, if the control valve 1 is closed,
or if another parallel condition was true,
the program would execute based on
any following instructions that follow the instruction.
Remember, just because the expression told us that the "control valve 1" is not closed,
we still aren’t sure it is open!
We will need to create logic that tells us that it is open.
Let’s create a simple statement in Structured Text
to turn on a pump when the control valve is open.
First, let’s start by evaluating the state of the control valve.
You will want to ensure that the valve is open by using this statement.
IF "control valve 1 underscore closed" is False
AND control valve 1 underscore open is True.
With this statement, you have evaluated that the control valve is not closed
and that the control valve is open.
Now, let’s turn on the pump.
To do this you will add “then pump underscore start is true” to the statement.
Now that the pump is running,
you need to add logic to stop the pump if the control valve closes.
To do this you will simply add another statement to the logic as...
IF "control valve 1 underscore closed" is TRUE OR
"control valve 1 underscore open" is FALSE THEN pump start is FALSE.
Now let’s put all of our statements together to create a routine.
To do this you’ll have the first statement and then add ELSE below that.
Next, you’ll have the second statement and then add...
End underscore if at the end.
With this routine, if the control valve 1 is not closed AND it’s open
THEN the pump starts.
Otherwise, if the "control valve 1" is closed
and it’s not open then the pump stops. Very easy, right?
To make this routine easier for others to read and understand,
you can also add a comment above that.
So you’ll go with a left parenthesis followed by an asterisk and then add...
this routine will start the pump when the control valve is open
and stop the pump when the control valve is closed or not open.
To close the comment, you will add another asterisk followed by a right parenthesis.
Congratulations! You have just created your first routine in Structured Text!
How cool is that?
While this simple tutorial was an introduction to Structured Text,
adding the ability to utilize Structured Text in your projects
will be another powerful tool in your programmer's toolbox.
It allows you to create large and complex PLC projects
quickly and efficiently while maximizing the allocation of memory.
I hope you enjoyed this easy-to-follow video tutorial
brought to you by the RealPars team.
Want to learn PLC programming in an easy to understand format
and take your career to the next level?
Head on over to realpars.com