• Create a "Standard project" and select CODESYS Control Win V3 as the device.
  • Define the target system by means of the Network scan.



  • Open the Library Manager and add the following libraries:
    CmpApp
    SysTypes2 interfaces



  • Create a persistent variable named PersistentVars.

    Adapt PersistentVars as follows:



    Declaration

    {attribute 'qualified_only'}
    VAR_GLOBAL PERSISTENT RETAIN
        xAutoRestart     : BOOL;
        udiCnt            : UDINT;    
    END_VAR





  • Adapt the POU PLC_PRG as follows:


    Declaration

    VAR
    	sAppName      :    STRING := 'Application';
        result        :    RTS_IEC_RESULT;
        
        pApp          :    POINTER TO CmpApp.APPLICATION;
        udiState      :    UDINT;
        udiOpState    :    UDINT;
        xInit         :    BOOL := TRUE;
        xReset        :    BOOL;
    END_VAR

    Implementation

    IF xInit THEN
        pApp := CmpApp.AppFindApplicationByName(pszString := sAppName, pResult := ADR(result));    
        xInit := False;
        PersistentVars.xAutoRestart := False;
    END_IF
    
    IF xReset THEN
        xReset := FALSE;
        PersistentVars.xAutoRestart := True;        
        CmpApp.AppReset(pApp := pApp, usResetOption := CmpApp.RTS_RESET);
    END_IF


  • Create a new function named taskev_StopDone with the return type DWORD.



    Then adapt the function as follows:



    Declaration

    FUNCTION taskev_StopDone : DWORD
    VAR_INPUT
        EventPrm: CmpApp.EVTPARAM_CmpAppStop;
    END_VAR
    VAR
    END_VAR

    Implementation

    IF PersistentVars.xAutoRestart THEN
    	PersistentVars.udiCnt := PersistentVars.udiCnt + 1;
    	CmpApp.AppStartApplication(PLC_PRG.pApp);
    END_IF


  • Open the tab in the task configuration:



    Create a new event of type StopDone and assign the function taskev_StopDone to it.






  • Start the project and set the variable xReset to TRUE in order to index it.



    The variable PersistantVars.udiCnt is incremented from the application with each restart.