A structure bundles related data into one whole: a motor's full state, a recipe's parameters, a message frame's fields. Define the type once, build instances everywhere; member access by "dotting" through names lifts readability immediately. The sections below cover definition, access, nesting and the array combination — syntax per the mainstream platforms (TIA/CODESYS/TwinCAT), all closely alike.
What is defined is the type (the blueprint), not data (the house). For real data, declare variables: M1 : UDT_Motor; M2 : UDT_Motor; — two motors, two complete data sets. Type definitions live in the project's data-type area (TIA PLC data types / CODESYS DUT), visible project-wide.
| Operation | Syntax | Note |
|---|---|---|
| Read a member | IF M1.FeedBack THEN | Motor 1's run feedback |
| Write a member | M1.Speed := 1200; | Motor 1's setpoint |
| Whole-block copy | M2 := M1; | Same-type assignment (all members) |
| Comparison | Member by member (most platforms lack whole-struct =) | Do not write M1 = M2 |
Whole-block copy is the sweet spot: saving/restoring recipes or parameter packs in one line. But it copies every member, including ones you may not want touched — when some members must hold, assign member by member instead of taking the shortcut.
Nested members dot all the way: Station1.Motor.Speed := 1500;. Nesting maps the "machine tree" naturally onto code: a line is a structure, stations are members, motors are members of stations. Keep depth to three levels — beyond that the variable names become unreadable and it is time to flatten.
| Declaration | Meaning |
|---|---|
| Recipes : ARRAY[1..20] OF UDT_Recipe | 20 recipes, each a complete structure |
| Recipes[3].Temp | Recipe 3's temperature |
| FOR i:=1 TO 20 DO Recipes[i].Flag := FALSE; | Batch-process every recipe |
Structure + array is the standard answer for recipe libraries, channel groups and multi-axis configuration: the index picks the object, the dot picks the member, the loop does the bulk. Combined with a DB (the array in a retentive DB) it is the data foundation of the recipe-switching article.
| Habit | Reason |
|---|---|
| Comment every member | One line each — no guessing in six months |
| Units in the name or comment | Speed_01mm versus Speed_mm — mixing them is an incident |
| Types matched to range | DINT for hour counts, BOOL for switches — not INT everywhere |
| Retention decided per member | Accumulators retained, transient states not (member-level retention where supported) |
| Version evolution logged | Add members at the tail to avoid online-download data misalignment |
Special warning on "append at the tail": on some platforms changing a structure's layout online re-interprets existing retained data by new offsets — a structure change is a structural operation, paired with download-side data verification, and doubly careful on shipped machines.
| Symptom | Cause | Fix |
|---|---|---|
| Whole-struct comparison fails to compile | Platform lacks struct = struct | Compare member by member, or write a compare FC |
| Whole-block copy touched what it should not | All-member copy | Member-wise assignment |
| Data scrambled after download | Layout change + retained data misaligned | Verify member changes; re-initialise the area if needed |
| Access path typos | Nesting depth misremembered | Let the IDE autocomplete; never hand-type full paths |
| Units mixed | No unit in the member name | Naming or comments must carry units |
A structure's value is "packing semantics": M1.FeedBack reads; M23 does not; one block copy beats twenty MOVs. Do the three things at definition — member comments, unit labels, retention attributes — and the structure is the program's equipment manifest; skip them and it is porridge. A data structure's maintainability never lives in syntax, only in discipline.
STRUCT/UDT/DUT definitions and member access follow IEC 61131-3 and the platform manuals (TIA PLC data types, CODESYS DUT, TwinCAT structure chapters); retention attributes follow platform storage documentation; layout-change effects on online downloads follow the platform download documentation.
The day a structure is created, its three-piece set is done: member comments, unit labels, retention attributes — no three-piece set, no entry into the program.