how to create a Struct in ST (TC3)

#PLC #TwinCAT3 #Beckhoff

Learn more at Part 5 - Structures & functions (IEC 61131-3)

1) Example

TYPE ST_Event :
STRUCT
	eEventType : E_EventType;
	eEventSeverity : TcEventSeverity;
	nEventIdentity : UDINT;
	sEvenText : STRING(255);
	dtTimestamp : DATE_AND_TIME;
END_STRUCT
END_TYPE
PROGRAM MAIN
VAR
	aEvents : ARRAY[1..100] OF ST_Event;
	sTempString : STRING(255);
END_VAR

-----------------------------------------------------------------------

sTempString := aEvents[33].sEventText;,

2) Documentation

2.1) What is a Structure?

Structures is a user defined data type available in IEC 61131-3 that allows us to combine data items of different types.

Structures are used to represent a record or logical group.

If we want to achieve what we did in the previous slide of storing 100 records of events, we now only need to create one array of 100 instances of this struct.
400

In this way all data is now grouped into one single array instead of five arrays with different data types.

To access a structures components, you use the following syntax.
400

In other words, you write the name of the structure, a dot, and the component inside of the structure.

In this example we are accessing the event text and we are storing it in another string variable.
400

2.2) How to create a Structure?

To create a new structure data unit type, right-click on DUTs, select Add, next select DUT.
400

Enter the name of the DUT, in this case ST_Event and make sure that "Structure" is selected.

This will create the STRUCT so that we can fill the members of the struct.

Just as with enumerations, the scope of structures is global which means that all the code in your project will see the structure definition.

You can't do like in for example C++ and create a local structure within a class.