Program Example

Overview

This chapter contains a program example with an example code that is used for executing an asynchronous method.

Program example FB_AsyncTest

G-SE-0068436.1.gif-high.gif

 

 

If you want to use the asynchronous mechanism to asynchronously execute a code section or to create own function blocks and to asynchronously call-up a method in an additional task, you have to implement a function block that implements the IF_Async interface.

You can implement this by either using the function blocks you have programmed, as described in this program example, or by using the function blocks of the PD_PacDriveLib library listed here.

The code that is to be executed asynchronously has to be written into the Job() method. An instance of the function block implementing IF_Async has to be transferred to G_ifAsyncMgr.Start(i_ifAsync:=).

 

In the present example, FB_AsyncTest implements the IF_Async interface. The FB_AsyncTest is a specially created sample function block providing the code to be asynchro­nously executed.

For fbAsnycTest: FB_AsyncTest a correct call-up would be as follows:

G_ifAsyncMgr. Start (i_ifAsyncInst := fbAsyncTest);

A StateMachine has to be created and it must be waited in this state until the xDone property of the FB_AsyncTest function block equals TRUE.

The Job() method contains the code to be asynchronously executed, e.g. read or write values on the SERCOS service channel. The xDone property is set depending on which code section in the Job() method is executed or not.

FB_AsyncTest

Declaration

FUNCTION_BLOCK FB_AsyncTest IMPLEMENTS PDL.IF_Async
VAR
   etDiag : GD.ET_Diag;
   etDiagExt : PDL.ET_DiagExt;
   xBusy : BOOL;
   xInitDone : BOOL;
   xDone_loc : BOOL;
   xError_loc : BOOL;
END_VAR  

Program

// This FB initializes the extra task and takes care, that your time consuming function gets called from this task  
IF PDL.G_ifAsyncMgr <> 0 THEN
   PDL.G_ifAsyncMgr.Init(q_xBusy=> xBusy, q_xDone=> xInitDone ,
    q_etDiag=> etDiag , q_etDiagExt=> etDiagExt );
   IF etDiag <> GD.ET_Diag.Ok THEN
      xError_loc:= TRUE;  // Error Reaction see Online Help for possible diag codes
   ELSIF xInitDone THEN
         PDL.G_ifAsyncMgr.Start(i_ifAsync:= THIS^, q_etDiag=> etDiag ,
          q_etDiagExt=> etDiagExt);
         IF etDiag <> GD.ET_Diag.Ok THEN
            xError_loc:= TRUE;  // Error Reaction see Online Help for possible diag codes
         END_IF
   END_IF
END_IF

FB_AsyncTest - Job method and its properties

Declaration

METHOD Job : BOOL  

Program

// Function which needs time and would block the main task
// This function is now executed in a extra low priority task (31)
PDL.FC_WaitTime(i_rWTime:= 1000);  // here is your code
PDL.FC_WaitTime(i_rWTime:= 500);  // here is your code
PDL.FC_WaitTime(i_rWTime:= 250);  // here is your code

Properties FB_AsyncTest.xDone, FB_AsyncTest.xDone.Get and FB_AsyncTest.xDone.Set

FB_AsyncTest.xDone

PROPERTY xDone : BOOL

FB_AsyncTest.xDone.Get

xDone:= xDone_loc;

FB_AsyncTest.xDone.Set

xDone_loc:= xDone;

Properties FB_AsyncTest.xError and FB_AsyncTest.xError.Get

FB_AsyncTest.xError

PROPERTY PUBLIC xError : BOOL

FB_AsyncTest.xError.Get

xError:= xError_loc;

SR_Main (PRG)

Declaration

PROGRAM SR_Main
VAR
   fbAsyncTest: FB_AsyncTest;
   diState: DINT;
   xStart: BOOL;
END_VAR

Program

CASE diState OF
   10:  // Application wait for start  
      IF xStart THEN
         diState:= 20;
      END_IF
   20:  // operation
       ;
   100:  // Error State
       ;
ELSE
   fbAsyncTest();  // This FB initializes the extra task and takes care, that your time consuming function gets called from this task
   IF fbAsyncTest.xDone THEN // Signals that it is done
      diState:= 10;  // now you can continue your application as usual
   ELSIF fbAsyncTest.xError THEN  // Signals an error
      diState:= 100;  // Sets application in Error State
   END_IF
END_CASE