# Error detection and diagnosis The EC-Master API functions generally return [`EC_E_NOERROR`](error-codes_generic.html.md#c.EC_E_NOERROR) or an error code. One of the parameters that the client must set when registering with the EC-Master is a generic notification callback function (`emNotify()`). If an error is detected, the EC-Master calls this function. The EC-Master log messages are enabled if [`EC_T_LOG_PARMS`](api_general_functions.html.md#_CPPv414EC_T_LOG_PARMS) is configured as described in [`emInitMaster()`](api_general_functions.html.md#_CPPv412emInitMaster10EC_T_DWORDPK22EC_T_INIT_MASTER_PARMS) or configured using [`emSetLogParms()`](api_diagnosis.html.md#_CPPv413emSetLogParms10EC_T_DWORDP14EC_T_LOG_PARMS). ## Cyclic cmd WKC validation and Frame Loss New input values received from the SubDevices will be written into the input process data memory only if the WKC of the corresponding datagram is not 0 and not greater than the configured WKC value. #### SEE ALSO [emNotify - EC_NOTIFY_CYCCMD_WKC_ERROR](api_diagnosis.html.md#emnotify-ec-notify-cyccmd-wkc-error) #### SEE ALSO [Working Counter (WKC) State in Diagnosis Image](#working-counter-wkc-state-in-diagnosis-image) It is possible to clear INPUTs in the process data image if an error is detected using the following IOCTLs: #### SEE ALSO [emIoCtl - EC_IOCTL_SET_ZERO_INPUTS_ON_WKC_ERROR](api_general_functions.html.md#emioctl-ec-ioctl-set-zero-inputs-on-wkc-error) #### SEE ALSO [emIoCtl - EC_IOCTL_SET_ZERO_INPUTS_ON_WKC_ZERO](api_general_functions.html.md#emioctl-ec-ioctl-set-zero-inputs-on-wkc-zero) #### SEE ALSO [emIoCtl - EC_IOCTL_SET_ZERO_INPUTS_ON_FRAME_LOSS](api_general_functions.html.md#emioctl-ec-ioctl-set-zero-inputs-on-frame-loss) ## Working Counter (WKC) State in Diagnosis Image Each cyclic Process Data cmd has its own WKC State bit in the diagnosis image. The state is updated on frame receiving, frame loss detection or link disconnection. All process data variables within a datagram have the same WKC State value. If the WKC value of the received datagram is not as expected, the WKC State bit is set to 1 for this datagram (error). In case of MainDevice Sync Units (MSU) if all the commands related to the MSU return WKC 0, the WKC State will be set to 1. The WKC State offset within the Diagnosis Image is available at [`EC_T_CFG_SLAVE_INFO`](api_slave_control.html.md#_CPPv419EC_T_CFG_SLAVE_INFO) and [`EC_T_PROCESS_VAR_INFO_EX`](api_process_data.html.md#_CPPv424EC_T_PROCESS_VAR_INFO_EX), [`EC_T_MSU_INFO`](api_diagnosis.html.md#_CPPv413EC_T_MSU_INFO) see [`emGetDiagnosisImagePtr()`](api_process_data.html.md#_CPPv422emGetDiagnosisImagePtr10EC_T_DWORD), [`emGetCfgSlaveInfo()`](api_slave_control.html.md#_CPPv417emGetCfgSlaveInfo10EC_T_DWORD9EC_T_BOOL9EC_T_WORDP19EC_T_CFG_SLAVE_INFO), [`emGetSlaveInpVarInfoEx()`](api_process_data.html.md#_CPPv422emGetSlaveInpVarInfoEx10EC_T_DWORD9EC_T_BOOL9EC_T_WORD9EC_T_WORDP24EC_T_PROCESS_VAR_INFO_EXP9EC_T_WORD), [`emGetSlaveOutpVarInfoEx()`](api_process_data.html.md#_CPPv423emGetSlaveOutpVarInfoEx10EC_T_DWORD9EC_T_BOOL9EC_T_WORD9EC_T_WORDP24EC_T_PROCESS_VAR_INFO_EXP9EC_T_WORD), [`emGetMasterSyncUnitInfo()`](api_diagnosis.html.md#_CPPv423emGetMasterSyncUnitInfo10EC_T_DWORD9EC_T_WORDP13EC_T_MSU_INFO). The application can check the WKC State of a variable e.g. as follows: ```cpp EC_T_CFG_SLAVE_INFO oSlaveInfo; EC_T_BYTE* pbyDiagnosisImage = emGetDiagnosisImagePtr(); EC_T_BYTE byWkcState = 1; if (EC_NULL != pbyDiagnosisImage) { if (EC_NOERROR == emGetCfgSlaveInfo(EC_TRUE, 2302, &oSlaveInfo)) { EC_GETBITS(pbyDiagnosisImage, &byWkcState, oSlaveInfo.wWkcStateDiagOffsOut[0], 1); } } if (1 == byWkcState) { /* ... error ... */ } ``` #### SEE ALSO [Cyclic cmd WKC validation and Frame Loss](#cyclic-cmd-wkc-validation-and-frame-loss) ### Behavior in case of automatically adjusted expected WKC value Optionally, the expected WKC value can be automatically adjusted according to the state and the presence of the SubDevices. See [emIoCtl - EC_IOCTL_SET_AUTO_ADJUST_CYCCMD_WKC_ENABLED](api_slave_control.html.md#emioctl-ec-ioctl-set-auto-adjust-cyccmd-wkc-enabled). The WKC State bits change synchronized to the corresponding notification, e.g. on link disconnection all SubDevices disappear and the behavior is as follows: - All WKC State bits are set to 1 as missing data is not expected. - The MainDevice notifies the application about the link disconnection and the disappearing of SubDevices. - All WKC State bits are set to 0 as it is now expected to have no process data if all SubDevices are absent. #### SEE ALSO - [emNotify - EC_NOTIFY_ETH_LINK_NOT_CONNECTED](api_diagnosis.html.md#emnotify-ec-notify-eth-link-not-connected) - [emNotify - EC_NOTIFY_SLAVE_PRESENCE](api_busscan.html.md#emnotify-ec-notify-slave-presence) ## MainDevice Sync Units (MSU) MSUs are useful in grouping specific data (into consistency units) > - Process Image: Variables are stored together within one memory block > - Error checking: Own datagram(s) allow individual WKC state check (consistency unit) ![Diagram of EC-Master Sync Units (MSU) showing how SubDevice process data is grouped into consistency units with individual EtherCAT datagrams and WKC checking within one EtherCAT frame](../Media/MasterSyncUnits_MSU.png) Figure out the MSU offsets by calling the functions [`emGetMasterSyncUnitInfoNumOf()`](api_diagnosis.html.md#_CPPv428emGetMasterSyncUnitInfoNumOf10EC_T_DWORD) and [`emGetMasterSyncUnitInfo()`](api_diagnosis.html.md#_CPPv423emGetMasterSyncUnitInfo10EC_T_DWORD9EC_T_WORDP13EC_T_MSU_INFO), as described in the corresponding documentation.