Code example

2.4. Code example

The example program EcMasterDemoDc demonstrates, in contrast to EcMasterDemo, how to programmatically configure DCM parameters and how the application can generate the DCM log.

DCM Configuration (optional)
/* initialize the master */
dwRes = ecatInitMaster(&oInitParms);

/* configure the master */
dwRes = ecatConfigureNetwork(eCnfType_Filename, (EC_T_BYTE*)"ENI.xml", (EC_T_DWORD)OsStrlen("ENI.xml"));

/* register client */
EC_T_REGISTERRESULTS oRegisterClientResults;
OsMemset(&oRegisterClientResults, 0, sizeof(EC_T_REGISTERRESULTS));
dwRes = ecatRegisterClient(EcMasterNotifyCallback, pAppContext, &oRegisterClientResults);

/* configure DC */
EC_T_DC_CONFIGURE oDcConfigure;
OsMemset(&oDcConfigure, 0, sizeof(EC_T_DC_CONFIGURE));
oDcConfigure.dwTimeout = ETHERCAT_DC_TIMEOUT;
dwRes = ecatDcConfigure(&oDcConfigure);

/* configure DCM Bus Shift */
EC_T_DCM_CONFIG oDcmConfig;
OsMemset(&oDcmConfig, 0, sizeof(EC_T_DCM_CONFIG));
oDcmConfig.eMode = eDcmMode_BusShift;
oDcmConfig.u.BusShift.nCtlSetVal = DCM_CONTROLLER_SETVAL_NANOSEC;
oDcmConfig.u.BusShift.bLogEnabled = EC_TRUE;
dwRes = ecatDcmConfigure(&oDcmConfig, 0 /* dwInSyncTimeout */);

/* set EtherCAT devices to OPERATIONAL state */
dwRes = ecatSetMasterState(ETHERCAT_STATE_CHANGE_TIMEOUT, eEcatState_OP);

See also

emDcConfigure()

See also

emDcmConfigure()

DCM Log (optional)
do
{
    /* wait for next cycle (event from scheduler task) */
    /* ... */

    /* process all received frames (read new input values) */
    dwRes = ecatExecJob(eUsrJob_ProcessAllRxFrames, &oJobParms);

    /* ... */

    EC_T_CHAR* szDcmLog = EC_NULL;
    ecatDcmGetLog(&szDcmLog);
    if (EC_NULL != szDcmLog)
    {
        ((CAtEmLogging*)pEcLogContext)->LogDcm(szDcmLog);
    }

    /* ... */
} while (!pAppContext->bJobTaskShutdown);

See also

emDcmGetLog()