Main Content

Generated Code Structure for Reusable Subsystems

This topic assumes that you have generated Structured Text code from a Simulink® model. If you have not yet done so, see Generate Structured Text from the Model Window.

The example in this topic shows generated code for the CODESYS Version 2.3 IDE. Generated code for other IDE platforms looks different.

  1. Open the plcdemo_reusable_subsystem model. To open the model, enter:


  2. Open the PLC Coder app.

  3. Click Generate PLC Code.

    The Simulink PLC Coder™ software generates Structured Text code and places it in current_folder/plcsrc/plcdemo_reusable_subsystem.exp.

  4. If you do not have the plcdemo_reusable_subsystem.exp file open, open it in the MATLAB® editor.

    The following figure illustrates the mapping of the generated code to Structured Text components for a reusable Simulink subsystem. This graphic contains a copy of the hierarchical subsystem, ReusableSubsystem. This subsystem contains two identical subsystems, S1 and S2. This configuration enables code reuse between the two instances (look for the ReusableSubsystem string in the code).

  5. Examine the generated Structured Text code. The code defines FUNCTION_BLOCK S1 once.

    Look for two instance variables that correspond to the two instances declared inside the parent FUNCTION_BLOCK ReusableSubsystem (i0_S1: S1 and i1_S1: S1). The code invokes these two instances separately by passing in different inputs. The code invokes the outputs per the Simulink execution semantics.

  6. For IEC 61131-3 compatible targets, the non-step and the output ssMethodType do not use the output variables of the FUNCTION_BLOCK. Therefore, the generated Structured Text code for SS_INITIALIZE does not contain assignment statements for the outputs Y1 and Y2.


    This optimization is applicable only to IEC 61131-3 compatible targets.