Skip to main content

Designing and Developing

When you program graphically in the class diagram, your changes automatically cause adaptations for the objects in the POUs view and Devices view. New objects are inserted in their basic structure, and existing objects are adapted. For example, you can insert declarations in the POUs via the class diagram.

This allows you to design the architecture of your application in a class diagram and get the suitable POUs synchronously. Then you supplement the implementations in the POUs.

The following steps create a class diagram for an application.

1. Creating a project

  1. Open CODESYS.

  2. Click File → New Project.

  3. Specify the name and location, and select the Standard Project template.

    The Standard Project dialog opens.

  4. For PLC_PRG in, select the Structured Text (ST) implementation language.

    The project is created.

  5. Select the Application code and click Add Object → UML Class Diagram.

    The Add UML Class Diagram dialog opens.

  6. Specify the object name CD_Simple.

  7. Select the Import project structure to active class diagram option.

  8. Click Add.

    _uml_img_cd_simple.png
  9. Save the project as SimpleMachine.project.

2. Identifying interfaces

Interfaces determine publicly visible methods and properties in order to implement classes.

  1. Open the editor of the class diagram CD_Simple.

  2. In the ToolBox view, select an interface.

  3. Drag the interface to the editor.

    The Add Interface dialog opens.

  4. Specify the name IBaseMachine. Click Add to exit the dialog.

  5. In the ToolBox view, select a method.

  6. Drag the method to the interface.

    The Add Interface method dialog opens.

  7. Specify the name DoIt.

  8. Specify the return type BOOL. Click Add to exit the dialog.

  9. In the ToolBox view, select a property.

  10. Drag the property to the interface.

    The Add Interface property dialog opens.

  11. Specify the name StateNumber.

  12. Specify the return type INT. Click Add to exit the dialog.

    _uml_img_cd_simple_imachine.png

Tip

For more information about interfaces, see: Object: Interface and Object: Interface Property

3. Identifying classes and implementing an interface

Different classes that implement the same interface contain the same methods and properties. If the interface is changed or extended later, then this will affect all implementing classes.

  1. Open the editor of the class diagram CD_Simple.

  2. In the ToolBox view, select a class.

  3. Drag the class to the editor.

    The Add POU dialog opens.

  4. Specify the name FB_Machine.

  5. Select the Function block option.

  6. For implementation language, select UML Statechart (SC). Click Add to exit the dialog.

    The class FB_Machine is added to the class diagram.

  7. Select the class.

    Command icons are displayed above the class.

  8. Click the _uml_icon_realization.png Realization (IMPLEMENTS) command icon and click the IMachine interface.

    The Select Implementation Language dialog opens.

  9. Select Structured Text (ST).

    The class FB_Machine inherits the properties and methods of the interface and therefore implements the interface IMachine.

    _uml_img_cd_simple_fbmachine.png

4. Generalizing

In generalization, a class inherits the properties and methods of another class.

  1. Open the editor of the class diagram CD_Simple.

  2. In the ToolBox view, select a class.

  3. Drag a class to the editor.

    The Add POU dialog opens.

  4. Specify the name FB_Machine_A.

  5. Select the Extends option and specify the class FB_Machine in the input field.

    The function block FB_Machine is extended and therefore it is a generalization of FB_Machine_A.

  6. For implementation language, select UML Statechart (SC). Click Add to exit the dialog.

    The class FB_Machine_A is created in the class diagram. A generalization exists between the classes FB_Machine and FB_Machine_A. FB_Machine_A inherits its properties and methods from FB_Machine.

  7. In the ToolBox view, select the Variable declaration element and drag it to the class FB_Machine_A.

    The Auto Declare dialog opens.

  8. Specify the name iCounter and the data type INT. Click OK to exit the dialog.

    Now the class FB_Machine_A has its own variables in addition to the inherited properties and methods.

  9. In the class FBMachine_A, select the method DoIt() and press the Del key.

    The method is removed from the diagram, as well as the object in the Devices view. The inheriting function block FBMachine_A does not need its own special implementation for the method. Instead, it calls the method DoIt of the base function block FBMachine when necessary.

    Note

    When you call methods, which are assigned to a base function block and inherited, use the pointer SUPER.

    _uml_img_cd_simple_fbmachine_a.png

Tip

For more information, see Extending a Function Block and Pointer: SUPER.

5. Instantiate

  1. Open the editor of the class diagram CD_Simple.

  2. Select the program PLC_PRG.

    Command icons are displayed above the program.

  3. Click the _uml_icon_composition.png Composition (VAR) command icon and click the FB_Machine_A class.

    The Variable declaration dialog opens.

  4. Specify the name fb_A.

    The object fb_A of the class FBMachine_A is instantiated. Now, the instance of the class in its concrete form is generated at runtime.

    _uml_img_cd_simple_plc_prg.png

The class diagram displays all objects that are relevant for generating the application code. Above all, however, the class diagram visualizes the relationships between the objects: Who implements whom? Who inherits from whom? Who instantiates what? Otherwise, this information is hidden in the declaration and for example cannot be seen in the Devices view.

6. Implementing

  1. Open the function block FBMachine.

  2. Declare the function block variables.

    FUNCTION_BLOCK FBMachine IMPLEMENTS IBaseMachine
    VAR
        iState: INT;
        iMachine : INT;
    END_VAR
  3. Implement the function block.

    iState := iState + 1; // Dummy for control ID
  4. Implement its method DoIt.

    METHOD PUBLIC DoIt : BOOL
    iMachine := iMachine + 1; // Dummy
  5. Implement the Get method of the property StateNumber of FBMachine.

    StateNumber := iState;
  6. Implement the Set method of the property StateNumber of FBMachine.

    iState := StateNumber;
  7. Open the function block FBMachine_A.

  8. Declare the function block variables.

    FUNCTION_BLOCK FBMachine IMPLEMENTS IBaseMachine
    VAR
     iCounter: INT;
    END_VAR
  9. Implement the function block.

    iCounter := iCounter + 1;
  10. Implement the Get method of the property StateNumber of FBMachine_A.

    StateNumber := iCounter;
  11. Implement the Set method of the property StateNumber of FBMachine_A.

    iCounter := StateNumber;
  12. Click Build → Generate Code.

    The application is compiled without errors.

    _uml_img_cd_simple_objects.png
  13. Download and start the application.

    You can monitor the function block instance fb_A.

    _uml_img_cd_simple_monitoring.png