IoDrvEtherCAT (FB)

FUNCTION_BLOCK IoDrvEtherCAT EXTENDS IoDrvBase IMPLEMENTS ICmpIoDrvParameter

Implicit Master Instance

The function block instance for the EtherCAT master. An instance type IoDrvEtherCAT is generated for each EtherCAT master that is inserted into the device tree. The name of the instance corresponds to the name of the master in the device tree. The availability of the instance is shown in the IEC Objects area of the EtherCAT I/O Mapping dialog box of the master

Examples how to use the master instance

  • Restarting the master with the xRestart variable:

    EtherCAT_Master.xRestart := xRestart;

    EtherCAT_Master();

  • Stopping communication on the bus with the xStop variable:

    EtherCAT_Master.xStopBus := xStop;

    EtherCAT_Master();

  • Calling the master for retrieving information about the download success of the configuration parameters:

    EtherCAT_Master();

    xFinish := EtherCAT_Master.xConfigFinished;

Chained List for Checking of All Slaves

For the monitoring of individual slaves in the program the instance is called and the state is determined via wState. For simplification all masters and slaves can be determined by chained lists and all slaves can be checked by a simple WHILE loop. The properties NextInstance and LastInstance exist for both the master and the slave. These properties point to the next or previous slave. For the master there is an additional property, FirstSlave, which provides a pointer to the first slave. In accordance with the following example you can check all slaves:

Example

Following example code checks all slaves for operational mode

PROGRAM PLC_PRG
VAR
   pSlave: POINTER TO ETCSlave;
END_VAR
pSlave := EtherCAT_Master.FirstSlave;
WHILE pSlave <> 0 DO
   pSlave^();
   IF pSlave^.wState = ETC_SLAVE_STATE.ETC_SLAVE_OPERATIONAL THEN
    ;
   END_IF
   pSlave := pSlave^.NextInstance;
END_WHILE

Initially, the first slave is fetched to the master via EtherCAT_Master.FirstSlave. In the WHILE loop the respective instance is called and wState is thus determined. The status can then be checked. The pointer to the next slave is fetched with pSlave^.NextInstance. If the list is finished, the pointer is ZERO and the loop ends.

Restart of EtherCAT master and slaves if option “auto restart slaves” is disabled

Example

Following example code restarts the master and slaves

PROGRAM AutoRestartEtherCAT
VAR
   pMaster : POINTER TO IoDrvEtherCAT;
   pSlave : POINTER TO EtcSlave;
END_VAR
pMaster := g_pFirstMaster;
// Check all masters
WHILE pMaster <> 0 DO
   IF NOT pMaster^.AutoSetOperational THEN
      // Auto restart slaves not set in EtherCAT master -> restart must be done in own application
      // Otherwise the master will do all itself
      IF pMaster^.xError THEN
         IF pMaster^.LastError = ETC_LASTERROR.NO_COMM  OR pMaster^.LastError = ETC_LASTERROR.NO_SLAVES_FOUND THEN
            // No communication at all or no slaves found yet -> restart master
            pMaster^.xRestart := TRUE; // toggle xRestart to restart the master completely
         END_IF
      ELSE
         pMaster^.xRestart := FALSE;
         IF pMaster^.xConfigFinished  THEN
            // Master sucessfully started
            pSlave := pMaster^.FirstSlave;
            // Check all slaves for operational mode
            WHILE pSlave <> 0 DO
               pSlave^(); // required to get the wState output
               IF pSlave^.wState <> ETC_SLAVE_STATE.ETC_SLAVE_OPERATIONAL THEN
                  // slave is not in operational -> try to bring it back to operational
                  CASE pSlave^.wState OF
                     ETC_SLAVE_STATE.ETC_SLAVE_INIT:
                        pSlave^.SetOpMode(ETC_SLAVE_STATE.ETC_SLAVE_PREOPERATIONAL);
                     ETC_SLAVE_STATE.ETC_SLAVE_PREOPERATIONAL:
                        pSlave^.SetOpMode(ETC_SLAVE_STATE.ETC_SLAVE_SAVEOPERATIONAL);
                     ETC_SLAVE_STATE.ETC_SLAVE_SAVEOPERATIONAL:
                        pSlave^.SetOpMode(ETC_SLAVE_STATE.ETC_SLAVE_OPERATIONAL);
                  END_CASE
               END_IF
               pSlave := pSlave^.NextInstance;   // next slave
            END_WHILE
         END_IF
      END_IF
   END_IF
   pMaster^(); // required for xRestart input and the get the outputts of the function block
   pMaster := pMaster^.NextInstance; // next master instance
END_WHILE
InOut:

Scope

Name

Type

Initial

Comment

Input

xRestart

BOOL

FALSE

Rising edge: The master is restarted and all configuration parameters are reloaded.

xStopBus

BOOL

FALSE

Stop the bus when signal is true

Output

xConfigFinished

BOOL

TRUE: Transfer of all configuration parameters was completed with no errors. Communication is running on the bus.

xDistributedClockInSync

BOOL

If distributed clocks are used, then the PLC is synchronized with the first EtherCAT slave with the DC option activated. The output is TRUE as soon as the synchronization is ended successfully. This signal can be used, for example, for activating SoftMotion function blocks when the PLC is in synchronous mode. Otherwise position jumps can occur. When starting the PLC, the output is FALSE and switches to TRUE after a few seconds. If the synchrony is lost due to an error, then the output is reset to FALSE.

xError

BOOL

The output is TRUE when an error occurs when starting the EtherCAT stack, or communication with the slaves is interrupted because no more messages can be received (for example, due to a damaged cable).

xSyncInWindow

BOOL

The output is TRUE when the Sync Window Monitoring option is activated and the synchronization of all slaves is within the Sync Window.

Properties:

Methods:

Structure: