Introduction to the PLC Language: Structured Text

Updated on July 17, 2018

About Structured Text

Structured text is one of (if not, the most) powerful of the available IEC languages. It allows complex, conditional code to be written much easier than any of the other languages.

The following functions are available in Structured Text:

  • FOR - Iterate through nested code a number of times
  • WHILE - Execute the same nested code until the while condition is false
  • IF - Execute nested code only if the condition is true
  • ELSE - Execute an alternative section of code if the IF condition is false
  • ELSIF - Conditional Else function, if the IF condition is false, but the ELSIF condition is true, execute the nested code. ELSIF can be stacked multiple times, and combined with an ELSE at the end to ensure code runs in the event of all conditions returning false. More on stacked conditions later!
  • CASE Statement - Execute a particular case, depending on the value of a declared variable

Structured Text lends itself heavily to complex algorithms, long mathematical functions, array manipulation and repetitive tasks. I often find myself using ST (structured text) in function blocks that iterate over arrays.

Programming Environment

ST is no different to any of the other IEC languages, and every editor displays it slightly differently.

The ST environment is extremely simple, it's essentially a blank text file. In most editors, the lines are numbered for convenience of referencing code in documentation.

IF Start THEN
	Start:=FALSE; //Remove Start Latch
	WHILE Initialised = FALSE DO
		System_Prime:=TRUE; //Set Output On For System Primer
	END_WHILE
	System_Prime:=FALSE; //Turn Off Output For System Primer Once Initialised
END_IF

Consider the above example, let's analyse this line by line

  1. IF Statement - If the variable "Start" is TRUE then execute all code between the IF and the END_IF
  2. Set the variable "Start" to FALSE. This stops "Start" from remaining TRUE once we have started executing the IF statement. Ensuring the IF Statement only executes once
  3. WHILE Statement - Whilst the variable "Initialised" is FALSE, DO the nested code repeatedly.
  4. Set the variable "System_Prime" to true
  5. END_WHILE - Ends the WHILE statement
  6. Set the variable "System_Prime" to false
  7. END_IF - Ends the IF Statement

Caveats

Structured Text is extremely powerful, however it does have some "issues" that need to be understood in order to use it correctly.

WHILE / FOR STATEMENTS ARE EXECUTION BLOCKING!

WHILE statements and FOR statements can cause some dangerous conditions if the above is not remembered or understood. Consider the earlier example, when the WHILE statement is repeating, no other code is executing, before or after the while loop.

This can be demonstrated by updating the code to the following:

Var1:=0;
IF Start THEN
	Start:=FALSE; //Remove Start Latch
	WHILE Initialised = FALSE DO
		System_Prime:=TRUE; //Set Output On For System Primer
	END_WHILE
	System_Prime:=FALSE; //Turn Off Output For System Primer Once Initialised
	Var1:=Var1+1;
END_IF

"Var1" will be initialised to the value 0 before the IF statement is executed, however when the WHILST statement is running, line 8 will not be executed, even though the IF statement is still executing.

Most PLCs use a single thread per task, meaning the code will execute sequentially one line at a time.

So how do we stop this from happening? If we had some critical outputs, we don't want code to block execution until the WHILE condition is complete? There are two options

  1. Simply don't use while statements. Replace them with IF statements that do not block execution. In most cases IF statements can reproduce the same desired outcome with a little extra coding, without the side effects of blocking code
  2. Run the WHILE code in a separate task with a low priority, this would not block execution on other tasks

IF Statements

IF statements are probably the most used function in ST and the most common reason why ST would be chosen for the POU / Function Block in development.

Take a look at the example below

IF S1 = 0 THEN
	S1:= S1+1;
ELSIF S1 = 1 THEN
	S1:= 2;
ELSIF S1 = 2 THEN
	S1:= 3;
ELSE
	S1:=4;
END_IF

This example shows a 4 state IF statement that is guaranteed to execute 1 state only.

  • S1 = 0
  • S1 = 1
  • S1 = 2
  • S1 = Anything else

The ELSIF statement will only execute if the IF or ELSIF statement above it has NOT executed.

The ELSE statement will always execute if none of the statements above have executed

CASE Statement

CASE Statements are very similar to IF statements, however they define the state to run without having to pass through every ELSIF statement.

Take a look at the code below:

CASE i OF
	0:
		i:=1;
	1:
		i:=2;
	2:
		i:=3;
	3:
		i:=0;
END_CASE

This CASE statement provides almost the same functionality as the previous IF example, the only difference is there is not a "Anything" case that captures any undefined conditions. CASE statements are explicit, the case must exist else it will not be actioned.

Common uses of CASE is to enforce a State Machine type architecture

Conculsion

Structured Text is extremely powerful and certainly has it's place in application design. Favoured for complex tasks, it's flexibility is superior to all other IEC languages, it is easily read, but can be confusing to beginners, especially with the caveats that other languages do not suffer with.

ST should be the language that is learnt second to Ladder (As ladder is most common), it will open up possibilities in functions and function blocks that are extremely difficult to achieve in Ladder alone

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, owlcation.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://owlcation.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)