MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do you define a STRUCT and access its members?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do you define a STRUCT and access its members?

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.

1. Definition: the type first, then instances

TYPE UDT_Motor :
STRUCT
  RunCmd  : BOOL;    // start command
  FeedBack : BOOL;   // run feedback
  Speed   : INT;     // setpoint speed
  Hours   : DINT;    // accumulated hours
  ErrID   : BYTE;    // error code
END_STRUCT
END_TYPE

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.

2. Access: dot your way down

OperationSyntaxNote
Read a memberIF M1.FeedBack THENMotor 1's run feedback
Write a memberM1.Speed := 1200;Motor 1's setpoint
Whole-block copyM2 := M1;Same-type assignment (all members)
ComparisonMember 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.

3. Nesting: structures within structures

TYPE UDT_Station :
STRUCT
  Motor : UDT_Motor;  // this station's motor
  Clamp : UDT_Motor;  // the clamp (reusing the motor structure)
  Step  : INT;         // step number
END_STRUCT

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.

4. Structure arrays: the standard recipe-library form

DeclarationMeaning
Recipes : ARRAY[1..20] OF UDT_Recipe20 recipes, each a complete structure
Recipes[3].TempRecipe 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.

5. Design habits that keep structures maintainable

HabitReason
Comment every memberOne line each — no guessing in six months
Units in the name or commentSpeed_01mm versus Speed_mm — mixing them is an incident
Types matched to rangeDINT for hour counts, BOOL for switches — not INT everywhere
Retention decided per memberAccumulators retained, transient states not (member-level retention where supported)
Version evolution loggedAdd 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.

6. Common errors

SymptomCauseFix
Whole-struct comparison fails to compilePlatform lacks struct = structCompare member by member, or write a compare FC
Whole-block copy touched what it should notAll-member copyMember-wise assignment
Data scrambled after downloadLayout change + retained data misalignedVerify member changes; re-initialise the area if needed
Access path typosNesting depth misrememberedLet the IDE autocomplete; never hand-type full paths
Units mixedNo unit in the member nameNaming or comments must carry units

7. Notes

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.

8. References

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.

9. Cautions

⚠️ Three traps to avoid:
① Members without comments or units — is Speed mm/s or 0.1 mm/s? Guessing forever;
② Whole-block assignment for convenience — accumulated members cleared along with the rest;
③ Careless mid-life structure edits — shipped machines' retained data misaligned.

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.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL