Example CAA_Timer.library

Cyclic sending of an event:

 

(* example of using the CAA.TIMER-library  *)

PROGRAM PLC_PRG

VAR

        xInit:              BOOL:=FALSE;

        cbTimer:            CB_CALLBACK;

        hTimerSendEv:       CAA.HANDLE;

        teError:            TMR.ERROR;

        ctEventCount:       CAA.COUNT;

END_VAR

 

IF NOT xInit THEN

 (* fill the callback structure *)

 cbTimer.eEvent := CB_TIMER_0;

 cbTimer.eClass := CB_TIMERS;

 cbTimer.eSource := CB_DRIVER;

 (* index of function *)

 cbTimer.iPOUIndex := INDEXOF(TimerEventCB);

 (* create a timer object for cyclic send event,the interval is 10ms *)

 hTimerSendEv:=TMR.Create( xCyclic:=TRUE, ctInterval: =10000, dwParam:=ADR(ctEventCount),

 xHardware:=FALSE, cbTimer, ADR(teError));

  IF teError <> 0THEN

  (* error handling *)

   ;

  END_IF

  (* start the timer *)

  teError:=TMR.Start(hTimerSendEv);

  IF teError <> 0THEN

  (* error handling *)

      ;

  END_IF

  xInit:=TRUE;

ELSE

  IF ctEventCount > 100000 THEN

  (* stop the timer *)

  teError:=TMR.Stop(hTimerSendEv);

  IF teError <> 0THEN

  (* error handling *)

   ;

  END_IF

  (* close, remove and unregister the timer object and the callback-function *)

  teError:=TMR.Close(hTimerSendEv);

 IF teError <> 0THEN

  (* error handling *)

  ;

  END_IF

END_IF

END_IF

 

(* Callback-Function *)

FUNCTION TimerEventCB : BOOL

VAR_INPUT

        dwSpec:          DWORD;

        dwSource:        DWORD;

        dwParam:         DWORD;

END_VAR

VAR

        pEventCount : POINTER TO CAA.COUNT;

END_VAR

 

(* analysis input values dwSpec, dwSource, dwParm *)

;

(* in dwParm transfer the pointer to ctEventCount *)

pEventCount := dwParam;

(* increment the value *)

pEventCount^:= pEventCount^+1;

(* return value *)

TimerEventCB:=TRUE;