Introduction

The Reference data type allows mapping of different types of data in a DDT.

A reference contains the memory address of a variable.

References are declared using the keyword REF_TO followed by the type of the referenced value (for example: 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

myRefInt of type REF_TO INT

Assignment

Assigns reference to reference (same type)

myRefINT1:= myRefINT2;

Assigns reference to parameter of a function

myFB (r := myRef);

Comparison with NULL

 

IF myRef = NULL THEN …

Referencing

Assigns address of a variable to a reference

myRefA := REF(A);

Dereferencing

Provides the value of the variable referenced to

A := myRefA^;

B := myRefArrayType^[12];

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 value

  • to 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 R/W Rights of Referenced Variable

  • 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 Data Editor:

  • RW program: used to set the reference as read only.

  • R/W Rights of Referenced Variable : used to specify if the referenced variable is a read-only variable (the referenced variable is read-only when R/W Rights of Referenced Variable is not selected).

  • Constant: used to prevent modification by program.

NOTE: A reference variable has to respect the R/W attributes of the referenced variable.

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;

NOTE: In all other cases, Control Expert software raises a detected error, the detected error message explains how to correct the application.