linux

canSetNotify

Syntax


#include <canlib.h>

canStatus canSetNotify canSetNotify (const CanHandle hnd,
                                     void (*callback) (canNotifyData *), 
                                     unsigned int notifyFlags, void * tag);

Description

This function allows a user-defined function to be called when certain events occur on the bus.

Input Parameters

handle
A handle to an open CAN circuit.
callback
Function to be called when an event occurs
notifyFlags
The events for which notification messages are to be sent. This value is zero, meaning that no event notification is to occur, or a combination of any of the following constants:
canNOTIFY_RXCAN message reception notification.
canNOTIFY_TXCAN message transmission notification.
canNOTIFY_ERRORCAN bus error notification.
canNOTIFY_STATUSCAN chip status change.
tag
Pointer to user defined data. Passed to callback in the canNotifyData struct.
Output Parameters

None.

Return Value

canOK (zero) if success
canERR_xxx (negative) if failure.

The notification message

When one of the above events take place, the 'callback function is called with a canNotityData struct as an argument which looks like:

typedef struct canNotifyData {
  void *tag;
  int eventType;
  union {
    struct {
      unsigned long time;
    } busErr;
    struct {
      long id;
      unsigned long time;
    } rx;
    struct {
      long id;
      unsigned long time;
    } tx;
    struct {
      unsigned char busStatus;
      unsigned char txErrorCounter;
      unsigned char rxErrorCounter;
      unsigned long time; 
    } status;
  } info;
} canNotifyData;

The 'info' field in the struct is a union that depends on eventType as follows:

canEVENT_RXA CAN message has been received.
The struct info.rx contains the id and reception time of the received message.
canEVENT_TXA CAN message has been transmitted.
The struct info.tx contains the id and transmission time of the transmitted message.
canEVENT_ERRORA CAN bus error is reported by the CAN controller
The struct info.busErr contains the time the error was detected.
canEVENT_STATUSThe CAN controller changes state
The struct info.status contains busstatus, transmit and receive errorcounters and time of the statechange.

The tag field in canNotifyData is a void pointer. This can be used as a reference to some data associated with a specific channel/handle.

Related Topics

An example.