EtherCAT® Communication with Beckhoff® Digital IO Slave Devices EL1004 and EL2004

This example shows how to communicate with EtherCAT devices using the Beckhoff digital I/O terminals EL1004 and EL2004.


To run this example, you need an EtherCAT network that consists of the target computer as EtherCAT Master device and two analog input/output terminals EL1004 and EL2004 as EtherCAT Slave devices. This example requires a dedicated network card that is installed and available on the target computer. Use the dedicated card for the EtherCAT communication. The dedicated card is in addition to the card used for the Ethernet link between the development and target computers.

To test this model:

  1. Connect the network port of the dedicated card in the target computer to the network IN port of the Beckoff® EK1100 coupler.

  2. Assemble Terminals EL1004 and EL2004 with Coupler EK1100.

  3. Loop back the first two I/O ports: Connect ports numbered 1 and 5 of Terminal EL2004 to ports numbered 1 and 5 of Terminal EL1004.

  4. Make sure that the terminals are supplied with the required 24-volt power supply.

  5. Build and download the model onto the target.

For a complete example that configures the EtherCAT network, configures the EtherCAT master node model, and builds then runs the real-time application, see the Simulink Real-Time EtherCAT documentation.

Open the Model

To open the model, in the Command Window, type:


This model drives a pulse wave signal and transmits the signal and its inverse as Boolean values to the EL2004 terminal, and receives the input signal transmitted by the EL1004 terminal.

The EtherCAT initialization block requires that the configuration ENI file is present in the current folder. Copy the example configuration file from the example folder to the current folder. Then, open the model.

copyfile(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','BeckhoffDIOconfig.xml'), '.', 'f' );
copyfile(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','xpcEthercatBeckhoffDIO.slx'), '.', 'f' );
mdl = 'xpcEthercatBeckhoffDIO';
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if isempty( strcmp(systems, mdl ) )
  mdlOpen = 1;

Figure 1: EtherCAT model using Beckhoff® digital I/O terminals EL1004 and EL2004.

Configure the Model

Open the mask for the EtherCAT Init block and provide the required values for the PCI bus and slot numbers for the network card being used for EtherCAT communication. To get these values, in the Command Window, type tg.getPCIInfo('ethernet'). An example command to set configuration parameters to for the EtherCAT Init block is:

set_param('xpcEthercatBeckhoffDIO/EtherCAT Init ','pci_bus','5','pci_slot','0','pci_function','0')

Describe Network with Configurator

Using a third-party EtherCAT configurator that you install on a development computer, generate an EtherCAT configuration file BeckhoffDIOconfig.xml. This file describes the network to the master. An overview of the process for creating the configuration file in the EtherCAT configurator is:

  1. Connect the network (consisting of terminals EK1100, EL1004, and EL2004 in this example) to the computer where the EtherCAT configurator is installed and scan the network to discover the connected devices.

  2. Select the transmit and receive variables to be accessed as signals from the IO terminals.

  3. Define at least one cyclic task, select a task execution rate, and associate the selected variables to the task. You only must select one variable from each PDO to make every variable in that PDO accessible.

  4. Export the configuration file into an XML file. Make sure the name of the XML file is different from the name of your Simulink® model.

Each EtherCAT configuration file is specific to the exact network setup from which it was created (for example, the network discovered in step 1 of the configuration file creation process). The configuration file provided for this example is valid if and only if the EtherCAT network consists of Terminals EK1100, EL1004, and EL2004 from Beckhoff®.

The configuration file defines a set of transmit and receive variables. For this example, four receive variables are defined for the four input channels of Terminal EL1004. Only the first two channels of Terminal EL1004 are used in this example. Make sure the receive variables for channel 1 and channel 2 of terminal EL1004 are selected respectively in the two EtherCAT PDO Receive blocks. These two variables are 'Term 3 (EL1004).Channel 1.Input' and 'Term 3 (EL1004).Channel 2.Input'. In the same way, four transmit variables are defined for the four output channels of terminal EL2004, but only the first two channels are tested in this example. Make sure the transmit variables for channel 1 and channel 2 of terminal EL2004 are selected respectively in the two EtherCAT PDO Transmit blocks. These two variables are 'Term 2 (EL2004).Channel 1.Output' and 'Term 2 (EL2004).Channel 2.Output'.

Build, Download, and Run the Model

Build the model and download to the target computer. Let the model run for 10 seconds

tg = slrt('TargetPC1');
### Starting Simulink Real-Time build procedure for model: xpcEthercatBeckhoffDIO
### Generated code for 'xpcEthercatBeckhoffDIO' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for: xpcEthercatBeckhoffDIO
### Created MLDATX ..\xpcEthercatBeckhoffDIO.mldatx

Display the Target Computer Scopes

Take a snapshot of the target computer video display. Plotted are the signals transmitted to terminal EL2004 and received from terminal 1004. As expected, the transmitted and received signals displayed on the two scopes are identical.

  • Scope 1 displays the outputs of the Ethercat Init block. See the documentation of this block for the meaning of the displayed values.

  • Scope 2 displays the pulse wave and its inverse generated by the application and sent to Terminal EL2004 by the EtherCAT Master.

  • Scope 3 displays the signals received at the two inputs ports of Terminal EL1004.

To take a snapshot of the target scopes, type:


Stop and Close the Model

When the example completes its run, stop and close the model.

if (mdlOpen)