Configuring PLC data types for EtherCAT Terminals

TwinCAT can automatically generate PLC data types for EtherCAT Terminals that correspond to the terminal's process data. The generated type is directly linked to a PLC variable, so that the terminal can be accessed via the PLC in a type-safe manner. The configuration is set via ConsumeXml on the terminal's Tree Item. There are two options available: per channel in “Per Channel” mode, or as an aggregated type across all channels.

Requirements

Variant 1: data type per channel

Each channel of the terminal block is assigned its own data type and linked to a separate PLC variable. Index specifies the channel (1-based). DataType contains the device-specific type name from the terminal's ESI file. LinkToPlc specifies the target path in the PLC in the format Variablenname (Instanzname).

ITcSmTreeItem ecSlave = sysManager.LookupTreeItem(@"TIID^EtherCAT Master^EL1809");

// Variant 1: per channel (CreateDataType with channel index)
string perChannelXml = @"<TreeItem>
  <EtherCAT>
    <Slave>
      <Plc>
        <CreateDataType>
          <Channels>
            <Channel Index=""1"">
              <DataType LinkToPlc=""MAIN.Sensor1 (PLC1 Instance)"">MDP5001_250_37E19AF2</DataType>
            </Channel>
            <Channel Index=""2"">
              <DataType LinkToPlc=""MAIN.Sensor2 (PLC1 Instance)"">MDP5001_250_37E19AF2</DataType>
            </Channel>
          </Channels>
        </CreateDataType>
      </Plc>
    </Slave>
  </EtherCAT>
</TreeItem>";

ecSlave.ConsumeXml(perChannelXml);

Variant 2: aggregated type (all channels)

// Variant 2: aggregated type (all channels in one data type)
string aggregatedXml = @"<TreeItem>
  <EtherCAT>
    <Slave>
      <Plc>
        <CreateDataType>
          <DataType LinkToPlc=""MAIN.SensorAll (PLC1 Instance)"">MDP5001_250_B6F8A7F1</DataType>
        </CreateDataType>
      </Plc>
    </Slave>
  </EtherCAT>
</TreeItem>";

ecSlave.ConsumeXml(aggregatedXml);

Making a data type global

A local PLC data type can be converted to a global data type for the GVL. To do this, call MakeDutGlobal on the DUT's Tree Item.

ITcSmTreeItem dut = sysManager.LookupTreeItem(@"TIPC^PLC1^PLC1 Project^DUTs^MyStruct");

dut.ConsumeXml(@"
<TreeItem>
  <PlcDutDef>
    <MakeDutGlobal>
      <Active>true</Active>
    </MakeDutGlobal>
  </PlcDutDef>
</TreeItem>");