This data type is available in extension to the IEC 61131-3 standard.
A reference stores the address of an object (a variable) that is located elsewhere in memory; in this respect, the behavior is identical to a pointer.
In contrast to a pointer, a variable behaves like an object when the syntax is concerned. Additionally, variables declared as REFERENCE provide the following advantages compared to POINTERS:
A reference does not have to be dereferenced explicitly (with ^
) to access the contents of the referenced object.
When passing values to input parameters of functions/function blocks/methods, the following applies: If an input is declared as REFERENCE TO
<data type>
, a variable of the corresponding <data type>
can be passed (refInput :=
variable instead of ptrInput :=ADR
(variable)).
The compiler verifies that references of the same data type are assigned to each other.
For further information, refer to the Assignment operator REF description.
You can also use an implicit pointer monitoring function as described in the paragraph CheckPointer function.
A : REFERENCE TO DUT;
B : DUT;
C : DUT;
A REF= B; // corresponds to A := ADR(B);
A := C; // corresponds to A^ := C;
You can use the operator __ISVALIDREF
to check whether a reference points to a valid value that is a value unequal to 0.
Syntax
<Boolean variable name> := __ISVALIDREF( <reference name> );
<reference name>
: Identifier declared with type REFERENCE TO
<data type>
.
The boolean variable is TRUE, if the reference points to a valid value. Otherwise it is FALSE.
Example
Declaration
ivar : INT;
ref_int : REFERENCE TO INT;
ref_int0: REFERENCE TO INT;
testref: BOOL := FALSE;
Implementation
ivar := ivar +1;
ref_int REF= ivar;
ref_int0 REF= 0;
testref := __ISVALIDREF(ref_int); (* will be TRUE, because ref_int points to ivar, which is unequal 0 *)
testref := __ISVALIDREF(ref_int0); (* will be FALSE, because ref_int is set to 0 *)