Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering PLC Programming

You're reading from  Mastering PLC Programming

Product type Book
Published in Mar 2023
Publisher Packt
ISBN-13 9781804612880
Pages 386 pages
Edition 1st Edition
Languages
Author (1):
Mason White Mason White
Profile icon Mason White

Table of Contents (25) Chapters

Preface 1. Part 1 – An Introduction to Advanced PLC Programming
2. Chapter 1: Software Engineering for PLCs 3. Chapter 2: Advanced Structured Text — Programming a PLC in Easy-to-Read English 4. Chapter 3: Debugging — Making Your Code Work 5. Chapter 4: Complex Variable Declaration — Using Variables to Their Fullest 6. Part 2 – Modularity and Objects
7. Chapter 5: Functions — Making Code Modular and Maintainable 8. Chapter 6: Object-Oriented Programming — Reducing, Reusing, and Recycling Code 9. Chapter 7: OOP — The Power of Objects 10. Part 3 – Software Engineering for PLCs
11. Chapter 8: Libraries — Write Once, Use Anywhere 12. Chapter 9: The SDLC — Navigating the SDLC to Create Great Code 13. Chapter 10: Advanced Coding — Using SOLID to Make Solid Code 14. Part 4 – HMIs and Alarms
15. Chapter 11: HMIs — UIs for PLCs 16. Chapter 12: Industrial Controls — User Inputs and Outputs 17. Chapter 13: Layouts — Making HMIs User-Friendly 18. Chapter 14: Alarms — Avoiding Catastrophic Issues with Alarms 19. Part 5 – Final Project and Thoughts
20. Chapter 15: Putting It All Together — The Final Project 21. Chapter 16: Distributed Control Systems, PLCs, and Networking 22. Assessments 23. Index 24. Other Books You May Enjoy

Debugging — Making Your Code Work

Chances are you have never written a program of any significant size that worked as expected on the first go. In fact, chances are you hardly ever get a program to compile and run on the first go. Every software engineer knows that defects are a part of life. As such, debugging is a part of life as well.

Debugging is a skill. Just as a programmer must learn to write code, they must also learn to debug software. A developer can be the best developer in the world; however, if they cannot effectively debug their software, no one is going to consider them very effective. Just as there are techniques to develop code, there are techniques that can be used to debug software.

There are many different ways to debug software. Some methods are more sophisticated than others. It doesn’t matter what method you choose to debug your software as long as the software is defect-free when you deploy it. Many tools can be used to troubleshoot code...

Technical requirements

Unlike Chapter 2, this chapter will focus on debugging code as opposed to developing code. Code examples will be provided in the text; however, it is recommended that you pull down the code from GitHub. The source code for this chapter can be downloaded at this link: https://github.com/PacktPublishing/Mastering-PLC-programming/tree/master/Chapter%203.

What is debugging?

Debugging a program starts with understanding what a bug is. A bug is best thought of as a software defect. Bugs range in severity—some bugs might produce minor inconveniences in a program, such as producing the wrong text, while severe bugs will prevent a program from compiling. When a bug is detected, it is important to find the bug and repair it; this act is what is known as debugging.

Debugging is as much an art as it is a science. Debugging is the act of finding and eliminating defects in software. As was discussed earlier, defects are a given for a program of any significant size and functionality. As such, it is important for you, as a developer, to be able to troubleshoot defects. The following section is dedicated to understanding bugs and the debugging process.

Types of bugs

Depending on who you ask and what article you read, there are many types of bugs. However, the following types of bugs are arguably the most common types you will run...

Understanding debugging tools and techniques

There are many different tools and techniques that can be used to debug a program. As was discussed at the beginning of the chapter, some techniques are more sophisticated than others. It doesn’t matter which technique you use as long as you debug the software and it works as intended. As such, the following section will explore some ways to track down problems in your code.

Print debugging

The easiest way to debug a program is with print statements. Print debugging is used to isolate problems; in other words, this technique will help you find the offending code. The IEC 61131-3 doesn’t support a command that will output to a console or screen the same way languages such as Java or C++ do. However, this technique can still be used in PLC programming, and in some regards, it is a little easier to use. To demonstrate the use of print debugging, we are going to create a simple program that toggles a variable to TRUE when...

Troubleshooting – a practical example

When working with motors, it is sometimes necessary to incrementally stop a motor. Sometimes this is due to the process, while other times it is due to the motor or component. To demonstrate practical troubleshooting, recreate the state machine from Chapter 2 with the following modifications, which are the necessary variables to power the new iteration of the state machine:

PROGRAM PLC_PRG
VAR
     machineState          : INT := 1;
     motorSpeedCutOff      : INT := 10000;
     runTime               : INT := 2;
     setSpeed              : REAL;
     numOfParts     ...

Summary

In this chapter, we explored debugging. Debugging is something that is overlooked by developing programmers and is a skill that is often learned by trial and error. The main takeaway from this chapter is that debugging is a skill much like coding and you have to practice it to hone it. As we have seen, much like the way software development is a process, so is debugging.

Many different tools and techniques can be used to help debug programs. However, the greatest tool that a developer has at their disposal is critical thinking. As we practiced with the state machine, you don’t always need tools such as debuggers. Though they are widely used tools that you should master, it is oftentimes just as effective to use print debugging and deductive reasoning to troubleshoot code. In short, as we move forward with an in-depth look at variables, it is important to note that you will continue to use debugging.

Questions

Answer the following questions based on what you've learned in this chapter. Cross-check your answers with those provided at the end of the book, under Assessments.

  1. Define print debugging.
  2. Define interactive debugging.
  3. Define the debugging process.
  4. What types of bugs were found in the practical example?

Further reading

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

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mastering PLC Programming
Published in: Mar 2023 Publisher: Packt ISBN-13: 9781804612880
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}