Reader small image

You're reading from  Mastering PLC Programming

Product typeBook
Published inMar 2023
PublisherPackt
ISBN-139781804612880
Edition1st Edition
Right arrow
Author (1)
Mason White
Mason White
author image
Mason White

M.T. White has been programming since the age of 12. His fascination with robotics flourished when he was a child programming microcontrollers such as Arduinos. M.T. currently holds an undergraduate degree in mathematics, a master's degree in soft ware engineering, and is currently working on an MBA in IT project management. M.T. is currently working as a soft ware developer for a major US defense contractor and is an adjunct CIS instructor at ECPI University. His background mostly stems from the automation industry where he programmed PLCs and HMIs for many different types of applications. M.T. has programmed many different brands of PLCs over the years and has developed HMIs using many different tools.
Read more about Mason White

Right arrow

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 2023Publisher: PacktISBN-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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Mason White

M.T. White has been programming since the age of 12. His fascination with robotics flourished when he was a child programming microcontrollers such as Arduinos. M.T. currently holds an undergraduate degree in mathematics, a master's degree in soft ware engineering, and is currently working on an MBA in IT project management. M.T. is currently working as a soft ware developer for a major US defense contractor and is an adjunct CIS instructor at ECPI University. His background mostly stems from the automation industry where he programmed PLCs and HMIs for many different types of applications. M.T. has programmed many different brands of PLCs over the years and has developed HMIs using many different tools.
Read more about Mason White