CODESYS Pointers and Dereferencing

Pointers and Arrays in CODESYS

The CODESYS language supports the usage of pointers which is essentially a variable that stores the location of another variable or data point instead of its value. The actual value of a variable that a pointer points to can be retrieved by dereferencing the pointer.

 

What could it be used for?

       1.Storing the location of the variable saves space specifically when pointing to large                 data points or structures.

  1. Pointers allow for sorting and re-arrangement of data sequences without actually changing the location of the original data points. To characterize in a diagram:

Pointers and Arrays Diagram

  1. Going down the path of object oriented programming in PLC’s/PAC’s, this supports abstraction by separating the usage, manipulation and parts of an object from the original object itself. This helps reduce the PLC code required to handle every scenario and also helps in situation where the original object can not be manipulated because its hardcoded to an input ( for example).

 

Example:

 

Let’s say the array of data on the left side of the diagram above is read (as inputs) from a set of 4 drives controlling 4 pumps. From top of the array to the bottom Array location 0 would be for Pump 1 , location 1 for Pump 2 …..location 4 for Pump 4.

 

If the data is each of the pumps runtime, the pointers to the runtime allow for it to be sorted and stored in the pointer of arrays without changing the order of the original array which is important, as the original array of runtime data may be hardcoded to the actual input or read values from the drives.

 

Secondly, when the decision is made to call a pump to run, the same mechanism of pointers and arrays and de-referencing can be used to point to the command word of each of the pumps on a communication bus.

The end result is the amount of code required in determining which of the pumps to run is reduced.

Pump Example Pointers and Arrays

What does the syntax look like in CODESYS?

To create a pointer:

The address operator or ADR is used.

Let’s say the variables are:

 

PROGRAM PLC_PRG

VAR

j: INT;

k:INT;

pointToJ: POINTER TO INT;

END_VAR

The pointer assignment would be:

 Pointer

in plain text:

pointToJ:=ADR( j);

Note: ADR is a CODESYS operator and not an operator prescribed in IEC61131-3.

 

To de-reference a pointer:

The ^ operator is used to de-reference pointers.

Example:

in plain text:

k:=pointToJ^;

This would yield the value of integer j.

SimulatedPointerDeReferencing

 

Share

One thought on “CODESYS Pointers and Dereferencing

Leave a Reply

Your email address will not be published. Required fields are marked *