Namespaces in ST: usage

Language elements of a namespace can be accessed from outside this namespace as follows:

Naming the fully qualified name

Define the language element with its fully qualified name. The fully qualified name of the language element consists of the namespace identifiers and the identifier of the language element that are separated by . (dots).

Example
FUNCTION_BLOCK Uses_Timer1
VAR
Ton1: Standard.Timers.TON; (* uses the 'TON' block that is declared in the nested namespace 'Standard.Timers' *)
Ton2: TON; (* uses the standard 'TON' block of the global namespace *)
bTest: BOOL;
END_VAR
Ton1(In:= bTest, PT:= t#5s);
END_FUNCTION_BLOCK

Restriction

It is not possible to define function →calls with the fully qualified name. For such cases, define the USING namespace directive from the following section.

Using the USING namespace directive

Define a USING namespace directive. The USING directive enables the access to language elements contained in the specified namespace to be used in the following elements:

Element

Insert the USING directive at this position:

within the ST-object

at the beginning of the ST-object (as first line in the ST-editor)

within the global-object

in front of the section VAR_GLOBAL ... END_VAR of the global-object

within the PLC-object

in front of the section VAR_GLOBAL ... END_VAR of the PLC-object
(= in front of the declaration of resource-global or configuration-global variables)

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/warning.svg Best practice is to define only one USING directive per configuration within the PLC-object – either in front of the declaration of configuration-global variables or in front of declaration of resource-global variables.

within a namespace

behind the name of the namespaces

within a →function block

behind the name of the function block

within a →function

behind the name of the function or the data type of the return value for the function

Another element where the USING directive can be inserted:

Element

Insert the USING directive at this position:

within the ST-interface of a C-/C++-block

after {extern_c} or {extern_cxx}

A USING directive starts with the keyword USING, followed by the fully qualified name of the namespaces and ; (see example 1 below). The USING directive enables the access to the specified namespace only, it does not enable access to other nested namespaces (see example 2 below).
images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg It is also possible to specify a USING instruction within the application navigator (see under " Actions with folders/objects in context with namespaces " for details). In this case , the ST-editor does not need a USING namespace directive to this namespace.

Restrictions

  • Directives in the form of USING Namespace1, Namespace 2; are not supported. If you want to access language elements from different namespaces, you have to define several USING directives.

  • At present, it is not possible to use language elements with the same name from the global namespace. This restriction applies to all language elements declared in the global namespace, but in particular to the system blocks. logi.CAD 3 changes the call of the language element declared in the global namespace to the call of the language element declared in the same namespace. See example 1.


images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg The following examples are based on the ST-code of example 2 under "Namespaces in ST: declaration".

Example 1: Correct access
FUNCTION_BLOCK Uses_Timer2
USING Standard.Timers;
VAR Ton1 : TON; (* uses the 'TON' block that is declared in the nested namespace 'Standard.Timers' *)
 (* Note: The standard 'TON' block of the global namespace cannot be used in this example. *)
(* If this is necessary, define all language elements with the fully qualified name; see above. *)
 bTest : BOOL;
 END_VAR
Ton1(In:= bTest, PT:= t#5s);
 END_FUNCTION_BLOCK

Example 2: Faulty access
NAMESPACE Infeed
 USING Standard;
USING Standard.Counters;
 FUNCTION_BLOCK Uses_Timer3
 VAR Ton1 : Timers.TON;
 (* error because access to the namespace 'Standard.Timer' is not possible *)
 (* Solution: *) (* either insert directive 'USING Standard.Timers' above and correct line to 'Ton1 : TON;' *)
 (* or use the fully qualified name, hence: correct line to 'Ton1 : Standard.Timer.TON;' *)
 bTest : BOOL;
END_VAR
Ton1(In:= bTest, PT:= t#5s);
 END_FUNCTION_BLOCK
 END_NAMESPACE