Sysmac Studio Β· NJ/NX Β· custom types

Data structures

Loose variables turn into a mess quickly. Group the ones that belong together into a named shape, a STRUCT, and your code starts to read like the machine it controls.

🧱 STRUCT🏷️ ENUMπŸ”€ UNION
01 Β· STRUCT

Bundle a recipe

A bottling line has a recipe with a fill volume, a cap torque and a label flag. You can hold that as three loose variables or as one tidy ST_Recipe. Edit the fields and the Structured Text updates to match.

ml
Nm

        

πŸ’‘ Now Line1.recipe is one variable you can pass around, copy, or store as an array of recipes, where before you had three to juggle. Add a field to the type and every recipe gets it for free.

02 Β· ENUM & UNION

Name your states, overlay your bytes

ENUM: readable states

Instead of "mode = 2", say what you mean. Click a mode:

UNION: one memory, two views

TYPE U_Word :
UNION
  asWord  : WORD;      // all 16 bits
  asBytes : ARRAY[0..1] OF BYTE;
END_UNION
END_TYPE

Both members share the same bytes. Write asWord and read asBytes[0], which is handy for decoding a fieldbus register without bit-shifting by hand.

03 Β· Check yourself

One quick question

You write to myWord.asWord. What happens to myWord.asBytes?