Open Channel

Once we have called canInitializeLibrary() to enumerate the connected Kvaser CAN devices, the next call is likely to be a call to canOpenChannel(), which returns a handle to a specific CAN circuit. This handle is then used to all subsequent calls to the library. The canOpenChannel() function takes two arguments, the first of which is the number of the desired channel, the second argument is modifier flags canOPEN_xxx.

canOpenChannel() may return several different error codes, one of which is canERR_NOTFOUND. This means that the channel specified in the first parameter was not found, or that the flags passed to canOpenChannel() is not applicable to the specified channel.

Open as CAN

No special canOPEN_xxx modifier flag is needed in the flags argument to canOpenChannel() when opening a channel in CAN mode.

hnd = canOpenChannel(channel_number, 0);

Example. This example opens channel 0 for exclusive usage by this application.

if (hnd < 0) {
printf("Open failed, stat=%d\n", hnd);
}

Open as CAN FD

To open a channel in CAN FD mode, either canOPEN_CAN_FD or canOPEN_CAN_FD_NONISO needs to be given in the flags argument to canOpenChannel().

hnd = canOpenChannel(channel_number, canOPEN_CAN_FD);

Example. This example opens channel 0 in CAN FD mode for exclusive usage by this application.

if (hnd < 0) {
printf("Open failed, stat=%d\n", hnd);
}

Close Channel

Closing a channel is done using canClose(). If no other handles are referencing the same CANlib channel, the channel is taken off bus. The handle can not be used for further references to the channel, so any variable containing this handle should be set to canINVALID_HANDLE.

canStatus stat = canClose(hnd);
if (stat < 0) {
printf("Close failed.");
}

Set CAN Bitrate

Use canSetBusParams() to set the CAN bus parameters. The bus parameters include the bit rate, the position of the sampling point etc, they are also described in most CAN controller data sheets. CANlib provides a few default set of parameters for the most common bus speeds through the canBITRATE_xxx constants. When using these constants in the call to canSetBusParams(), set the rest of the arguments to '0'.

Example. To set the bus speed to 500 kbit/s:

stat = canSetBusParams(hnd, canBITRATE_500K, 0, 0, 0, 0, 0);
if (stat < 0) {
printf("canBusParams failed, status=%d\n", stat);
exit(1);
}

To set a bit rate that is not predefined, you have to calculate the appropriate bus parameters yourself.

Example. Set the speed to 125 kbit/s, each bit comprising 8 (= 1 + 4 + 3) quanta, the sampling point occurs at 5/8 of a bit; SJW = 1; one sampling point.

stat = canSetBusParams(hnd, 125000, 4, 3, 1, 1, 0);

Example. Set the speed to 111111 kbit/s, the sampling point to 75%, the SJW to 2 and the number of samples to 1:

stat = canSetBusParams(hnd, 111111, 5, 2, 2, 1, 0);

Set CAN FD Bitrate

In CAN FD we need to set both the arbitration/nominal bitrate, using canSetBusParams(), and the data phase bitrate, using canSetBusParamsFd(). CANlib provides a few default set of parameters for the most common CAN FD bus speeds through the canFD_BITRATE_xxx constants. When using these constants in the call to canSetBusParams() and canSetBusParamsFd(), set the rest of the arguments to '0'.

Example. Set the nominal bitrate to 500 kbit/s, with sampling point at 80% and the data phase bitrate to 1000 kbit/s, with sampling point at 80%.

stat1 = canSetBusParams(hnd, canFD_BITRATE_500K_80P, 0, 0, 0, 0, 0);
stat2 = canSetBusParamsFd(hnd, canFD_BITRATE_1M_80P, 0, 0, 0);

For more copmplex settings of CAN FD bitrates, use the Bit Timing Calculator for CAN FD available on the Kvaser web site.

CAN Driver Modes

Use canSetBusOutputControl() to set the bus driver mode. This is usually set to canDRIVER_NORMAL to obtain the standard push-pull type of driver. Some controllers also support canDRIVER_SILENT which makes the controller receive only, not transmit anything, not even ACK bits. This might be handy for e.g. when listening to a CAN bus without interfering.

stat = canSetBusOutputControl(canDRIVER_SILENT);

canDRIVER_NORMAL is the default, so your code doesn't have to set it explicitly.

Code Sample

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