Looking to get started with Structured Text / Structured Control Logic (ST / SCL)?

Here’s an introductory guide to the PLC language that is not restricted to a single development platform or environment

Although most of the images use Siemens TIA Portal, the rules of the language will still apply to Allen BradleyCodesys (and all flavours of Codesys) and many other PLC development softwares.

As always, if you want to discuss further, drop me a message in the comments or on LinkedIn

Basic Principles

SCL / ST Basic Example – Comments (in green) explain what the line below is doing

Depending on the platform you are working in, Structured Text can be referred to as one of the following:

  • Structured Control Language – SCL – (Siemens)
  • Structured Text – ST – (Nearly Everyone Else)

Whatever your design environment calls it, Structured Text behaves the same way in most cases.

Note
Note
Note

From here on in, we’ll refer to Structured Text as either ST, or SCL depending on the environment examples are taken from

Flow

The flow of ST / SCL is simple, like reading a book, from left to right, line by line.

Left to right, top to bottom flow direction

The above example means Lines 1-4 are executed first, then 4-6 and finally 8-9

Monitoring

Different environments have different methods to view SCL / ST:

Siemens TIA Portal

In Siemens TIA Portal, monitoring is done via a floating window to the right of the SCL.

My personal opinion is that this is a horrible way of doing it.

You need to read the code, then look at the variables to the side. If more than 1 variable appears on a line, then they are listed in the monitoring box.

It gets messy to follow and read.

Allen Bradley – Studio 5000

In Studio5000, a better solution is implemented where the variable value is indicated below the variable in an “Inline View” mode

This is easy to follow and read.

CODESYS V3.5

CODESYS has my personal favourite approach, with completely inline monitoring

This just makes things super simple to read, after all, when you are monitoring, you’re really just looking at the values.

Note: “Flow Mode” is enabled in this image

Calling Instructions

As with all languages, SCL/ST can call other Functions and Function Blocks. Because of the textual nature of the language, this can leave a lot to be desired over graphical languages

Example of calling a TON Timer in SCL (Siemens TIA Portal)

The above is an example of calling a TON Timer, however, you don’t know its a TON unless you either recognise the interface, or view the declaration pane to see what data type #Timer is. Even the tooltip doesn’t tell you what the actual instruction is:

Siemens TIA Portal – Tooltip for instruction / call

In contrast, CODESYS does tell you when you hover over the instance of the timer:

CODESYS Tooltip

This is one of the little things that can make ST/SCL difficult for beginners or people reading other people’s code.

Interface Parameters

ST/SCL have different symbols on the interface to denote the direction that the data is travelling (into the block or out of the block)

  • := – Either Input or InOut
  • =>Output

Interface orders are not fixed, although the first time a block is loaded they should be in the order of the block’s interface. There is nothing stopping you from writing a Timer as follows:

Different interface order to the default for a TON timer

The above shows the PT first and the IN last, which is not the conventional order of a TON block.

IF Statements

For more complex decision making, or non-boolean actions, IF Statements can be used to make things easy

CODESYS IF Statement

The above example shows the Signal value being evaluated to see if it is above 10.2. If it is, then the enclosed code is executed

  • ErrorCode is set to 16#AF
  • Signal_Error is set to the value of Signal

The END_IF denotes the end of the IF Statement

If the conditional part of the IF Statement is False, the enclosed code between IF and END_IF is simply not executed

IMPORTANT
IMPORTANT
IMPORTANT

Remember, you skip over code enclosed in IF and END_IF.

If you’ve set a variable to True inside an IF Statement, it’ll remain true forever unless you set it false outside of the IF statement.

The same goes for numerical values, they won’t update again until they are updated elsewhere or the IF Statement is True

There are also variations to the IF Statement by using ELSIF

ELSIF instruction

The above example shows an ELSIF additional instruction to the IF Statement

ELSIF‘s are processed in order, and only if no other condition that precedes it has been executed due to a true conditional statement.

In the above example, because IF Signal > 10.2 was False, ELSIF Signal > 0 was evaluated as True and ErrorCode was set to 16#0

Case Statement

TIA Portal example of a Case instruction

The Case Statement allows the evaluation of a variable and a section of code to be executed as a result. You can also optionally include an ELSE to capture anything other than a valid case.

Above, because #Val is set to 2, the #Result is set to 2 also in accordance to the code on line 21

Conclusion

There are a lot of other instructions and patterns you can use in SCL/ST, it is one of the most flexible languages available in PLC programming.

One of the strongest advantages of Structured Text though is the ability to copy / paste between different environments. There may be some tweaks to make to how variables are displayed (# in siemens for example is before any local variables), but these are easy to fix with Find/Replace.

One of the biggest weaknesses is in formatting and layout. People will write differently, some writing standards could be useful if multiple programmers are working on the same project


Check Out Another Post

Leave a Reply