Technical Article

Basic Structured Text Programming in Mitsubishi PLCs

July 06, 2023 by Bob Odhiambo

Structured Text (ST) is one of the common IEC programming languages that can be used in programming PLCs. Learn how to use structured text to write executable programs for MELSEC’s FX series PLC.

Most of the familiar PLC programming languages, ladder logic, function blocks, and sequential function charts, are basic languages that involve visual representation of program instructions. Instruction lists and structured text programs, on the other hand, are the IEC 61131 programming languages that are text-based. Structured text (ST) is a hierarchal form of code that is executed in code blocks by the PLC CPU, more reminiscent of traditional computer programming languages. In this tutorial, we are going to explore how to design and download structured text commands to the MELSEC series PLC while providing some practical examples.

 

Basic Rules of Programming In ST

In programming using ST, there are a couple of considerations that need to be taken to ensure that it is done the right way. Such considerations include the I/O control statements, assignment statements, numerical notations, and program execution sequences.

 

Instruction Arguments

In a basic ST command line, there is an instruction to be executed by the PLC followed by one or more arguments. These arguments might describe arithmetic expressions, variables, or constant values, and they are written after the instructions in parenthesis, as required by each unique command. There can be multiple arguments depending on the instruction and by creating a combination of statements, a workable program can be created.

 

Command parameters

Figure 1. Basic OUT instruction breakdown in ST. All article images used courtesy of the author

 

Assignment Statements

Another essential aspect to note in ST programming is the assignment statement. This is a statement that assigns values from one-bit device to another depending on the program's need. The statement operator is a combination (:=) of a colon and an equal sign.

In a practical example of variable value assignment, a command might be written as D10:= 5. In that example, the value ‘5’ is saved into the data register D10.

When it comes to numerical notations, decimals can be directly written as numbers or the prefix 'K' added before the value. For the case of binary and octal, the prefixes ‘2#’ and ‘8#’ are added, respectively.

When representing binary notations, ON and OFF are represented in terms of 1 and 0. The words ‘TRUE’ or ‘FALSE’ can also represent binary notation.

 

Binary commands

Figure 2. Binary notations in ST.

 

Comments

Comments are also essential in ST as they act as reference text for your program, making it understandable for yourself or others in the future. They are also frequently used to break up long blocks of code into functional areas for clarity. Comments are enclosed between the two (* *) two asterisks.

 

Basic Structured Text Program in FX CPU

To get started, you will need to have the latest version of GX Works2 or GX Works3 software installed (these examples all use GX Works3). Once the software is installed, we can go ahead and launch it and create a new project.

GX Works3 supports structured text in FX-5U CPUs, which can be selected while creating a new project. Once the project is created, we can start our program.

For the first programming example, we are going to use that previously explained ‘OUT’ instruction to write a simple ST program to write the state of X000 to the output data of Y000.

 

ST command

Figure 3. Simple ST program example with input contact X000 and output Y000.

 

That example is quite simple, where an output mirrors the state of an input. But most programs are a little more involved than this simple example.

Next, consider the ladder program in the figure below. We are going to look a how we can implement structured text in the event of multiple conditions like in this common latching output configuration.

 

Latching ladder logic program

Figure 4. Ladder program example with multiple conditions.

Latching ST program

Figure 5. ST program to implement the ladder example with multiple conditions.

 

For multiple instructions, logical operators are often used. The parentheses determine the order in which the logic progresses. Changing the order of the parentheses will have a major impact on the operation of the command. AND, OR, NOT, and XOR are a few examples of the logical operators that can be used in such cases.

In ST, GX Works3 has allowed for the addition of labels to represent variables. For instance, input X000 can be represented by a label like sensor1. To add the labels, we click on 'Global label' and then 'Global' in the left sidebar of the software interface. Once clicked, we can add the label name, data type, and comments.

 

Variable names in ST

Figure 6. Adding global labels to variables for use in an ST program.

 

Once the labels are added, we can now replace the variables with the labels while writing the ST program. This is the same example as in Figure 5, but the real-world variables are suddenly much easier to recognize and troubleshoot when errors occur.

 

Alternative names for variables

Figure 7. ST program with the inclusion of labels of input and output variables.

 

Structured Text Programming

While this simple tutorial was an introduction to the format of structured text, adding the ability to use such a language in your projects would be another powerful tool in your programming toolbox. It allows for the creation of complex and large PLC projects in a fast and efficient way, making memory allocation easier and overcoming some of the obstacles presented by the limitations of pre-built function blocks available in many of the other IEC 61131 languages.

 

Want to learn PLC programming in an easy-to-understand format and take your career to the next level?

Check out this PLC programming worksheet.

 

Featured image used courtesy of Adobe Stock