Devices and Channels

Identifying Devices and Channels

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.

stat = canGetNumberOfChannels(&chanCount);
if (stat < 0) ... ;
if (chanCount < 0 || chanCount > 300) {
printf("ChannelCount = %d but I don't believe it.\n", chanCount);
exit(1); // Error
} else {
printf("%d channels.\n", chanCount);

Channel Information

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.

stat = canGetNumberOfChannels(&chanCount);
if (stat < 0) ... ;
for (i=0; i < chanCount; i++) {
DWORD tmp;
printf("== Channel %d ===============================\n", i);
stat = canGetChannelData(i, canCHANNELDATA_CARD_TYPE, &tmp, sizeof(tmp));
if (stat < 0) ...; // Error
printf("cardtype = 0x%08lx\n", tmp);

See how-to/c/listChannels.c for code sample.

Customized Channel Name

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"

Setting the device's Channel Name from inside Kvaser Device Guide

Note that if no channel name is set, canERR_NOT_IMPLEMENTED will be returned by canGetChannelData() when accessing canCHANNELDATA_CUST_CHANNEL_NAME.

Virtual Channels

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.

The virtual busses are not shared between the virtual devices.

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.

if (hnd < 0) {
printf("Open failed, stat=%d\n", hnd);
// Disconnect from virtual bus 0
// Connect to virtual bus 1