Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Mastering PLC Programming
Mastering PLC Programming

Mastering PLC Programming: The software engineering survival guide to automation programming , Second Edition

Arrow left icon
Profile Icon M. T. White
Arrow right icon
$39.99 $49.99
Paperback Jan 2026 528 pages 2nd Edition
eBook
$35.99 $39.99
Paperback
$39.99 $49.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon M. T. White
Arrow right icon
$39.99 $49.99
Paperback Jan 2026 528 pages 2nd Edition
eBook
$35.99 $39.99
Paperback
$39.99 $49.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$35.99 $39.99
Paperback
$39.99 $49.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Mastering PLC Programming

Advanced Structured Text: Programming a PLC in Easy-to-Read English

Software engineering is a pivotal, yet often overlooked, aspect of programmable logic controller (PLC) programming. PLC software development often takes a backseat to hardware development. Unfortunately, many in the modern automation landscape see PLC software as a disposable component. Contrary to this belief, the software that controls the PLC is the true heart and soul of the system. The cold reality is that, without properly written software, fancy hardware is little more than very expensive paperweights.

Object-oriented programming (OOP) has dominated the IT landscape for decades. Most general-purpose programming languages, such as Java, C++, C#, Python, and so on, support the paradigm. Even some functional-first programming languages, such as Microsoft’s F#, support OOP to some extent. Though often overlooked in the automation world, certain PLCs that follow the IEC 61131-3 standard can utilize the...

Free Benefits with Your Book

Your purchase includes a free PDF copy of this book along with other exclusive benefits. Check the Free Benefits with Your Book section in the Preface to unlock them instantly and maximize your learning experience.

Technical requirements

To get the most out of this chapter, a Windows computer and a working copy of an IEC 61131-3-compliant programming environment that supports OOP will be needed. For this book, the recommended programming system is CODESYS: https://us.store.codesys.com/.

This chapter will have multiple code examples. The code examples can be downloaded from GitHub by following the link: https://github.com/PacktPublishing/Mastering-PLC-Programming-Second-Edition.

The projects will utilize CODESYS and the book will assume you’re using that system. If you opt to use a different programming system, you will need to copy and paste the examples into the system of your choice. For the most part, an IEC 61131-3-compliant system should require minimal to no modifications to the source code; however, this will depend on which feature the system has adopted and how the vendor chose to implement the features. Regardless, the principles and techniques explored in this book...

Exploring the IEC 61131-3 standard

Most advanced PLCs are IEC 61131-3-compliant to some degree, especially if they are PC-based, such as Beckhoff PLCs. IEC 61131-3 is a standard that essentially governs the programming environments that the PLC supports and the general functionality for the programming system. This means aspects such as syntax, semantics, typing, memory management, error handling, modularity, code organization, and so on should be mostly consistent between PLCs. The standard also governs the following programming languages:

  • ST
  • LL
  • Sequential Function Chart (SFC), often called Sequential Flow Charts in slang
  • Function Block Diagram (FBD)

The IEC 61131-3 standard is just that, a standard. IEC 61131-3 is not a programming language, PLC brand, or anything of the sort. The standard promotes uniformity across programming systems and ultimately promotes vendor neutrality.

Though the standard is vendor-neutral, you typically cannot...

Needed software and learning approach

The biggest hurdle to being a programming instructor is getting students to understand that the key to being a good engineer is not memorizing patterns or programming commands. To be successful as a developer, whether it be a developer for traditional apps or PLCs, lies in the ability to take established programming principles and apply them to any system. The key to mastering the material presented in this book is to approach it from a software engineering perspective. As you go through this book you will be tempted to think the material presented is only for CODESYS or even PLCs in general. However, this is a misnomer as the techniques explored in this book can be used with any OOP based language or system.

Programming software

To follow along with this book, an IEC 61131-3 programming environment that supports OOP will be required. The CODESYS environment will support most, if not all, of the IEC 61131-3 features and is free to download...

Error handling

Errors can kill the execution of a program, which, in turn, can lead to injury or death in extreme cases. Exceptions occur when the PLC encounters a problem that it cannot handle at runtime. When one such error occurs, the PLC program will lock up or crash, and the PLC will typically need to be rebooted. On top of all that, if the condition that caused the error occurs again, the program will again crash and cause another lock up. In essence, the only safe way to handle the condition is to modify the code to ensure that the erroneous condition is handled gracefully.

Exception errors will not show up during the compilation process. Instead, exceptions occur when the program is running. Due to their nature, it is often difficult or impossible to fully predict when an exception will occur. To make matters worse, some exceptions can take very specific conditions to trigger, and, as a result, there might be long intervals between occurrences. To compound the issue, certain...

Understanding pointers

To understand a pointer, it is first necessary to understand the basics of how variables are stored in memory. For many PLC programmers, creating a variable or a tag is simply inputting a name and assigning it a data type; however, some mechanics take place under the hood. For starters, a variable is much more than just a name and a data type that holds a value. A variable is a dedicated memory block that the computer (in this case, the PLC) uses to hold a value of a specific type. The memory block is generally not human-readable; as such, the variable name is just a human-readable façade that makes accessing and manipulating the data in the memory block easy while adding context to the value.

Representing PLC memory

Figure 1.7 is a graphical representation of a PLC’s memory. It is a simplified way of conceptualizing how the PLC sees its memory addresses and the values that reside in those blocks:

Figure 1.7: A graphical representation of computer memory

Figure 1.7: A graphical representation...

State machines

State machines sound like a complex and scary concept. Depending on the context of the state machine, they can be complex; however, fundamentally, they aren’t. Fundamentally, a state machine can be defined as a structure where some type of input and a current state dictate some type of output state.

There are many types of state machines. In terms of automation, the most common is what’s known as a finite state machine (FSM). Essentially, an FSM is a state machine with a finite number of states. In a very basic sense, there are a few key components to an FSM:

  • Finite number of states: A state can be thought of as a mode. In terms of automation, a mode can be something like a machine being in an on, off, or standby mode.
  • Transitions: A transition can be thought of as the change from one state to another. For example, a machine going from on to off can be thought of as a transition.
  • Input: An input can be some type of action or...

Expert systems

In the early days of artificial intelligence (AI), when computer and data science weren’t nearly as fleshed out, there was an AI technique called expert systems or expert machines. Essentially, these systems were a series of IF-THEN statements that, when executed correctly, could provide a level of expertise in a given system. Nowadays, these systems aren’t used as much in the traditional programming landscape due to the advances of actual AI systems, such as deep learning and the like. Nonetheless, expert systems can still have a great effect in the automation industry.

An expert system is a program or functionality of a program that is used to simulate human-like judgment and decision-making. Essentially, you can think of these systems as simulated experts for a domain. For example, suppose you have a cutting machine; you can use an expert system to automatically adjust the saw blade in relation to the consistency of the material.

Though traditional...

Final project: Making a simple state machine

For our final project, we’re going to make a simple FSM that will control the state of a machine that is used to produce a certain number of parts. The machine will have multiple states, such as an on, off, and error state. To begin, let’s design our state machine!

State machine design

To begin, let’s jot out some pseudocode to get a basic feel for the general skeleton of the system:

Case state
1:
    Machine off
2:
    Production run (on)
    If error detected
        state = 3
3:
    Error state
    state = 1

As we can see, all we have are three states: on, off, and error. If an error is detected during a production run, the machine will transition to an error state. With the general logic in place, let’s implement the code.

Variables for the state machine

These are the variables that will be used for the state machine:

PROGRAM PLC_PRG
VAR
    machineState     : INT := 1;
    motorSpeedCutOff...

Chapter challenge

As a chapter challenge, integrate an expert system into the state machine. Add an extra state that will house a simple knowledge base that can determine whether the user inputted an invalid value when an error occurred. Be sure to add variables as needed to support the expert system!

Summary

This chapter has been an introduction to some of the more complex topics that you may encounter as a PLC programmer. We explored some advanced features of ST, such as pointers and error handling, as well as some complex topics such as state machines and expert systems. So far, we have only touched on the basics of what ST in the IEC 61131-3 standard can do. In the next chapter, we’re going to explore how to get the most out of variables!

Questions

  1. What are three keywords that can be used with a TRY-CATCH block?
  2. What is an expert system?
  3. What is the difference between a fact and a rule?
  4. What is a state machine transition?
  5. What is an FSM?
  6. What is the main purpose of a TRY-CATCH block?
  7. What are some applications for an expert system?

Further reading

Have a look at the following resources to further your knowledge:

Get This Book’s PDF Version and Exclusive Extras

Scan the QR code (or go to packtpub.com/unlock). Search for this book by name, confirm the edition, and then follow the steps on the page.

Note: Keep your invoice handy. Purchases made directly from Packt don’t require an invoice.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Deep dive into object-oriented PLC programming through hands-on, cutting-edge topics
  • Explore software engineering concepts like SDLC, debugging, version control, and SOLID programming
  • Get a thorough grasp on HMI development to build various HMI projects
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

Object-oriented programming (OOP) is a game-changing addition to PLC programming that’s revolutionizing the automation industry. This book equips you with the necessary skills to succeed in the modern automation programming environment. This second edition is designed to systematically guide you through advanced topics such as OOP design, SOLID programming, the software development life cycle (SDLC), library design, version control, HMI development, general software engineering practices, and more. To hone your programming skills, each chapter has a simulated real-world project that'll enable you to apply the skills you've learned. In all, this book not only covers complex PLC programming topics but also removes the financial barrier that comes with most books, as all examples utilize free software. This means that to follow along, you do not need to purchase any PLC hardware or software. By the end of this PLC book, you’ll have what it takes to create long-lasting codebases for any modern automation project. *Email sign-up and proof of purchase required

Who is this book for?

This book is for automaton programmers with a background in software engineering topics such as object-oriented programming and general software engineering knowledge. Automation engineers, software engineers, electrical engineers, PLC technicians, hobbyists, and upper-level university students with an interest in automation will also find this book useful and interesting. Anyone with a basic knowledge of PLCs can benefit from reading this book.

What you will learn

  • Find out how to write PLC programs using advanced programming techniques
  • Explore OOP and UML concepts for PLC programming
  • Delve into software engineering topics such as libraries and SOLID programming
  • Explore HMIs, HMI controls, HMI layouts, and alarms
  • Grasp Industry 4.0 concepts to create smart, interconnected, and automated industrial systems
  • Gain hands-on experience by building simulated PLC and HMI projects
  • Get hands-on with using version control
Estimated delivery fee Deliver to Russia

Economy delivery 10 - 13 business days

$6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 30, 2026
Length: 528 pages
Edition : 2nd
Language : English
ISBN-13 : 9781836642558
Category :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Russia

Economy delivery 10 - 13 business days

$6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Jan 30, 2026
Length: 528 pages
Edition : 2nd
Language : English
ISBN-13 : 9781836642558
Category :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Table of Contents

25 Chapters
Part I: Advanced Structured Text Chevron down icon Chevron up icon
Advanced Structured Text: Programming a PLC in Easy-to-Read English Chevron down icon Chevron up icon
Complex Variable Declaration: Using Variables to Their Fullest Chevron down icon Chevron up icon
Functions: Making Code Modular and Maintainable Chevron down icon Chevron up icon
Object-Oriented Programming: Reducing, Reusing, and Recycling Code Chevron down icon Chevron up icon
OOP: The Power of Objects Chevron down icon Chevron up icon
Best Practices for Writing Incredible Code Chevron down icon Chevron up icon
Libraries: Write Once, Use Anywhere Chevron down icon Chevron up icon
Part II: Software Engineering for Automation Chevron down icon Chevron up icon
Getting Started with Git Chevron down icon Chevron up icon
SDLC: Navigating the SDLC to Create Great Code Chevron down icon Chevron up icon
Architecting Code with UML Chevron down icon Chevron up icon
Testing and Troubleshooting Chevron down icon Chevron up icon
Advanced Coding: Using SOLID to Make Solid Code Chevron down icon Chevron up icon
Part III: HMI Design Chevron down icon Chevron up icon
Industrial Controls: User Inputs and Outputs Chevron down icon Chevron up icon
Layouts: Making HMIs User-Friendly Chevron down icon Chevron up icon
Alarms: Avoiding Catastrophic Issues with Alarms Chevron down icon Chevron up icon
Part IV: Putting Knowledge Into Action Chevron down icon Chevron up icon
DCSs, PLCs, and the Future Chevron down icon Chevron up icon
Putting It All Together: The Final Project Chevron down icon Chevron up icon
Unlock Your Exclusive Benefits Chevron down icon Chevron up icon
Answer Sheet Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon