Professional Documents
Culture Documents
Skip to content
Home
About
VFD’s
PLC’s
Links and Resources
Search for:
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 initial values can be tied to another variable of the same data type:
where:
VarName2:= INT;
VarName2 :STRING(40);
With the above, a string of 40 characters is created. Each character is one byte.
Multi-dimensional Arrays:
Declaring Pointers:
In both the pointer examples above, the pointer values can be loaded with the ADR function.
More on that here.
Declaring an OFF delay timer from the Standard library with an initialized preset time
The above declaration method can also be used with the preset time tied to another variable of
type time:
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:
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.
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.
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
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
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
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.
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.
To create a pointer:
PROGRAM PLC_PRG
VAR
j: INT;
k:INT;
END_VAR
in plain text:
pointToJ:=ADR( j);
To de-reference a pointer:
Example:
in plain text:
k:=pointToJ^;
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.
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.
To create a pointer:
PROGRAM PLC_PRG
VAR
j: INT;
k:INT;
END_VAR
pointToJ:=ADR( j);
To de-reference a pointer:
Example:
in plain text:
k:=pointToJ^;
Post navigation
← IEC61131-3 PLC Programming Languages PLC Programming Patterns- The Debounced
Threshold/Limit →