Skip to content

Digital Input

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

Step 1: Define the Interface

Create an interface I_DigitalInput that defines the properties for the Digital Input component.

1
2
3
INTERFACE I_DigitalInput
    PROPERTY IsActive : BOOL
END_INTERFACE

Step 2: Create the Digital Input Component

Create a function block DigitalInput that implements the I_DigitalInput interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
FUNCTION_BLOCK DigitalInput IMPLEMENTS I_DigitalInput
VAR
    _Input : REFERENCE TO BOOL;
END_VAR

PROPERTY IsActive : BOOL
    GET IsActive := _Input;
END_PROPERTY

END_FUNCTION_BLOCK

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

The FB_Init method initializes the reference to the actual digital input variable. This allows the dependency input 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)   
    Input : REFERENCE TO BOOL;
END_VAR

_Input REF= Input;

END_METHOD

Step 4: Using the Digital Input Component

To use the DigitalInput component, create an instance of it in your main program or another function block. Initialize it with the actual digital input variable and read its state through the IsActive property.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
PROGRAM MAIN
VAR
    DigitalInputVar AT %I*: BOOL; // Must be declared before injected into the component
    DigitalInput : DigitalInput(Input := DigitalInputVar);

    InputState       : BOOL;
END_VAR

// Read the digital input state
InputState := DigitalInput.IsActive;

Conclusion

The DigitalInput component can now be reused across different projects, adhering to the SOLID principles, particularly the Dependency Inversion Principle (DIP). By depending on the I_DigitalInput interface, the component remains flexible and easily testable.