KL6401 with CX5120

This example explains how to write a simple PLC program for LON in TwinCAT and how to link it with the hardware. The task is to change the state of a switching output with a button.

Example: LON_Sample_KL6401_CX5120.zip

Hardware

Setting up the components

Set up the hardware and the LON components as described in the respective documents.

This example assumes that a Set button was linked to the first KL1104 input and a Reset button to the second.

Software

Creation of the PLC program

Create a new “TwinCAT XAE project” and a “Standard PLC project”.
Add the library Tc2_LON in the PLC project under “References”.
Generate a global variable list with the name GVL_LON and create the following variables:

VAR_GLOBAL
    bSet                AT %I* : BOOL;
    bReset              AT %I* : BOOL
    stParameter_IN      AT %I* : ST_LON_Parameter_IN_36B;
    stParameter_OUT     AT %Q* : ST_LON_Parameter_OUT_36B;
    stLON_Com                  : ST_LON_Communication
END_VAR

bSet: Input variable for the Set button.

bReset: Input variable for the Reset button.

stParameter_IN: Input structure for the LON terminal (see ST_LON_Parameter_IN_36B).

stParameter_OUT: Output structures for the LON terminal (see ST_LON_Parameter_OUT_36B).

stLON_Com: Structure for the internal communication with LON (see ST_LON_Communication).

All LON function blocks must be called in the same task.

Create a LON program (CFC) in which the function blocks FB_LON_KL6401 and FB_SEND_095_SNVT_switch are called up. At the communication block link the variables stParameter_IN, stParameter_Out and stLON_Com.

KL6401 with CX5120 1:

Link the local variable bSwitch with the global variables bSet and bReset, then with the selected input. Link the local variable stValue (see ST_LON_SNVT_switch) with the selected output, then with the input stValue of the send block.

KL6401 with CX5120 2:

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_LON_KL6401.

KL6401 with CX5120 3:

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 input and output variables of the task.

Now link the variables with the inputs and outputs of the Bus Terminals.

The linking of the LON variables is described in detail below.

Right-click the variable wParameterStatus of the input structure and select "Change link".

KL6401 with CX5120 4:

Select the terminal under "I/O Configuration", click "Parameter Status" and confirm with "OK".

KL6401 with CX5120 5:

In the next step, right-click the structure stParameterReadValue within the input structure and select "Change Link".

wDummy is not linked.

KL6401 with CX5120 6:

Select "All Types" and "Continuous", then select "Parameter Input Data 1" to "Parameter Input Data 8" with the left mouse button and the >SHIFT< key. Then click "OK".

KL6401 with CX5120 7:

Now also link the variables byParameterType and byLONStatus.

KL6401 with CX5120 8:
KL6401 with CX5120 9:

You can now check the connection. To this end, select the KL6401 in the "I/O Configuration" and open it. All terminal data should now show a small arrow. If that is the case, then proceed in exactly the same way with the outputs.

KL6401 with CX5120 10: