Integration in TwinCAT (CX9020)

This example describes how a simple PLC program for M-Bus can be written in TwinCAT and how it is linked with the hardware. The task is to read a counter with four digital inputs.

Unpacking the example files Integration in TwinCAT (CX9020) 1:

Hardware

Setting up the components

The following hardware is required:

Set up the hardware and the M-Bus components as described in the associated documentation.

This example assumes that the counter address is known.

Software

Creation of the PLC program

Create a new PLC project for PC-based systems (ARM) and add the TcMBus.lib library.

Next, generate the following global variables:

VAR_GLOBAL
    stComIn     AT%I* : ST_KL6781inData22B;
    stComOut    AT%Q* : ST_KL6781outData22B;
    stCom             : ST_MBUS_Communication;
END_VAR

stComIn: Input variable for the M-Bus terminal.

stComOut: Output variable for the M-Bus terminal.

stCom: Required for communication with M-Bus.

Then create a program (CFC) for background communication with M-Bus. The FB_MBUSKL6781() block is called in this program. Make sure to link the communication block with stComIn, stComOut and stCom.

Integration in TwinCAT (CX9020) 2:

Create a MAIN program (CFC) in which the block FB_MBUS_REL_PadIn4() is called up. Link the input usiAddress of the counter block with the local variable usiAddress and stCom with the global variable stCom.

Integration in TwinCAT (CX9020) 3:

Go to the task configuration and give the task a lower interval time. More detailed information can be found in the FB_MBUSKL6781() block description.

Integration in TwinCAT (CX9020) 4:

Load the project to the CX as the boot project and save it.

Configuration in the System Manager

Create a new TwinCAT System Manager project, select the CX as the target system, and search for the associated hardware.

Add the PLC program created above under PLC configuration. The two tasks are listed when the PLC project is expanded in the tree view. However, since the variables stComIn and stComOut are to be processed faster, move them to the background communication task via drag & drop.

Integration in TwinCAT (CX9020) 5:

Now link the global variables of the PLC program with the Bus Terminal inputs and outputs, create the allocations, and activate the configuration. Then start the device in run mode.

Your CX is now ready for use.

After starting the PLC, the current values are regularly read by the counter.