• No results found

3.4 Data types

3.4.2 User-defined data types

3.4.2.1 User-defined data types

User-defined data types (UDT) are created with the construct TYPE/END_TYPE in the declaration sections of subsequent source file sections (see Structure of an ST source file (Page 86) and Source file sections (Page 169)) of the following:

● Interface section

● Implementation section

● Program organization unit (POU)

You can continue to use the data types you created in the declaration section. The source file section determines the range of the type declaration.

See also

Syntax of user-defined data types (type declaration) (Page 95) Derivation of elementary or derived data types (Page 96) Derived data type ARRAY (Page 97)

Derived data type - Enumerator (Page 99)

Derived data type STRUCT (structure) (Page 100)

3.4.2.2 Syntax of user-defined data types (type declaration)

,GHQWLILHU

'DWDW\SH

$55$<

GDWDW\SH VSHFLILFDWLRQ

(QXPHUDWRU

಺GDWDW\SH

VSHFLILFDWLRQ

6758&7 GDWDW\SH

VSHFLILFDWLRQ

,QLWLDOL]DWLRQ 8VHUGHILQHGGDWDW\SHVಥ8'7 XQIRUPDWWHG

8'7LGHQWLILHU

 



 (1'B7<3(

7<3(

Figure 3-6 Syntax: User-defined data type

The declaration of the UDT is introduced with the keyword TYPE.

For each data type to be declared, this is followed by (see figure):

1. Name:

The name of the data type must comply with the rules for identifiers.

2. Data type specification

The term data type comprises (see Derivation of elementary or derived data types (Page 96)):

– Elementary data types – Previously declared UDTs – TO data types

– System data types

The following data type specifications are also possible:

– ARRAY data type specification (see Derived data type ARRAY - field (Page 97)) – Enumerator data type specification (see Derived data type enumerator (Page 99))

3. Optional initialization:

You can specify an initialization value for the data type. If you subsequently declare a variable of this data type, the initialization value is assigned to the variable.

Exception: With the STRUCT data type specification, each individual component is initialized within the data type specification.

See also Initialization of variables or data types (Page 107).

The complete UDT declaration is terminated with the keyword END_TYPE. You can create any number of data types within the TYPE/END_TYPE construct. You can use the defined data types to declare variables or parameters.

UDTs can be nested in any way as long as the syntax in the figure is observed. For example, you can use previously defined UDTs or nested structures as a data type specification. Type declarations can only be used sequentially and not in nested structures.

Note

You can learn how to declare variables and parameters in Overview of all variable declarations (Page 106), and how to assign values with UDT in Syntax for value assignment (Page 113).

Below is a description of individual data type specifications for UDTs and examples demonstrating their use.

3.4.2.3 Derivation of elementary or derived data types

In the derivation of data types, an elementary or user-defined data type (UDT) is assigned to the data type to be defined in the TYPE/END_TYPE construct:

TYPE identifier : Elementary data type { := initialization } ; END_TYPE TYPE identifier : User-defined data type { := initialization } ; END_TYPE

Once you have declared the data type, you can define variables of derived data type identifier. This is equivalent to declaring variables as data type elementary data type.

Table 3-12 Examples of derivation of elementary data types TYPE

3.4.2.4 Derived data type ARRAY

The ARRAY derived data type combines a defined number of components of the same data type in the TYPE/END_TYPE construct. The syntax diagram in the following figure shows this data type, which is specified more precisely after the reserved identifier OF.

TYPE identifier : ARRAY data type specification { := initialization } ; END_TYPE

'DWDW\SH ,QGH[VSHFLILFDWLRQ

&RQVWDQWH[SUHVVLRQ &RQVWDQWH[SUHVVLRQ

',17GDWDW\SH ',17GDWDW\SH

$55$<GDWDW\SHVSHFLILFDWLRQ XQIRUPDWWHG

 @

>

2)

$55$<

Figure 3-7 Syntax: ARRAY data type specification

The index specification describes the limits of the array:

● The array limits specify the minimum and maximum value for the index. They can be specified using constants or constant expressions; the data type is DINT (or can be implicitly converted to DINT – see Elementary data type conversion (Page 141)).

● The array limits must be separated by two periods.

● The entire index specification is enclosed in square brackets.

● The index itself can be an integer value of data type DINT (or it can be implicitly converted to DINT – see Elementary data type conversion (Page 141)).

Note

If array limits are violated during runtime, a processing error occurs in the program (see SIMOTION Basic Functions Function Manual).

You declare the data type of the array components with the data type specification. All of the options described in this chapter can be used as data types, for example, even user-defined data types (UDT).

There are several different ARRAY types:

● The one-dimensional ARRAY type is a list of data elements arranged in ascending order.

● The two-dimensional ARRAY type is a table of data consisting of lines and columns. The first dimension refers to the line number, the second to the column number.

Table 3-13 Examples of one-dimensional arrays TYPE

x : ARRAY[0..9] OF REAL;

y : ARRAY[1..10] OF C1;

END_TYPE

Two-dimensional arrays are comparable to a table with lines and columns. You can create two- or multi-dimensional arrays by means of a multi-level type declaration, see example:

Table 3-14 Examples of multi-dimensional arrays TYPE

a : ARRAY[1..3] OF INT; // one-dimensional array (3 columns):

matrix1: ARRAY[1..4] OF a; // two-dimensional Field // (4 lines with 3 columns) b: ARRAY[4..8] OF INT; // one-dimensional array (5 columns):

matrix2: ARRAY[10..16] OF b; // two-dimensional Field // (7 lines with 5 columns) END_TYPE

VAR

m: matrix1; // Variable m of data type two-dim. Field n: matrix2; // Variable m of data type two-dim. Field END_VAR

m[4][3] := 9; // Write to Matrix1 at line 4, column 3 n[16][8] := 10; // Write to Matrix2 at line 7, column 5

In the example, you can define:

1. Table columns a[1] to a[3] as a one-dimensional array that will contain integers.

2. Table lines matrix1[1] to matrix2[4] also as an array but take as the data type specification the array a you just created with the columns of the table.

When you specify an array in the data type specification, you create a second dimension.

You can create further dimensions in this way.

Now declare a variable using the data type created for the table. You address each dimension of the table using square brackets, in this case specifying the line and column.

3.4.2.5 Derived data type - Enumerator

In the case of enumerator data types, a restricted set of identifiers or names is assigned to the data type to be defined in the TYPE/END_TYPE construct:

TYPE identifier : Enumerator data type specification { := initialization } ; END_TYPE

(QXPHUDWRUGDWDW\SHVSHFLILFDWLRQ XQIRUPDWWHG

(QXPHUDWRUHOHPHQW ,GHQWLILHU

 



Figure 3-8 Syntax: Enumerator data type specification

Once you have declared the identifier data type, you can define variables in the enumerator data type. In the statement section, you can assign only elements from the list of defined identifiers (enumerator elements) to these variables.

You can also specify the data type directly: Place the enumerator data type identifier and the

"#" sign in front of the enumerator element (see Table Examples of enumerator data types).

You can obtain the first and last value of an enumeration data type with enum_type#MIN and enum_type#MAX respectively, whereby enum_type is the enumeration data type identifier.

You can obtain the numeric value of an enumeration element with the ENUM_TO_DINT conversion function.

Table 3-15 Examples of enumerator data types TYPE

C1: (RED, GREEN, BLUE);

END_TYPE VAR

myC11, myC12, myC13 : C1;

END_VAR

myC11 := GREEN;

myC1l := C1#GREEN;

myC12 := C1#MIN; // RED myC13 := C1#MAX; // BLUE

Note

You will also find enumerator data types as system data types.

3.4.2.6 Derived data type STRUCT (structure)

The derived data type STRUCT, or structure, encompasses an area of a fixed number of components in the TYPE/END_TYPE construct; the data types of these components can vary:

TYPE identifier : STRUCT data type specification; END_TYPE

&RPSRQHQWV

GHFODUDWLRQ 6758&7GDWDW\SHVSHFLILFDWLRQ XQIRUPDWWHG

'RQRWIRUJHWWRWHUPLQDWHWKH(1'B6758&7NH\ZRUGZLWKDVHPLFRORQ

6758&7 (1'B6758&7 

Figure 3-9 Syntax: STRUCT data type specification

The syntax of the component declaration is shown in the following figure.

&RPSRQHQWGHFODUDWLRQ XQIRUPDWWHG

,GHQWLILHU 'DWDW\SH

$55$<

GDWDW\SH VSHFLILFDWLRQ

,QLWLDOL]DWLRQ ,GHQWLILHURIWKH

FRPSRQHQW

   

Figure 3-10 Syntax: Component declaration The following are permitted as data types:

● Elementary data types

● Previously declared UDTs

● System data types

● TO data types

● ARRAY data type specification

You also have the option to assign initialization values to the components. Proceed as for the initialization of variables or data types (see Initialization of variables or data

types (Page 107)).

Note

The following data specifications cannot be used directly within a component declaration:

• STRUCT data type specifications

• Enumerator data type specifications

Solution: Declare a UDT (user-defined data type) beforehand with the above-mentioned specifications and use this in the component declaration.

This allows you to nest STRUCT data types.

You will also find STRUCT data types as system data types.

This example shows how a UDT is defined and how this data type is used within a variable declaration.

Table 3-16 Examples of derived data type STRUCT TYPE // UDT definition

S1: STRUCT var1 : INT;

var2 : WORD := 16#AFA1;

var3 : BYTE := 16#FF;

var4 : TIME := T#1d_1h_10m_22s_2ms;

END_STRUCT;

END_TYPE VAR

myS1 : S1;

END_VAR

myS1.var1 := -4;

myS1.var4 := T#2d_2h_20m_33s_2ms;