You are on page 1of 15

Drives & Systems

PLC's, PAC's, Variable Speed Drives and Industrial


Automation

Skip to content

 Home
 About
 VFD’s
 PLC’s
 Links and Resources

Search for:

CODESYS Declaration Examples: Variables,


Arrays, Function Blocks…
June 13, 2016CODESYS, PAC, PLC, Programming, SoMachine

There are many ways to implement variable declaration in CODESYS. It can be easy to forget
the syntax or specifics on some of them. This post covers them for reference. Also, a couple of
programmer cautionary comics from XKCD at the bottom… 😀
Variable Declarations:

VarName : INT ;

Variable with located memory address

VarName AT % MW123: INT;

Variables with Initial Value:

VarName : INT := 23;

Variable initial values can be tied to another variable of the same data type:

VarName: INT:= VarName2;

where:

VarName2:= INT;

Declaring STRING with defined length:

VarName2 :STRING(40);

With the above, a string of 40 characters is created. Each character is one byte.

Declaring STRING with initial assignment:

VarName :STRING:= ‘HELLO WORLD’;

Declaring Arrays with Initial Value:

Array1: ARRAY[1..5] OF INT := [1,2,3,4,5];

Without initial value it would just be:

Array1: ARRAY[1..5] OF INT;


Some other notes about arrays:

The first element can be element zero, i.e.

Array1: ARRAY[0..5] OF INT;

Multi-dimensional Arrays:

Array1: ARRAY[0..5,0..2] OF INT;

Array1: ARRAY[0..5,0..2,0..4] OF INT;

Declaring Pointers:

PointerToStrArray : POINTER TO STRING ( 9 );

Declaring Pointers to Arrays:

PointerToStrArray : POINTER TO ARRAY[1..10] OF STRING ( 9 );

In both the pointer examples above, the pointer values can be loaded with the ADR function.
More on that here.

Declaring Function Blocks with Inputs:

Declaring an OFF delay timer from the Standard library with an initialized preset time

OffDelayTimer: TOF:= (PT:=T#2S);

The above declaration method can also be used with the preset time tied to another variable of
type time:

OffDelayTimer: TOF:= (PT: = PresetTime);

where the PresetTime is declared as follows:

PresetTime: TIME := T#2s;


One of the positive points of a textual variable declaration area is the option to copy and paste. In
some programming environments, the alternate to a textual declaration is a tabular or table based
variable declaration area. Some table based ( or tabular) variable areas might include a pull-
down menu of options for each declaration. This may help someone who is beginning to learn in
an environment.

Nevertheless, in the long term, textual declarations are more flexible. Another benefit is opens
up the option to generate the variable declaration externally- in Excel for example. The external
declaration is useful for larger projects with multiple programmers. In Excel, a name can be
created and multiple versions of a variable generated with prefixes or suffixes to that name. One
example may be:

For a variable called SysRun:

The local variable could be SysRun_Local

The status to be fed to a SCADA system could be SysRun_SCADA

Instead of typing out: SysRun_Local: BOOL; and SysRun_SCADA: BOOL; , the entire
declaration process can be automated in Excel and then copied and pasted into CODESYS.

To wrap up for now..

Comments in variable declaration field are important. It helps the next person who reads the
code/program. It might even help you make sense of your work, in the future. Also, for function
blocks, the comments on the same line as a variable become the description of input pins when
the cursor hovers over a pin.
Include comments with variable declarations.Comments help refresh your memory or serve as a
guide for the next user.

There are probably other useful points and methods of variable declarations in CODESYS that
are not included here. If one comes mind, please comment below.
CODESYS Array Sorting for PLC programs
September 27, 2015Uncategorized

Sorting functions have increasing use in PLC programs thanks to more powerful processors and
more memory. One example is sorting an array of motor runtimes from lowest runtime to highest
runtime so that the next run cycle will utilize the motor with the lowest runtime first. This
example is useful for pump applications and is referred to in the post about pointers in
CODESYS.
To implement a pump runtime based sort, other elements in CODESYS such as DUT’s( user
defined data types) and structures could probably be useful. The reason is that the result of the
sorting function is to know which pump to run next. This can be done by having two dimensions
in the array, a Pump ID/Number along with the Pump Runtime. For the purpose of simplicity,
only a simply bubble sort with two FOR loops is shown below, the DUT approach is not shown
here.

A simple bubble sort can be implemented in CODESYS with the following code. Instead of
sorting the actual array, this example sorts an array of pointers ( dereferenced to get the actual
values from the pointers). This way the original array data locations do not need to be altered.
Not altering the original array locations is a good thing because in some cases, these array
locations may be hardcoded to physical inputs.

(*Prior to implementing the sort, an array if pointers is loaded *)

FOR l:=0 TO n BY 1 DO

pointToArrayElement[l]:=ADR(TestArray[l]);

END_FOR

(*Bubble sort- The value of n should be equivalent to the last array element*)

FOR i:=0 TO n BY 1 DO

(* The second FOR loop compares two elements of the array at


a time and sorts*)

FOR j:=0 TO n-1 BY 1 DO

IF(pointToArrayElement[j]^>pointToArrayElement[j+1]^) THEN

temp := pointToArrayElement[j+1];

pointToArrayElement[j+1] := pointToArrayElement[j];

pointToArrayElement[j] := temp;

END_IF

END_FOR

END_FOR
The variables used in this example:

VAR

TestArray: ARRAY [0..4] OF Pump;


TestArrayPointers: ARRAY [0..4] OF POINTER TO INT;

i: INT;
n: INT;
j: INT;

l: INT;
temp: pointer to INT;
pointToArrayElement: ARRAY OF POINTER TO INT;
END_VAR
CODESYS Pointers and Dereferencing
September 26, 2015CODESYS, PAC, PLC, ProgrammingCODESYS, PLC, Pointers

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.

2. 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:

3. 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.

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:

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.


Post navigation
← IEC61131-3 PLC Programming Languages PLC Programming Patterns- The Debounced
Threshold/Limit →

CODESYS Pointers and Dereferencing


September 26, 2015CODESYS, PAC, PLC, ProgrammingCODESYS, PLC, Pointers

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.
2. 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:

3. 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.

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:


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.

Post navigation
← IEC61131-3 PLC Programming Languages PLC Programming Patterns- The Debounced
Threshold/Limit →

You might also like