• No results found

Integration of ST in SIMOTION 5

5.1 Source file sections

5.1.2 Import and export between ST source files

ST applies the unit concept, where you can access the global variables, data types, functions (FCs), function blocks (FBs), and programs of other source files. Thus, for example, you can compile reusable subroutines and make them available.

5.1.2.1 Unit identifier

Below, unit refers to a program source file (e.g. ST source file, MCC source file). The name of the program source file defined in SIMOTION SCOUT is applied as the identifier.

Optionally, you can set the unit statement as first statement for an ST source file (preceding the interface section). Syntax:

UNIT name;

name corresponds to the name of the ST source file defined in SIMOTION SCOUT, see Add ST source (Page 21) or Change the properties of an ST source file (Page 23).

The unit statement is ignored if the name specified there differs from the name of the ST source file.

5.1.2.2 Interface section of an exporting unit

You can enter the following constructs in the interface section of an exporting unit. The syntax of the constructs is only implied here, for details, see "Interface section (Page 170)".

● The type declarations to be exported TYPE

User-defined data types with their complete declaration.

● The variable declarations to be exported

VAR_GLOBAL, VAR_GLOBAL RETAIN, or VAR_GLOBAL CONSTANT

Non-retentive and retentive unit variables and unit constants with their complete declaration.

● POUs (functions, function blocks, and programs) to be exported

Specify each POU (function, function block, or program) to be exported with the relevant keyword. Close each entry with a semicolon.

– FUNCTION_BLOCK fb_name ; – FUNCTION fc_name ;

– PROGRAM program_name ;

Specifications can be made in any order; the POU itself is programmed in the implementation section of the ST source file.

Note

The following further specifications are possible in the interface section, they are listed before the exported data types, variables and POU:

1. Specification of utilized technology packages (USEPACKAGE …).

2. Specification of utilized libraries (USELIB …).

3. Reference to other units in order to use their exported units (USES …).

These imported technology packages, libraries and units are also exported. For inheritance, see "USES statement in an importing unit (Page 181)".

You must adhere to the order presented for the specifications in the interface section of a unit (ST source file), see "Interface section (Page 170)". Otherwise, error-free compilation of the ST source file will not be possible.

The programs of an ST source file must be listed in the interface section so that they can be assigned to a task in the execution system (see Configuring the execution system in the SIMOTION Basic Functions Function Manual). The compiler outputs a warning message if programs cannot be exported in the interface section of an ST source file.

Functions and function blocks that are only used in the ST source file should not be listed in the interface section.

5.1.2.3 Example of an exporting unit

Below is an example of an exporting unit (myUnit_A). It is imported by myUnit_B (see Example of an importing unit (Page 183)).

Table 5-11 Example of an exporting unit

UNIT myUnit_A; // Optional, name of the ST source file

END_FUNCTION_BLOCK

PROGRAM myProgram_A // Program written out ; // ... (Statements)

END_PROGRAM END_IMPLEMENTATION

5.1.2.4 USES statement in an importing unit

Enter the following statement in the interface or implementation section of an importing unit:

USES unit_name-list

unit_name-list is a list of units separated by commas from which the modules are to be imported.

Example:

USES unit_1, unit_2, unit_3;

This enables you to access the following elements specified or declared in the interface section of the imported unit (e.g. ST source file, MCC source):

● User-defined data types (UDT)

● Unit variables and unit constants

● Programs, functions and function blocks

● Imported technology packages, libraries and units

You can use the imported elements as if they existed in the current unit.

Note

The keyword USES can only occur once in the interface section or in the implementation section of a unit. When multiple units are to be imported, enter them as a list separated by commas after the keyword USES.

The USES statement can appear in either the interface section or the implementation section of a unit. This has far-reaching implications:

Table 5-12 Implications regarding placement of USES statement in interface section or in implementation section

Effect USES statement

in the interface section USES statement in the implementation section Inheritance The current unit continues exporting the

imported unit; the imported unit is inherited by all other units that access the current unit.

Example:

Because of inheritance, Unit A must not be imported explicitly into Unit C.

Inheritance is interrupted.

Example:

1. Unit B imports Unit A in the implementation section.

2. Unit C in turn imports Unit B.

3. Then Unit C has no automatic access to Unit A.

Unit C must explicitly import Unit A if it wants to access Unit A.

Variable declaration The declaration of a unit variable of an imported data type is possible in:

Interface section

Implementation section

The declaration of a unit variable of an imported data type is only possible in the implementation section.

Note

You will find tips for use of unit variables in the SIMOTION Basic Functions Function Manual.

5.1.2.5 Example of an importing unit

Below is an example of an importing unit (myUnit_B). It imports the unit myUnit_A from Example of an exporting unit (Page 181).

Table 5-13 Example of an importing unit

UNIT myUnit_B; // Optional, name of the ST source file INTERFACE

// ... if required, USES statement PROGRAM myProgram_B;

// Specification of programs to be exported, FB, FC // Data types and unit variables

END_INTERFACE IMPLEMENTATION

USES myUnit_A; // Specification of unit to be imported VAR_GLOBAL

myInstance : myFB; // Declaration of an instance // of the imported FB

mycolor : color; // Declaration of a variable // of the imported data type END_VAR

PROGRAM myProgram_B

mycolor := GREEN; // Value assignment to a variable of the // data type to be imported

cycle := cycle + 1; // Value assignment to // imported variable END_PROGRAM

END_IMPLEMENTATION