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

Libraries — Write Once, Use Anywhere

Usually, when code is developed by a third party, it is shipped as a library. In CODESYS, a library is precompiled code that is designed to augment your code. In traditional programming, libraries are everywhere and are the backbone of most projects. In short, libraries offer a way to interface with unique hardware or add advanced functionality to a project without you having to worry about developing code for it.

Libraries are everywhere. If you ever wondered how Android and iPhone devices talk to hardware such as glucometers, household robots, and so on, the answer is libraries. You will not be able to function as a programmer without understanding libraries. Without libraries, you will not be able to talk to custom hardware, such as motor drives, encoders, and so on. In short, you will be very limited in what you can do as a programmer if you don’t know how to use libraries.

In this chapter, we will explore the following:

...

Technical requirements

As per all previous chapters, this chapter will require nothing special other than a copy of CODESYS installed on your machine. If you have skipped ahead and have not read the past chapters, you will need to download and install a copy of CODESYS. The code for this project and all other examples can be found at the following URL: https://github.com/PacktPublishing/Mastering-PLC-programming/tree/master/Chapter%208/Library.

Investigating libraries

We have touched on libraries a bit in the introduction. However, there is a lot to libraries, and an in-depth knowledge of how they work is needed before we can proceed.

A library is a prebuilt code that can augment your code by allowing you to easily talk to hardware, perform networking, and so on, without having to write code. In many cases, especially when it comes to proprietary systems such as hardware components, it would be difficult or impossible to effectively write code to interface with it. Many hardware manufacturers will simply provide a library to interface with the device.

Why do we need libraries?

With that in mind, what is the purpose of a library? Libraries exist for multiple reasons, including the following:

  • To avoid developing the same functionality multiple times
  • To interface with custom or proprietary components
  • To augment existing code with third-party libraries
  • To distribute code to other developers
...

Guiding principles for library development

Developing an effective library can be tricky. Where you have a clear-cut application in mind when developing a PLC program for a machine, developing a library will be a bit different. When you’re developing a library, you have to think of everything at a very generic level. You will not know ahead of time who will use the library, how they will use the library, or what they will use the library for. Hence, creating a good library can be a very tricky and daunting task. There are no clear-cut ways to create a perfect library but there are a few rules that I came across that have helped me develop some decent libraries.

Rule 1 – Keep it simple, stupid (KISS)

KISS is the golden rule for many programmers. When it comes to developing libraries, you must keep it as simple as possible. Generally, you need to have a very clear issue in mind that the library is designed to solve. Now, libraries can have many different functionalities...

Building custom libraries

By exploring third-party libraries and the guiding principles of developing libraries, we have touched on building custom libraries. So, before we move on and attempt to build a working math library, we are going to develop a simple library using some of the principles we have learned so far in the book.

Requirements

For this project, we are going to build a simple library that can perform the following functions:

  • Home the motor
  • Turn the motor on
  • Turn the motor off
  • Stop the motor
  • Position the motor

In short, this will be a very simple library and will not require complex architecture. For a library as simple as this, we don’t have to worry too much about complexities such as design patterns; however, the facade pattern may help a little. Turning the motor off and on will be a bit more complex as we will need to automatically zero the motor out, which means that the homing function will also need to zero out. Let...

Final project – part computation library

In automation, it is common to have to program PLCs to keep track of many aspects of the job that is running. For example, it is common for a plant to want to know the number of parts in a job, and the total amount of parts that were rejected. For an operation like this, the calculations are never going to change. Writing the calculation for each different machine is rather pointless and redundant. Depending on what you work on, it may be best to just create a library and consume the library in multiple projects. Our first order of business is to figure out our requirements for the project.

Requirements

The first step in developing a library is to gather the requirements. For this project, we need to create a library that will need to do the following:

  • Compute the total parts created for a job
  • Compute the number of parts lost for a job

With these requirements, we can deduce that we will need the following methods...

Summary

In this chapter, we have explored libraries. We have learned what they are, how to use them, what third parties are, basic development principles, and so on. You should now be able to use libraries from external sources or create your own. What you will find is that by using libraries, you can now truly port code to different projects and cut down on your overall development time and effort.

There is a lot more to libraries, such as namespaces and so on, that was not explored in this chapter. A whole book could be dedicated to this subject. It is recommended that you explore libraries more on your own, as this chapter was just a crash course to get you familiar with the concept and consumption of libraries.

After covering many of the foundational elements of developing PLC software, we need to start exploring the software development life cycle so we can better manage the development of not only libraries but also entire codebases.

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. What is a library?
  2. Why is documentation important?
  3. How is a library imported?
  4. What are some good design patterns to use in a library?
  5. What is the difference between (**) and /// in library documentation?
  6. Go back and document the final project.

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 €14.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