Mastering Pointers in CODESYS: A Guide to Efficient PLC Programming

In the world of IEC 61131-3 programming—specifically within the CODESYS environment—pointers are often viewed with a mix of curiosity and fear. To the uninitiated, they seem like a recipe for a system crash. However, when used correctly, pointers are one of the most powerful tools in a programmer’s arsenal for optimizing performance and managing complex…

In the world of IEC 61131-3 programming—specifically within the CODESYS environment—pointers are often viewed with a mix of curiosity and fear. To the uninitiated, they seem like a recipe for a system crash. However, when used correctly, pointers are one of the most powerful tools in a programmer’s arsenal for optimizing performance and managing complex data structures.

In this article, we will break down what pointers are, how to use them in Structured Text (ST), and more importantly, how to use them without crashing your PLC runtime.

What is a Pointer?

To understand a pointer, you must distinguish between a value and a memory address.

Imagine you have a house.

  • The value is the people living inside the house.
  • The address is “123 Main Street.”

A standard variable (like an INT or BOOL) stores the “people” (the value). A pointer does not store the people; it stores the “address.” It is a variable that holds the memory location of another variable.

The Three Pillars of Pointer Manipulation

To work with pointers in CODESYS (Structured Text), you must master three specific syntax elements:

1. Declaration (POINTER TO)

You cannot simply declare a pointer; you must tell the compiler what kind of data the pointer is pointing to. This ensures “type safety.”

VAR
iMyData : INT := 42; // The actual integer variable
pMyPointer : POINTER TO INT; // The pointer, designed to hold an INT address
END_VAR

2. The Address Operator (ADR)

A pointer cannot “see” a variable name; it only understands memory addresses. The ADR() function is used to extract the memory address of a variable and assign it to your pointer.

pMyPointer := ADR(iMyCode); // pMyPointer now holds the memory address of iMyCode

3. The Dereference Operator (^)

This is the most critical symbol in pointer programming. If you want to access or change the value located at the address held by the pointer, you must use the caret symbol (^).

  • pMyPointer → Returns the address (e.g., 16#1234).
  • pMyPointer^ → Returns the value stored at that address (e.g., 42).

Practical Code Example

Here is a complete implementation of a pointer manipulation in a CODESYS Program Organization Unit (POU).

PROGRAM PLC_PRG
VAR
// Target Variables
iTargetValue : INT := 55;
iResultValue : INT;
// Pointer Variable
pPointerToData : POINTER TO INT;
END_VAR
// STEP 1: Point the pointer to the address of iTargetValue
pPointerToData := ADR(iTargetValue);
// STEP 2: Safety Check (Crucial!)
// Always verify the pointer is not NULL before dereferencing
IF pPointerToData <> 0 THEN
// STEP 3: Read the value via the pointer
iResultValue := pPointerToData^; // iResultValue becomes 55
// STEP 4: Modify the value via the pointer
pPointerToData^ := pPointerToData^ + 10; // iTargetValue is now 65
END_IF

Why Use Pointers?

If pointers are dangerous, why use them? In industrial automation, efficiency is king.

  1. Passing Large Structures (DUTs): If you have a complex STRUCT containing hundreds of elements, passing that structure into a Function Block “by value” copies every single byte, consuming CPU cycles. Passing a POINTER TO that structure only copies a few bytes (the address), making your code significantly faster.
  2. Array Manipulation: Pointers allow for advanced array traversal and the creation of circular buffers for data logging.
  3. Hardware Interfacing: When communicating with low-level hardware drivers or shared memory, pointers allow you to map software variables directly to specific hardware registers.

The Danger Zone: Best Practices

In a PC environment, a pointer error might crash an application. In a PLC environment, a pointer error can halt the entire CPU, potentially stopping a production line or causing hardware damage.

1. The Null Pointer Check

Never use the ^ operator without first verifying that the pointer is not 0 (NULL). Bad: x := pPtr^; Good: IF pPtr <> 0 THEN x := pPtr^; END_IF

2. Avoid “Dangling Pointers”

A dangling pointer occurs when you point to a variable that no longer exists (for example, a local variable inside a Function that has finished executing). Always ensure the “target” variable has a longer lifecycle than the pointer.

3. Type Consistency

Never use a POINTER TO INT to try and read a REAL. While the PLC might not crash immediately, you will be reading “garbage” data, leading to logic errors that are incredibly difficult to debug.

Conclusion

Pointers are a high-level tool for high-performance programming. By mastering the ADR and ^ operators and implementing strict safety checks, you can leverage pointers to write more efficient, professional, and scalable CODESYS applications.

Tags:

Leave a comment