'Are there something like classes in Structured Text?

I'm working with a Beckhoff PLC and I am trying to make a couple of POU's that are exactly the same except for the IO. I am looking for a way so I have to write the code only ones but be able to run it more than ones with different IO. For example something like classes where the IO is declared in the constructor. The different POU's also have to run simultaneously. I have already looked a little bit into a sequential function chart and function blocks with inheritance but that doesn't seem to work the way I want. I'm not even sure what I want is possible.



Solution 1:[1]

Function blocks are the closest thing you'll get to the equivalent in classes. The closest thing to a constructor in the Beckhoff world is FB_Init (https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/63050399827943947.html&id=).

How more specifically do you want things to work?

Solution 2:[2]

Have a look at the following link (especially the links at the bottom) to get an overview of how to use the function blocks in an object oriented way: https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/63050399827943947.html&id=

When you use %I and %Q inside a function block, you will get linkable variables for each of your instances of the block. This is the case both when using an FB as an object in TC3 or when you use it just as a function block in TC2. (The FB will contain the IO)

Another solution is to make an IO-object fitting your requirements and then pass its interface pointer to the function block that handles it. (The FB will work on IO declared parallel to it, e.g. in MAIN)

Solution 3:[3]

The solution to your task is really laying based on IDE you are working on. Some IDE does not support methods or extends in function blocks some does. So, the level of OOP implemented depends on IDE.

But yes, generally you can use function blocks as class objects. But you must take in account that you cannot dynamically change number of input or output variables, or their types. Another words, yes you can look at FB as a class, but it is not.

I think if you transition to ST from C/C++ like languages you need to change the way you think. ST is different. You better think more like procedural approach. The principles I use to create PLC independent program or universal program, most often use pointers. But again, without knowing your task and IDE it is hard to suggest.

The problem with questions like this, you have already decided what to use to solve your task and you ask how to use it, when you had to ask how to solve task like this and what to use and explain a task itself. Because it is possible that your task is classically solved using completely different tools that OOP in PLC world.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jakob
Solution 2 pboedker
Solution 3 Sergey Romanov