User manual

CAN API reference
22 © 2007-2010 Analytica GmbH
CANSetCallback, CANSetCallbackEx
CANSetCallback, CANSetCallbackEx — Defines an asynchronous callback function, which is called for
each incoming CAN telegram.
Syntax
#include <AnaGateDllCan.h>
typedef void (WINAPI * CAN_PF_CALLBACK)(int nIdentifier, const char *
pcBuffer, int nBufferLen, int nFlags, int hHandle);
int CANSetCallback(int hHandle, CAN_PF_CALLBACK pCallbackFunction);
typedef void (WINAPI * CAN_PF_CALLBACK_EX)(int nIdentifier, const char
* pcBuffer, int nBufferLen, int nFlags, int hHandle);
int CANSetCallbackEx(int hHandle, CAN_PF_CALLBACK_Ex
pCallbackFunctionEx);
Parameter
hHandle Valid access handle.
pCallbackFunction Function pointer to the private callback function. Set this parameter to
NULL to deactivate the callback function. The parameters of the callback
function are described in the documentation of the CANWrite funciton.
pCallbackFunctionEx Function pointer to the private callback function. Set this parameter to
NULL to deactivate the callback function. The parameters of the callback
function are described in the documentation of the CANWriteEx funciton.
Return value
Returns Null if successful, or an error value otherwise (Appendix A, Return Codes).
Description
Incoming CAN telegrams can bei received via a callback function, which can be set by a simple API call.
If a callback function us set, is will be called by the API asynchronous.
Caution
The callback fucntion is called from a thread which is started by the CAN API and which is
reading data from the socket. Because of this behaviour the callback code is executed by the
thread context of the API and therefore it uses it the heap memeory of the API DLL and not
the application program. So programming code should not use functions like new, delete,
alloc or free which allocate, free or reallocate heap memory inside the callback.
See the following example for using a callback.
#include <AnaGateCANDll.h>
// Defintion of a callback, which writes incoming CAN data with timestamp to console