ECS FoE API Example

1 Example project

There is no EoE Example available. Take the source from ECS simpleConfig Example

2 Configuration packet

For FoE it is necessary to change the EtherCAT Set Configuration Packet.

add ECAT_SET_CONFIG_FOE flag:

ptConfigReq->tData.tBasicCfg.ulComponentInitialization |= ECAT_SET_CONFIG_DEVICEINFO | ECAT_SET_CONFIG_FOE;


ECAT_SET_CONFIG_FOE_T* ptFoe;

ptFoe = &ptConfigReq->tData.tComponentsCfg.tFoECfg;

ptFoe->ulTimeout=1000;

3 ESI

Add in the following FoE parameters to the ESI, in the Mailbox area:

<Mailbox DataLinkLayer="1">

   <FoE/>

...

</Mailbox>

4 Register File Indication Request

For example:


uint32_t AppECS_RegisterFileIndication(CIFX_PACKET* ptPkt)

{

  uint32_t ulRet = SUCCESS_HIL_OK;

  ECAT_FOE_REGISTER_FILE_INDICATIONS_REQ_T * ptFoeFileInd = ( ECAT_FOE_REGISTER_FILE_INDICATIONS_REQ_T * ) ptPkt;


  memset( ptFoeFileInd, 0, sizeof(ECAT_FOE_REGISTER_FILE_INDICATIONS_REQ_T) );


  ptFoeFileInd->tHead.ulDest = HIL_PACKET_DEST_DEFAULT_CHANNEL;

  ptFoeFileInd->tHead.ulCmd = ECAT_FOE_REGISTER_FILE_INDICATIONS_REQ;

  ptFoeFileInd->tHead.ulLen = 1+ sizeof("Examplefile.txt")-1;

  ptFoeFileInd->tData.bIndicationType = ECAT_FOE_INDICATION_TYPE_VIRTUAL_FILE;


  memcpy( ptFoeFileInd->tData.abFilename, "Examplefile.txt", sizeof("Examplefile.txt"));

  return ulRet;

}

5 Packet Handler and ECAT_FOE_WRITE_FILE_IND

...

    case ECAT_FOE_WRITE_FILE_IND:

      AppECS_HanldeFoeWriteFileIndication(ptAppData, &ptAppData->aptChannels[ECS_DEMO_CHANNEL_INDEX]->tPacket);

      fPacketCouldBeHandled = true;

      break;

...


and


int32_t AppECS_HanldeFoeWriteFileIndication(APP_DATA_T *ptAppData, CIFX_PACKET* ptPkt)

{


  ptPkt->tHeader.ulLen    = 0;

  ptPkt->tHeader.ulState  = SUCCESS_HIL_OK;

  ptPkt->tHeader.ulCmd   |= 0x01; /* Make it a response */


  Pkt_SendPacket(ptAppData, ECS_DEMO_CHANNEL_INDEX , ptPkt, TX_TIMEOUT);

  return 0;

}

6 Call after Channel Init the function AppECS_RegisterFileIndication


  App_SysPkt_AssembleChannelInitReq(ptPacket);

  ulRet = Pkt_SendReceivePacket(ptAppData, ECS_DEMO_CHANNEL_INDEX, ptPacket, TXRX_TIMEOUT);

  if( ulRet != CIFX_NO_ERROR )

      return ulRet;


  AppECS_RegisterFileIndication(ptPacket);

  ulRet = Pkt_SendReceivePacket(ptAppData, ECS_DEMO_CHANNEL_INDEX, ptPacket, TXRX_TIMEOUT);

    if( ulRet != CIFX_NO_ERROR )

      return ulRet;


  App_SysPkt_AssembleStartStopCommReq(ptPacket, true);

  ulRet = Pkt_SendReceivePacket(ptAppData, ECS_DEMO_CHANNEL_INDEX, ptPacket, TXRX_TIMEOUT);

  if( ulRet != CIFX_NO_ERROR )

      return ulRet;

7 Twincat3


7 Packet Handler and ECAT_FOE_READ_FILE_IND

    case  ECAT_FOE_READ_FILE_IND:

      AppECS_HanldeFoeReadFileIndication(ptAppData, &ptAppData->aptChannels[ECS_DEMO_CHANNEL_INDEX]->tPacket);

      fPacketCouldBeHandled = true;

      break;


and


int32_t AppECS_HanldeFoeReadFileIndication(APP_DATA_T *ptAppData, CIFX_PACKET* ptPkt)

{

  ECAT_FOE_READ_FILE_IND_T *ptReadfileInd= (ECAT_FOE_READ_FILE_IND_T *) ptPkt;

  ECAT_FOE_READ_FILE_RES_T *ptReadfileRes= (ECAT_FOE_READ_FILE_RES_T *) ptPkt;


  ptReadfileRes->tHead.ulSta  = SUCCESS_HIL_OK;

  ptReadfileRes->tHead.ulCmd  |= 0x01; /* Make it a response */


  if (ptReadfileInd->tData.ulPassword == 0x1234)

  {

    ptReadfileRes->tHead.ulLen  = sizeof("Hello World");

    memcpy( ptReadfileRes->tData.abData, "Hello World", sizeof("Hello World"));

  }

  else

  {

    ptReadfileRes->tHead.ulLen  = 0;

  }


  Pkt_SendPacket(ptAppData, ECS_DEMO_CHANNEL_INDEX , ptPkt, TX_TIMEOUT);

  return 0;

}

7 TwinCAT


on Desktop is not the file Examplefile.txt with "Hello World"