Sample35: Access Ethernet

This article describes the implementation of TC3 C++ modules that communicate directly via an Ethernet card. The sample code queries a hardware address (MAC) from a communication partner by means of the cyclic transmission and reception of ARP packets.

The sample illustrates the direct access to the Ethernet card. The TF6311 TCP/UDP RT function provides access to Ethernet cards on the basis of TCP and UDP, so that an implementation of a network stack is not necessary on the basis of this sample.

Download

Here you can access the source code for this sample.

1. Unpack the downloaded ZIP file.
2. Open the zip file that it contains in TwinCAT 3 by clicking on Open Project ….
3. Select your target system.
4. Build the sample on your local machine (e.g. Build->Build Solution).
5. Note the actions listed on this page under Configuration.
6. Activate the configuration by clicking on Sample35: Access Ethernet 1:.
The sample is ready for operation.

Description

The sample contains an instance of the TcEthernetSample module, which sends and received ARP packets for the purpose of determining the remote hardware address (MAC).

The CycleUpdate method implements a rudimentary state machine for sending ARP packets and waiting for a response with a timeout.

The sample uses two Ethernet components from TwinCAT:

  1. An ITcEthernetAdapter (instance name in the sample is m_spEthernetAdapter) represents an RT Ethernet adapter. It enables access to the adapter parameters such as hardware MAC address, link speed and link errors. It can be used to send Ethernet frames and enables a module instance to register itself as an ItcIoEthProtocol via the registerProtocol method.
  2. The ITcIOoEthProtocol is extended by the sampling module, which ensures that a notification takes place via the ITcEthernetAdapter in case of Ethernet events.

Configuration

The downloaded TwinCAT project must be configured for execution in a network environment. Please carry out the following steps:

This sample demands the use of the TwinCAT driver by the Ethernet card.
1. Start TcRteInstall.exe either from the XAE via the menu TwinCAT->Show Realtime Ethernet compatible devices… or from the hard disk on the XAR systems.
Sample35: Access Ethernet 2:
2. You may have to install and activate the driver with the help of the buttons.
3. TwinCAT must know which Ethernet card is to be used. Open the project in XAE and click on Select I/O / Devices / Device 1 (RT-Ethernet Adapter).
4. Click on the Adapter tab and select the adapter with Search.
5. TcEthernetSample_Obj1 must be configured. Open the instance window and set the following values:
Parameter (Init): SenderIpAddress (IP of the network adapter configured in step 2)
Parameter (Init): TargetIpAddress (IP of target host)
Interface pointer: EthernetAdapter must point to I/O / Devices / Device 1 (RT-Ethernet Adapter).