MATLAB: How to define global variables using PLC Coder

coderdsmglobalplcSimulink PLC Codersimulink.signalvar_globalvariable

How do I define global variables using PLC Coder?

Best Answer

The supported way of defining global variables for PLC Coder is using Data Store Memory (DSM) blocks that resolve to Simulink.Signal objects. See below documentation page for more information:
To model externally defined output data, use DSM read/write block to access the data; to suppress the definition as VAR_GLOBAL, add the Simulink.Signal name to the "Externally defined symbols" list as follows: Simulation > Model Configuration Parameters > PLC Code Generation > Symbols pane > Externally Defined Symbols.
Attached is an example which illustrates this. Things to note:
  1. The global variable is defined as a 'Simulink.Signal' in the PreLoadFcn callback of the model (File > Model Properties > Model Properties > Callbacks tab)
  2. In the Data Store Memory block's Signal Attributes tab, check the 'Data store name must resolve to Simulink signal object.'.
Please refer to PLC Coder Release Notes regarding global variable limitations for specific target IDEs.
Note that in contrast to Embedded Coder, PLC Coder does not support using signals that resolve to a Simulink.Signal object to define global variables.