|
A STRUCT_BLOCK is a user-defined composite type. Struct blocks are an extremely useful facility to group variables as in this example:
STRUCT_BLOCK sbBufferPDU
length : INT;
data : ARRAY[1..140} OF SINT;
END_STRUCT_BLOCK;
The biggest advantage of the STRUCT_BLOCK is found when writing data of different types to the serial port, PDU message, or file system.
Instead of writing the data types individually, they can be grouped in a STRUCT_BLOCK, and the device sees it as one variable.
STRUCT_BLOCK sbGpsData
mode : SINT;
linsec : DINT;
latitude : DINT;
longitude : DINT;
speed : DINT;
course : DINT;
height : INT;
PDOP : INT;
HDOP : INT;
VDOP : INT;
inview : SINT;
used : SINT;
END_STRUCT_BLOCK;
VAR
gpsData : sbGpsData;
END_VAR;
To access the members of a STRUCT_BLOCK, the same syntax as used with a FUNCTION_BLOCK is used.
For example:
gpsData.speed := 100;
STRUCT_BLOCK assignment
A complete STRUCT_BLOCK value can be assigned to another variable with the := operator. The source and destination must be declared with the exact same STRUCT_BLOCK type.
The assignment copies all members, including elementary values, STRING values, nested STRUCT_BLOCK values, arrays, arrays of STRING, and arrays of STRUCT_BLOCK. STRING members use normal STRING assignment semantics, including automatic reference management.
STRUCT_BLOCK position;
value : DINT;
text : STRING;
END_STRUCT_BLOCK;
STRUCT_BLOCK record;
title : STRING;
location : position;
values : ARRAY[0..9] OF INT;
names : ARRAY[0..2] OF STRING;
positions : ARRAY[0..1] OF position;
END_STRUCT_BLOCK;
VAR
source, destination : record;
list1, list2 : ARRAY[0..3] OF record;
i, j : INT;
END_VAR;
destination := source;
list2[i] := list1[j];
destination.location := source.location;
Restrictions:
•Separately declared STRUCT_BLOCK types are incompatible even when their members have identical types and layout. •Individual ARRAY OF STRUCT_BLOCK elements can be assigned. Complete standalone ARRAY variables cannot be assigned. •FUNCTION_BLOCK and THREAD_BLOCK instances cannot be assigned. •A FUNCTION cannot return a STRUCT_BLOCK value directly. Use ACCESS STRUCT_BLOCK parameters when a function must produce a composite result. •Self-assignment, for example item := item, is permitted.
To send the data through a serial channel:
serSendData(port:=1, data:=ADDR(gpsData), size:=SIZEOF(gpsData));
Note: STRUCT_BLOCK assignment handles STRING members safely using normal STRING assignment semantics. Raw memory operations are different: a STRING member stored inside a STRUCT_BLOCK contains a string handle, not the character data. Sending or storing a STRUCT_BLOCK using ADDR and SIZEOF therefore does not serialize the contents of its STRING members.
|