Evaluate the AutomationView desktop suite free for 30 days. No credit card required. Claim trial key →
arrow_back Back to Articles

Inside CODESYS V3 OOP: A Comprehensive Guide for PLCs

calendar_month
person Carvalho Raphael

Inside CODESYS V3 OOP: A Comprehensive Guide for PLCs

Learning
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    classDef main fill:#2563eb,stroke:#fff,stroke-width:2px,color:#fff
    classDef secondary fill:#16a34a,stroke:#fff,stroke-width:2px,color:#fff
    classDef tertiary fill:#dc2626,stroke:#fff,stroke-width:2px,color:#fff

    A["CODESYS V3 OOP"]:::main --> B["Function Blocks"]:::secondary
    A --> C["Interfaces"]:::secondary
    A --> D["Inheritance"]:::secondary
    B --> E["Methods & Properties"]:::tertiary
AutomationView Icon AutomationView

Transitioning from procedural ladder logic to Object-Oriented Programming often feels like a leap of faith for seasoned automation engineers. Yet, scaling complex machinery requires more than massive, monolithic programs scattered with global variables. By leveraging CODESYS V3 OOP, which strictly adheres to the IEC 61131-3 standard, engineers can build modular, testable, and reusable PLC code. This shift not only accelerates commissioning but also drastically reduces the painful troubleshooting hours spent tracing rogue signal overwrites on the plant floor.

Key Takeaways

  • Function Blocks act as true classes, encapsulating both data and logic.
  • Interfaces enable polymorphism, standardizing equipment interactions across different vendors.
  • Inheritance cuts down on duplicated logic across similar machine modules.

The Core Elements of CODESYS V3 OOP

When deploying CODESYS V3 OOP, the structural paradigm revolves around familiar IT concepts adapted for industrial determinism. Organizing your logic correctly from the start prevents architectural bottlenecks as the machine complexity grows.

Function Blocks as Classes

In the IEC 61131-3 standard, Function Blocks (FBs) are the closest equivalent to classes. They encapsulate both state (variables) and behavior. Instead of scattering logic across multiple routines, you consolidate it within the FB. This means an FB representing a pneumatic cylinder holds its own state, timers, and specific alarm logic independently from the rest of the program.

Methods and Properties

To interact with these FBs cleanly, we use Methods and Properties.

  • Methods are used to execute actions, such as starting a motor profile or calculating a PID response. They run their code sequentially and finish execution within the same scan cycle.
  • Properties act as safe getters and setters, hiding the internal implementation details of your variables. This strict access control prevents external routines from accidentally corrupting critical states.

Establishing Hierarchies and Contracts

Real-world automation demands dealing with hundreds of similar devices like valves, drives, and sensors. Hardcoding interactions for each unique device creates bloated programs that are nearly impossible to maintain.

Inheritance to Reduce Duplication

Using the EXTENDS keyword, you can create a base FB (for example, a generic Valve) and inherit from it to create specialized versions (for example, a Proportional Valve). The child inherits all logic, and you only write the new or modified code. This prevents the nightmare of updating ten different valve blocks when a core bug is discovered. Pointers like THIS (referencing the current instance) and SUPER (referencing the parent FB) are invaluable here for calling the correct methods within the hierarchy.

Interfaces and Polymorphism

Interfaces (using the keywords INTERFACE and IMPLEMENTS) define strict contracts. If you define an IDrive interface with a Start() method, any FB implementing it must provide that method. This allows you to pass different drive brands to a single control routine, achieving polymorphism. The control routine only knows it is talking to an IDrive, entirely decoupling the high-level logic from the hardware-specific details.

flowchart TD
    classDef interface fill:#dc2626,stroke:#fff,stroke-width:2px,color:#fff
    classDef base fill:#2563eb,stroke:#fff,stroke-width:2px,color:#fff
    classDef child fill:#16a34a,stroke:#fff,stroke-width:2px,color:#fff

    A["Interface: IDrive"]:::interface -->|Implemented By| B["FB: GenericDrive"]:::base
    B -->|Extends| C["FB: ServoDrive"]:::child
    B -->|Extends| D["FB: VFD"]:::child

Access Specifiers for Safe State Management

One of the largest causes of machine faults is unintended variable modification from random parts of the program. Access Specifiers allow you to lock down your data and enforce clear boundaries.

Specifier Accessibility Rules
PUBLIC Accessible from anywhere in the application. This is the default state for Methods and Properties.
PRIVATE Accessible only within the FB itself. Completely hidden from inherited blocks.
PROTECTED Accessible within the FB and any child FB that EXTENDS it. Ideal for internal logic sharing.
INTERNAL Accessible only within the current namespace or library.

Conclusion and Next Steps

Adopting CODESYS V3 OOP transforms a brittle, sprawling PLC program into a robust, maintainable architecture. While the initial learning curve is steeper than procedural programming, the long-term benefits in code reuse and standardized machine modules are immense. By mastering Function Blocks, Inheritance, and Interfaces, you future-proof your industrial control systems.

To further elevate your engineering toolkit and find pre-built, optimized components, explore the AutomationView Store for proven templates and advanced logic modules.

Frequently Asked Questions

Can I mix procedural code with OOP in CODESYS?

Yes. The IEC 61131-3 standard allows you to mix standard ladder logic or structured text with OOP concepts. You can wrap legacy code inside an FB or call procedural functions from within methods to transition gradually.

Does OOP negatively impact PLC scan time?

Modern PLCs handle OOP overhead efficiently. While there is a slight performance cost when calling methods compared to inline logic, the architectural benefits almost always outweigh the negligible microsecond differences in execution time. Proper design actually improves overall cycle times by avoiding unnecessary logic execution.

Share this article

Stay Updated with Learning

Get the latest articles and news delivered directly to your inbox.

Log in to Subscribe

You must be registered and logged in to manage subscriptions.

Recommended for you

Structured Text vs Ladder Logic: 5 Essential PLC Tips

Learning
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    A{PLC Programming}
    A -->|Visual| B[Ladder Logic]
    A -->|Textual| C[Structured Text]
    B --> D[Machine Sequence]
    C --> E[Data Processing]
AutomationView Icon AutomationView
calendar_month

Structured Text vs Ladder Logic: 5 Essential PLC Tips

The Reality of PLC Programming Choices For decades, the factory floor has been dominated by relay-style diagrams. Yet, as automation systems scale to handle array processing, MQTT communications, and multi-axis kinematics, limiting a project to Ladder Logic can severely handicap development speed. Conversely, writing a machine’s main safety sequence entirely in Structured Text can frustrate […]

Read Article arrow_forward

Inside Studio 5000 Add-On Instructions: A Comprehensive Guide

Learning
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    classDef block fill:#2563eb,stroke:#60a5fa,stroke-width:2px,color:#ffffff
    classDef io fill:#16a34a,stroke:#4ade80,stroke-width:2px,color:#ffffff
    
    in_params["Input Parameters"]:::io --> aoi["Studio 5000 Add-On Instructions"]:::block
    aoi --> out_params["Output Parameters"]:::io
    aoi --> local["Local Tags Encapsulation"]:::block
AutomationView Icon AutomationView
calendar_month

Inside Studio 5000 Add-On Instructions: A Comprehensive Guide

Implementing modular code on the plant floor is not just about writing clean logic; it is about surviving commissioning at 3 AM when the line is down. At the heart of modular Rockwell Automation programming lies a powerful but sometimes polarizing tool: Studio 5000 Add-On Instructions. While they offer unparalleled encapsulation and code reusability, they […]

Read Article arrow_forward

Inside IEC 61499: A Tutorial on Event-Driven Distributed Control

Learning
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    classDef blueFill fill:#2563eb,stroke:#1d4ed8,color:#ffffff
    classDef greenFill fill:#16a34a,stroke:#15803d,color:#ffffff
    classDef redFill fill:#dc2626,stroke:#b91c1c,color:#ffffff

    event["Event Trigger"]:::blueFill --> fb1["IEC 61499 Function Block"]:::greenFill
    fb1 --> out["Distributed Action"]:::redFill
AutomationView Icon AutomationView
calendar_month

Inside IEC 61499: A Tutorial on Event-Driven Distributed Control

Key Takeaways: IEC 61499 replaces the cyclic scan of traditional PLCs with an event-driven execution model. The standard separates the application logic from the underlying hardware, enabling true platform independence. Engineers can design object-oriented, encapsulated function blocks distributed across smart devices, edge gateways, and controllers. Transitioning from the cyclic logic of IEC 61131-3 to the […]

Read Article arrow_forward