PLC Basics: The Allure of Programming Languages

 



The development of microelectronics has endowed integrated circuits with a certain level of "intelligence." Microprocessors (CPUs) can execute predefined "programs," and individuals communicate with computers (PCs) by writing "program" code.

In the early stages of computer technology development, writing "program" code was not a simple task. Since computers could only understand code composed of "0s" and "1s," programmers had to encode program instructions onto punched tapes (with "1s" representing punched holes and "0s" representing no holes). These punched tapes were then fed into the computer's input system, enabling the computer to comprehend the tasks assigned by the programmer. Programmers had to meticulously define the meaning of each "bit" and logically combine them to form the code. This binary code was highly complex and challenging for humans to comprehend, leading to difficulties in writing, modifying, and maintaining programs. The following image depicts machine code in hexadecimal format (whether aliens find it understandable is unknown, but for me, it's indecipherable):




To enhance the efficiency of program writing and improve code readability and maintainability, computer scientists invented "assembly language." Assembly language uses specific English symbols to represent machine instructions, significantly enhancing program readability and improving programming efficiency.

While the English instructions of assembly language are human-friendly, they are not directly understandable by microprocessors (CPUs). Therefore, scientists designed a dedicated program to "translate" assembly language's English instructions into machine code that microprocessors (CPUs) can execute. This translation program is called a "compiler" (the evolution of compilers has its own history, but we won't delve into it here). Code written in assembly language is referred to as "source code." The image below shows machine code on the left and "source code" written in assembly language on the right:











In comparison to early punched-tape programming, assembly language programming significantly improved program readability. However, with the rapid development of microprocessor technology, writing programs in assembly language gradually became inadequate, primarily due to two reasons:

Firstly, as program complexity increased, the volume of code written in assembly language significantly grew, leading to increased difficulty in writing and maintaining the code.

Secondly, assembly language relies on specific microprocessors, resulting in poor program portability across platforms. Often, rewriting is necessary.

As a result, a programming language that doesn't require memorization of intricate instructions and is hardware-independent became a coveted goal for programmers. Thus, high-level programming languages for computers were born, with C language being a typical representative. C language replaced assembly language's instructions with structured statements, provided rich data types and operators, supported pointer functionality, and offered concise and compact code. Since its inception, C language has been highly acclaimed by numerous programmers and is considered a classic in programming languages. Even today, C language maintains its strong vitality and holds an irreplaceable position in single-chip and embedded programming.

With the use of microelectronics in industrial control, PLCs (Programmable Logic Controllers) have replaced traditional relay control systems. As a "programmable" logic controller, programming languages are indispensable. However, due to the diverse nature of PLC products from various manufacturers, each with its own programming language syntax, PLC application and promotion have faced inconvenience.

To standardize PLC programming languages, the International Electrotechnical Commission (IEC) drafted and issued a standard for programming languages in the field of industrial automation (IEC 61131-3). This standard defines five languages for use in industrial control, including graphical and textual languages. Graphical languages include Ladder Diagram (LD), Function Block Diagram (FBD), and Sequential Function Chart (SFC). Textual languages include Instruction List (IL) and Structured Text (ST).

To further promote the IEC 61131-3 language standard, the PLCOpen international organization was established in 1992, headquartered in the Netherlands. PLCOpen is a global organization independent of manufacturers and products, aiming to standardize programming methods for industrial control software and promote the IEC 61131-3 language standard. PLCOpen grants certificates to programming languages that comply with this standard.

After the publication of the IEC 61131-3 language standard, major PLC manufacturers expressed their support, revising their products accordingly.

Among various PLC programming languages, the Ladder Diagram (LD) language is the most widely used. The Ladder Diagram evolved from early relay control system schematics and shares similarities with relay circuit diagrams. It is intuitive and easy to understand, retaining the style and conventions of relay circuit diagrams. Ladder Diagram is the language that personnel familiar with relay control systems find the easiest to accept and use. The image below shows a simple Ladder Diagram code:







While the Ladder Diagram (LD) is easy to get started with, it can become challenging when writing programs for large-scale systems. Many books recommend beginners to learn Ladder Diagrams, but I don't entirely agree, unless you are dealing with simple logic control. When the logical systems you need to control become complex and you consider switching to another language, you essentially have to start from scratch. Therefore, I recommend beginners to learn Function Block Diagrams (FBD).

Function Block Diagrams (FBD) use logic symbols from digital circuits (such as "AND," "OR," "NOT") to express control logic, ensuring clarity of logic even when writing programs for large and complex systems. The image below shows a simple example of a Function Block Diagram (FBD):









Structured Text (ST) programming language, known as SCL (Structure Language) in Siemens PLC programming, let me show you a screenshot of the program code I wrote using SCL in a certain project:
































Yes, Structured Text (SCL) is quite similar to computer programming languages. SCL's syntax is similar to high-level languages like VB (PASCAL), making it close to human thought processes and highly readable. In Siemens Step7 5.x platform, you can program using the insertion of source files, while in the TIA Portal platform, you can write code directly. SCL can be considered a "high-end" programming language in the field of industrial control. Its source code compiles to highly efficient code. If you have prior experience with high-level computer languages, it is strongly recommended to delve into learning SCL.

Instruction List (IL), referred to as Statement List (STL) in Siemens PLCs, is similar to assembly language. It demands a higher level of expertise from programmers who need to be familiar with various registers, status words inside the PLC, and understand the impact of instructions on these registers. Programs written in Statement List (STL) tend to have lower readability, but it boasts the highest execution efficiency among all languages. Some specific functionalities are difficult or impossible to express in other languages, such as ladder diagram. A few lines of code in Statement List (STL) may accomplish tasks that require much more code in other languages. Here is a screenshot of a simple STL code segment:


Sequential Function Chart (SFC), known as S7-GRAPH in Siemens PLCs, is highly suitable for the smooth control of production processes. It divides the entire production process into several "steps," each with specific actions to be completed. When certain conditions are met, it can transition to the next "step," known as "sequential control." Here is a schematic representation of S7-GRAPH code:















IEC61131-3 recommends five languages, each with its applications in various industrial control scenarios. For beginners, it is advisable to start with the Function Block Diagram (FBD) language. If you have a foundation in high-level computer language programming, consider learning the Structured Control Language (SCL). Regardless of the chosen language, a fundamental understanding of the PLC itself is essential. You need to grasp how internal resources are stored in the PLC, how programs are invoked, and once you understand these aspects, you can determine how to write programs and why they are written that way.

Originally, I only intended to write a basic introduction to PLC languages. Thinking about how the code written in these languages ultimately needs to be compiled into machine code, I then reflected on computer programming languages. This led me to provide an overview of the evolution of languages from their origins. I hope this extensive information helps you better comprehend both computer and PLC programming languages. May you experience the allure of programming languages in your future coding endeavors. This concludes my writing for now.