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:
oA reference does not have to be dereferenced explicitly (with ^) to access the contents of the referenced object.
oWhen 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)).
oThe compiler verifies that references of the same data type are assigned to each other.
For further information, refer to the Assignment operator REF description.
<identifier> : REFERENCE TO <data type>
A : REFERENCE TO DUT;
B : DUT;
C : DUT;
A REF= B; // corresponds to A := ADR(B);
A := C; // corresponds to A^ := C;
NOTE: It is not possible to declare references like REFERENCE TO REFERENCE or ARRAY OF REFERENCE or POINTER TO REFERENCE.
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> := __ISVALIDREF(identifier, declared with type <REFERENCE TO <datatype>);
<boolean variable> will be TRUE, if the reference points to a valid value, FALSE if not.
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 *)