Deep dive Β· expands Data Structures

Data Types Deep

Custom types earn their keep when they map to a real job. STRUCT for recipes, ENUM for modes, and the sharpest tool of all β€” the UNION, which lets you read the same bytes two ways. Flip some bits and watch every view update at once.

πŸ”€ UNION overlayWORD ↔ bytes ↔ bits
01 Β· One memory, three views

Decode a fieldbus word

A drive packs a status word into 16 bits. Type the value, or click individual bits β€” the WORD, the two bytes and the 16 BOOLs are the same memory, so they always agree.

0..65535
asWord (WORD)
β€”
asBytes[1] Β· high
β€”
asBytes[0] Β· low
β€”
hex
β€”

asBits β€” ARRAY[0..15] OF BOOL (click to toggle)

02 Β· The rest of the toolbox

ALIAS, subrange & arrays of structs

TYPE Temperature : REAL; END_TYPE  // ALIAS: clearer intent
TYPE Channel : UINT(1..8); END_TYPE // subrange: range-safe
stations : ARRAY[1..8] OF ST_Station; // a tabulated line

🏷️ ALIAS renames a type so Temperature reads better than a bare REAL β€” same storage, clearer code.

πŸ“ Subrange bakes limits into the type: a Channel can only be 1–8, so an out-of-range value is caught at the source.

03 Β· Check yourself

One quick question

Why reach for a UNION when decoding a fieldbus register?