Skip to content

MU Data from ST

Commercial

Included in the public demo Compare editions

A part can carry its own data through the plant: a variant, an order number, a list of items. This is MU data. With the MU_* functions an ST program reads and writes these values directly, by the id of the part and a path. The functions are ordinary function calls in standard ST notation; you need no switch and no special type.

The program needs the id of the part, as a DINT. A typical source is an int signal at the place where the part stands, for example the signal a reader or a transfer place reports for the part it holds.

FunctionResultMeaning
MU_EXISTS(MU)BOOLTRUE while a part with this id exists
MU_HAS(MU, PATH)BOOLTRUE when a value is stored at the path
MU_LEN(MU, PATH)INTNumber of elements of the list at the path, -1 when no list stands there
MU_GET_INT(MU, PATH, DEFAULT)DINTWhole number at the path
MU_GET_REAL(MU, PATH, DEFAULT)LREALNumber at the path
MU_GET_BOOL(MU, PATH, DEFAULT)BOOLBoolean at the path
MU_GET_STRING(MU, PATH, DEFAULT)STRINGText at the path
MU_SET_INT, MU_SET_REAL, MU_SET_BOOL, MU_SET_STRING (MU, PATH, VALUE)BOOLWrites the value; TRUE when it was accepted
  • Paths use the MU data path syntax: sku, items[0].qty, ["text key"]. List positions start at 0.
  • Reading an unknown part, a missing path or a value of another type returns the DEFAULT you pass.
  • Writing follows the same rules as every other writer of MU data, including the document limits. A refused write returns FALSE, and the Problems panel shows one entry per call site (plc-mu-data). It disappears when that call succeeds again.
  • A fixed path in quotes is checked when you deploy: a typo is a compile error with line and column. A path you build at run time, for example with CONCAT, is checked when the call runs.
  • ST has no function call as a statement. Assign the result of a write: ok := MU_SET_INT(PartId, 'qty', 5);.

The program reads the id of the part from the signal PartId and the item position from Slot. It shows the article number, the number of items and the quantity of one item, and marks the part as checked.

PROGRAM CheckPart
VAR_EXTERNAL
PartId : DINT;
Slot : INT;
Sku : STRING(40);
Lines : INT;
Qty : DINT;
Checked : BOOL;
END_VAR
IF MU_EXISTS(PartId) THEN
Sku := MU_GET_STRING(PartId, 'sku', '');
Lines := MU_LEN(PartId, 'items');
Qty := MU_GET_INT(PartId, CONCAT(CONCAT('items[', INT_TO_STRING(Slot)), '].qty'), 0);
Checked := MU_SET_BOOL(PartId, 'checked', TRUE);
END_IF;
END_PROGRAM

For a part with this data:

{
"sku": "A-100",
"items": [
{ "qty": 2 },
{ "qty": 5 }
]
}

and Slot = 1, the program shows Sku = 'A-100', Lines = 2, Qty = 5, and the part then carries "checked": true.

The functions work in continuous simulation and in DES.

In DES they work on the MU data of the run, and what a program writes is part of every snapshot: after a restore the run continues with the same data. The id always means the part that exists now. A part that has left the plant reads as unknown, and a later part that gets the same id is a new part with its own data.

A change of MU data does not wake a program in DES. The program scans when a signal it reads changes, so read the id of the part (or a trigger) from a signal, as in the example. A program that calls MU_* but reads no signal gets the DES hint D7 in the editor: its MU_* calls then run only in scans that something other than a signal starts, for example on start, deploy and after a reset, at the deadline of one of its timers, when the outputs of one of its motion blocks change, when the connection mode changes (if it reads RV_SimulationMode), and when its own change makes it scan again at the same time. A new part or new MU data never starts a scan. D7 is a hint and does not block the run.

To work with the whole data of a part as one value, with keys known only at run time or lists of changing length, the virtual PLC offers the JSON type with MU_READ and MU_WRITE. It is a realvirtual extension of ST and not portable to CODESYS/TwinCAT. For single values, the MU_* functions on this page are the better choice.