Introduction
The Reference data type allows mapping of different types of data in a DDT.
A reference contains the memory address of a variable.
NOTICE | |
---|---|
References are declared using
the keyword myRefInt: REF_TO INT
).
A reference can
be assigned to another reference if it points to the same or compatible
data type (for example, myRefINT1:= myRefINT2
).
References can be assigned to parameters of functions.
Summary of Control Expert reference operations:
Operation |
Description |
Example |
---|---|---|
Declaration |
Declaration of a variable to be a reference |
|
Assignment |
Assigns reference to reference (same type) |
|
Assigns reference to parameter of a function |
|
|
Comparison with |
|
|
Referencing |
Assigns address of a variable to a reference |
|
Dereferencing |
Provides the value of the variable referenced to |
|
A reference can be dereferenced using a postfix “^”
(caret), but dereferencing a NULL
reference
produces a detected error.
Reference Limitations
A reference:
to a reference is not supported
cannot be explicitly assigned the
NULL
valueto an IODDT is not supported because it has no memory allocation; it has no address to reference
can only refer to variables of the given reference data type (EDT, DDT, or Device DDT) and can only be compared to a reference of the same or compatible type
can only be used with the “:=”, “=” and “<>” operators and the EFs “EQ” and “NE”.
cannot be a temporary variable, for example, a FBD-link or result value of a nested EF-call
cannot be used with the SFC and LL984 programming languages
respects the access rights of the referenced variable by variable attribute
has to be assigned to an FFB’s reference pin (mandatory parameter)
Declaring a DFB or FFB with an input or output parameter references is allowed, but not an in/out parameter, which is already a reference.
A dereferenced reference can be used like a variable of the referenced type.
Only 1 level of dereferencing is allowed.
The initial value of a reference cannot be cyclic:

Possible usages in an application section
We can only reference an application variable to an application variable reference or to a DFB public variable reference:
Var_Ref := REF(Var);
DFB_Instance.public_Var_Ref := REF(Var);
We can only assign an application variable to an application variable or to a DFB public variable:
Var1_Ref := Var2_Ref;
DFB_Instance.public_Var_Ref := Var_Ref;
Possible usages in a DFB section
We can only reference an In/Out variable or a private variable, to an Out reference or Public reference for the In/Out and to a Private reference for a Private variable:
Out_Var_Ref := REF(In_Out_Var);
Public_Var_Ref := REF(In_Out_Var);
Private_Var1 := REF(Private_Var2);
We can only assign an In reference, an out reference, an In/Out reference and a Public reference to an Out reference or a Public reference. And a Private references can only be assigned to a Private reference:
Out_Var_Ref := In_Var_Ref;
Out_Var_Ref := Out_Var_Ref;
Out_Var_Ref := In_Out_Var_Ref;
Out_Var_Ref := Public_Var_Ref;
Public_Var_Ref := In_Var_Ref;
Public_Var_Ref := Out_Var_Ref;
Public_Var_Ref := In_Out_Var_Ref;
Public_Var_Ref := Public_Var_Ref;
Private_Var_Ref := Private_Var_Ref;
Reference access rights
The following attributes can be set to a Reference by the
:: used to set the reference as read only.
: used to specify if the referenced variable is a read-only variable (the referenced variable is read-only when is not selected).
: used to prevent modification by program.
This table shows the only available access rights for variables and their referenced variables:
Reference |
Variable |
Assignment example |
||
---|---|---|---|---|
RW Program |
RW Rights of Referenced Variable |
Constant |
RW Program |
|
RO |
RW |
Yes |
RO |
MyREF^ := Var; |
RO |
RW |
Yes |
RW |
MyREF^ := Var; |
RW |
RO |
No |
RO |
MyREF := REF(Var); |
RW |
RO |
No |
RW |
MyREF := REF(Var); |
RW |
RW |
No |
RO |
MyREF^ := Var; |
RW |
RW |
No |
RW |
MyREF := REF(Var); |
RW |
RW |
No |
RW |
MyREF^ := Var; |