Skip to content

Digital Output

A simple Digital Output component can be created using the SOLID principles outlined in the SOLID Overview document. This example will demonstrate how to create a reusable Digital Output component that can be easily integrated into various projects.

Step 1: Define the Interface

Create an interface I_DigitalOutput that defines the properties for the Digital Output component.

1
2
3
INTERFACE I_DigitalOutput
    PROPERTY On : BOOL
END_INTERFACE

Step 2: Create the Digital Output Component

Create a function block DigitalOutput that implements the I_DigitalOutput interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
FUNCTION_BLOCK DigitalOutput IMPLEMENTS I_DigitalOutput
VAR
    _Output : REFERENCE TO BOOL;
END_VAR

PROPERTY On : BOOL
    GET On := _Output;
    SET _Output := On;
END_PROPERTY

END_FUNCTION_BLOCK

Step 3: Add the FB_Init Method to Initialize the Output Reference

The FB_Init method initializes the reference to the actual digital output variable. This allows the dependency output hardware variable to be injected, adhering to the Dependency Inversion Principle (DIP).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
METHOD FB_Init : BOOL
VAR_INPUT
    bInitRetains : BOOL; // TRUE: the retain variables are initialized (reset warm / reset cold)
    bInCopyCode  : BOOL; // TRUE: the instance will be copied to the copy code afterward (online change)   
    Output : REFERENCE TO BOOL;
END_VAR

_Output REF= Output;

END_METHOD

Step 4: Using the Digital Output Component

To use the DigitalOutput component, create an instance of it in your main program or another function block. Initialize it with the actual digital output variable and control it through the On property.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
PROGRAM MAIN
VAR
    DigitalOutputVar at %Q*: BOOL; // Must be declared before injected into the component
    DigitalOutput : DigitalOutput(Output := DigitalOutputVar);

    Energize         : BOOL;
    Deenergize       : BOOL;
END_VAR

// Turn the digital output on
IF Energize THEN
    Energize        := FALSE;
    DigitalOutput.On := TRUE;
END_IF


// Turn the digital output off
IF Deenergize THEN
    Deenergize      := FALSE;
    DigitalOutput.On := FALSE;
END_IF

END_PROGRAM

Conclusion

This example demonstrates how to create a simple Digital Output component using the SOLID principles. The component is reusable, maintainable, and adheres to best practices in software design. You can easily integrate this component into various projects by injecting the appropriate digital output variable.