Initialization

Library Initialization

The first call to the library must be a call to canInitializeLibrary(). This function will initialize the CANlib library and enumerate all currently available CAN channels. It is sufficient to call this routine once. Calling it more than once will have no effect.

Example This initializes the CANlib library.

Library Deinitialization and Cleanup

Strictly speaking it is not necessary to clean up anything before terminating the application. If the application quits unexpectedly, the device driver will ensure the CAN controller is deactivated and the driver will also ensure the firmware (if any) is left in a consistent state.

To deinitialize the library in an orderly fashion you may want to call canWriteSync() with a short timeout for each open handle before closing them with canClose(), to ensure the transmit queues are empty.

Note
When calling canUnloadLibrary(), all previously opened CAN handles will be closed and invalidated.

If you want to reinitialize the library from scratch in an application that already has initialized the library, call canUnloadLibrary(). You can then start afresh by calling canInitializeLibrary() again.

Example This deinitializes the CANlib library.

if (stat < 0) {
printf("Unload library failed.");
}

Manually Enumerating CAN channels

The function canEnumHardwareEx() scans all currently connected devices and creates a completely new set of CANlib channel numbers, while still keeping all currently opened channel handles valid and usable. This can be viewed upon as a replacement for calling the function canUnloadLibrary() followed by canInitializeLibrary() which do invalidate all open channel handles.

One thing to keep in mind when using this functionality is to never track devices based on their CANlib channel number, since this number may change anytime canEnumHardwareEx() is called. To retrieve information about a specific channel use canGetHandleData(hnd, …), instead of canGetChannelData(channel, …).

Note
On Linux, no re-enumeration is needed since enumeration takes place when a device is plugged in or unplugged.