KL6831 with CX5120
This sample describes how to write a simple PLC program for SMI in TwinCAT and how to link it with the hardware.
A motor is controlled stepwise with a button. One button sends the Up command, the other the Down command.
Sample: SMI_Sample_KL6831_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
- 1x CX5120 Embedded PC
- 1 x KL1104 digital 4-channel input terminal (for the Up, Down and Reset function)
- 1x KL6831 SMI terminal
- 1x KL9010 end terminal
Set up the hardware and the SMI components as described in the documentation.
The sample assumes that a Reset button has been connected to the first input of the KL1104, an Up button to the second input and a Down button to the third input. There is a drive at the SMI device address 1.
Software
Creation of the PLC program
Create a new "TwinCAT XAE Project" and a "Standard PLC Project".
Add the library Tc2_SMI in the PLC project under "References".
Create the following global variables:
VAR_GLOBAL
bReset AT %I* : BOOL;
bUp AT %I* : BOOL;
bDown AT %I* : BOOL;
stKL6831InData AT %I* : ST_KL6831KL6841InData;
stKL6831OutData AT %Q* : ST_KL6831KL6841OutData;
stCommandBuffer : ST_SMICommandBuffer;
END_VAR
Name | Type | Description |
---|---|---|
bReset | BOOL | Input variable for the Reset button |
bUp | BOOL | Input variable for the Up button |
bDown | BOOL | Input variable for the Down button |
stKL6831InData | Input variable for the SMI terminal | |
stKL6831OutData | Output variable for the SMI terminal | |
stCommandBuffer | Required for communication with SMI |
Create a program (CFC) for the background communication with SMI. The function block FB_KL6831KL6841Communication is called in the program. With the communication block, ensure that the structures stKL6381InData and stKL6831OutData and stCommandBuffer are linked.

Create a MAIN program (CFC) in which the function blocks FB_SMIUpStep and FB_SMIDownStep are called.
The input bStart of the function block for sending the Up command is linked to the global variable bUp.
The input bStart of the function block for sending the Down command is linked to the global variable bDown.

Navigate to the task configuration section and configure the PlcTask. By way of example, the task is assigned priority 16 and a cycle time of 10 ms.

Create a further task for the background communication. Assign a higher priority (smaller number) and a lower interval time to this task than the PlcTask.

Add the program for the communication to this task. More precise information on the task configuration can be found in the function block description.

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 corresponding tasks.

Now link the global variables of PLC program with the inputs and outputs of the bus terminals. Create the Solution and enable the configuration.
The lamp with the maximum brightness value is switched on by pressing the first push button. The second push button can be used to switch it off again.
You can use the Reset button to reset the inputs in arrBufferMaximumDemandMeter and arrBufferOverflowCounter.