Asynchronous Notification

Asynchronous Notifications

You can receive an asynchronous notification when certain events occur in CANlib, for example, when a message arrives or when the CAN controller goes "error passive" due to bus errors. Use canSetNotify() to tell CANlib what events you want notifications for. The different kinds of events are described below.

When the specified kind of event occur, the callback function that was specified in the call to canSetNotify() is called. The callback receives a pointer to a canNotifyData struct; its contents vary depending on the kind of event and is described below.

Example

hnd = canOpenChannel(...);
...
stat = canSetNotify(hnd, wndHandle, canNOTIFY_RX);

When a CAN message is received and the receive queue is empty, a WM__CANLIB message will be sent to the window whose handle is wndHandle. The WM__CANLIB message will have

wParam == hnd
HIWORD(lParam) == 0
LOWORD(lParam) == canEVENT_RX

In the routine that handles WM__CANLIB, you should call canRead() repeatedly, using the handle found in wParam, until it returns canERR_NOMSG or some other error code.

Receive Events

A receive event occurs when a CAN message arrives to a previously empty queue. In other words, there will be a receive event when the queue needs servicing but you will not receive an event for every received CAN message. The canNotifyData eventType field will be set to canEVENT_RX, and the rx.id and rx.time fields will be set to the message ID and timestamp.

Transmit Events

A transmit event occurs whenever a CAN message has been transmitted. The canNotifyData eventType field will be set to canEVENT_TX, and the tx.id and tx.time fields will be set to the message ID and timestamp.

Status Events

A status event occurs when the bus status of the CAN controller has changed, for example, if the controller goes "error passive" due to bus errors. The canNotifyData eventType field will be set to canEVENT_STATUS. The info.status.busStatus, info.status.txErrorCounter, info.status.rxErrorCounter, and info.status.time field will be set to status information.

Error Events

An error frame has been received. The canNotifyData eventType field will be set to canEVENT_ERROR, and the info.busErr.time field will be set to the error timestamp.

Receiving Using Callback Function

You can call the kvSetNotifyCallback() function to register a callback that is called by CANlib when certain events occur. The callback function is called in the context of a high-priority thread created by CANlib. You should be careful not to perform any time consuming tasks within the callback function, and you must also arrange for the synchronization between the callback function and the rest of your code.