KL6581 with CX5120
This sample shows how to write a simple PLC program for EnOcean® in TwinCAT and how to link it with the hardware. The task is to receive four switch signals of an EnOcean® wireless switch module.
Sample: EnOcean_Sample_KL6581_CX5120.zip
![]() | The TwinCAT project is available for download as *.zip file. This must first be unpacked locally so that the archive (*.tnzip file) is available for import into the TwinCAT project. |
Hardware
Setting up the components
The following hardware is required:
- 1x CX5120 Embedded PC
- 1x KL6581 EnOcean® master terminal
- 1x KL6583-0000 EnOcean® transmitter and receiver
- 1x KL9010 end terminal
Set up the hardware and the EnOcean® components as described in the associated documentation.
This example assumes that the ID of the wireless switch module is known.
Software
Creation of the PLC program
Create a new "TwinCAT XAE Project" and create a "Standard PLC Project".
Add the Tc2_EnOcean library to the PLC project under "References".
Create a global variable list with the name GVL_EnOcean and create the following variables:
VAR_GLOBAL
stKL6581Input AT %I* : KL6581_Input;
stKL6581Output AT %Q* : KL6581_Output;
stKL6581 : STR_KL6581;
END_VAR
Name | Type | Description |
---|---|---|
stKL6581Input | Input variable for the EnOcean® terminal. | |
stKL6581Output | Output variable for the EnOcean® terminal. | |
stKL6581 | Required for the communication with EnOcean®. |
All EnOcean® function blocks must be called in the same task.
Create a MAIN program (CFC) in which the function blocks FB_KL6581 and FB_Rec_RPS_Switch are called. Ensure that the communication block is linked with the structures stKL6581Input, stKL6581Output and stKL6581.

The input dw_ID of the receive block is linked with the local variable dwId (ID from wireless switch module) and str_KL6581 with the global variable stKL6581.

Go to the task configuration and give the task a lower interval time.
Further conditions can be found in the description of the function block FB_KL6581.

I/O configuration
Select the CX as target system and initiate a search for its hardware. In the project instance within the PLC section, you can see that the input and output variables are assigned to the task.
Now link the global variables with the inputs and outputs of the bus terminals.
The linking of the EnOcean® variables is described in detail below.
Right-click the structure stKL6581Input and select “Change link”.

Under "I/O Configuration" select the terminal, select "All Types" and "Continuous", then select “Status” to “InputStatus” with the left mouse button and the >SHIFT< key. Then click "OK".

You can now check the link. To do this go onto the KL6581 and open it. All terminal data must now be marked with an arrow. If that is the case, then proceed in exactly the same way with the outputs.