Once we have called canInitializeLibrary() to enumerate the connected Kvaser CAN devices, we can call canGetNumberOfChannels() to get the number of enumerated channels in our system.
We can then call canGetChannelData() for the CANlib channel numbers 0, 1, 2,…, n-1 (where n is the number returned by canGetNumberOfChannels()) to get information about that specific channel.
Example. This code snippet prints the number of enumerated channels found in the PC.
Use canGetChannelData() to obtain data for a specific channel, for example, the hardware type of the CAN interface. The same information can also be retrieved using canGetHandleData(), which instead takes an active CanHandle.
To uniquely identify a device, we need to look at both the canCHANNELDATA_CARD_UPC_NO and canCHANNELDATA_CARD_SERIAL_NO.
Use canIoCtl() to obtain various pieces of information for a specific handle, or to perform special operations.
Example. This code snippet loops through all known channels and prints the type of the CAN card they're on.
See how-to/c/listChannels.c for code sample.
It is possible to set the customized name returned by canCHANNELDATA_CUST_CHANNEL_NAME on the device using Kvaser Device Guide by right clicking on the device channel and selecting "Edit Channel Name"
Note that if no channel name is set, canERR_NOT_IMPLEMENTED will be returned by canGetChannelData() when accessing canCHANNELDATA_CUST_CHANNEL_NAME.
CANlib supports virtual channels that you can use for development, test, or demonstration purposes when you don't have any hardware installed.
For installation information, see Installing Virtual Devices on Linux and Installing Additional Virtual Devices for Windows.
There are 32 virtual busses on each virtual device, and you can connect any virtual channel to any virtual bus on the same virtual device. The default connection of a virtual device is that all of it's channels are connected to the same virtual bus.
We can use canIoCtl() with canIOCTL_DISCONNECT_FROM_VIRTUAL_BUS to disconnect a virtual channel from a virtual bus. Similarly, canIOCTL_CONNECT_TO_VIRTUAL_BUS can be used to connect the now disconnected virtual channel to a new virtual bus.
To open a virtual channel, call canOpenChannel() with the appropriate channel number, and specify canOPEN_ACCEPT_VIRTUAL in the flags argument to canOpenChannel().
Example. This code snippet disconnects the first virtual channel from the virtual bus 0, and reconnects it to virtual bus 1.