Code snippets

# canAccept / canSetAcceptanceFilter Example

To set the mask to 0xF0 and the code to 0x60:

stat = canAccept(hnd1, 0x0F0L, canFILTER_SET_MASK_STD);
if (stat < 0) printf("canAccept failed, stat = %d\n", stat);
stat = canAccept(hnd1, 0x060L, canFILTER_SET_CODE_STD);
if (stat < 0) printf("canAccept failed, stat = %d\n", stat);

or, alternatively,

stat = canSetAcceptanceFilter(hnd1, 0x060, 0x0F0, FALSE);
if (stat < 0) printf("canSetAcceptanceFilter failed, stat = %d\n", stat);

Result
Messages having a standard identifier with bit7-bit4 in the identifier equal to 0110 (binary) will pass through. Other messages with standard identifiers will be rejected.

# canBusOff Example

This example takes an open channel off-bus and then takes it on-bus again, for what purpose it may serve.

canStatus stat;
CanHandle hnd;
.
.
.
stat = canBusOff(hnd);
if (stat < 0) {
printf("canBusOff failed, status=%d\n", stat);
exit(1);
}
stat = canBusOn(hnd);
if (stat < 0) {
printf("canBusOn failed, status=%d\n", stat);
exit(1);
}

# canBusOn Example

This example opens a channel (see canOpenChannel()), sets the bit rate to 125 kbit/s using default bit timing parameters, and goes on bus.

canStatus stat;
CanHandle hnd;
.
.
.
hnd = canOpenChannel(0, 0);
stat = canSetBusParams(hnd, canBITRATE_125K, 0, 0, 0, 0, 0);
if (stat < 0) {
printf("canBusParams failed, status=%d\n", stat);
exit(1);
}
stat = canBusOn(hnd);
if (stat < 0) {
printf("canBusOn failed, status=%d\n", stat);
exit(1);
}

# canClose Example

This example closes the specified handle.

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

# canGetErrorText Example

Here's a procedure that prints an error message when the passed status code means failure.

void Check(canStatus stat)
{
char buf[100];
if (stat != canOK) {
buf[0] = '\0';
canGetErrorText(stat, buf, sizeof(buf));
printf("Failed, stat=%d (%s)\n", (int)stat, buf);
exit(1);
}
}

# canInitializeLibrary Example

This example initializes the library.

canInitializeLibrary();

# canOpenChannel Example

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

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

# canRead Example

The following code fragment reads the next available CAN message, if any.

CanHandle hnd;
long id;
unsigned char data[8];
unsigned int dlc, flags;
unsigned long timestamp;
stat = canRead(hnd, &id, data, &dlc, &flags, &timestamp);
if (stat != canERR_NOMSG) {
printf("Failed, status == %d\n", stat);
}

# canSetBusParams Example

This example opens a channel (see canOpenChannel()), sets the bit rate to 125 kbit/s using default bit timing parameters, and goes on bus.

canStatus stat;
CanHandle hnd;
.
.
.
hnd = canOpenChannel(0, canOPEN_EXCLUSIVE);
stat = canSetBusParams(hnd, canBITRATE_125K, 0, 0, 0, 0, 0);
if (stat < 0) {
printf("canSetBusParams failed, status=%d\n", stat);
exit(1);
}
stat = canBusOn(hnd);
if (stat < 0) {
printf("canBusOn failed, status=%d\n", stat);
exit(1);
}

If we had used this call instead:

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

the result would have been:

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.

# canSetNotify 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.

# canGetChannelData Example

This code snippet enumerates all CAN channels in the computer and prints the type of the CAN card they're on.

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);
}
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);
}

# canWrite Example

The following code fragment sends a CAN message on an already open channel. The CAN message will have identifier 1234 (extended) and DLC = 8. The contents of the data bytes will be whatever the data array happens to contain.

canStatus stat;
CanHandle hnd;
unsigned char data[8];
stat = canWrite(hnd, 1234, data, 8, canMSG_EXT);
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}

# canProbeVersion Example

# canWaitForEvent Example

while (!ready) {
if (canWaitForEvent(hnd, 100) == canOK) {
while (canRead(hnd, ...) == canOK) {
// Handle incoming messages here
}
canReadStatus(hnd, &can_status);
}
}

# kvTimeDomainXxx Example

The following code fragment resets the time for some already opened channels. It also collects and interprets some data from a domain.

#define NUMBER_OF_CHANNELS 3
CanHandle hnd[NUMBER_OF_CHANNELS];
canStatus stat;
int i;
kvTimeDomain tDomain;
kvTimeDomainData tData;
...
// Create a time domain for my handles.
stat = kvTimeDomainCreate(&tDomain);
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}
// Add the handles to the domain.
for (i = 0; i < NUMBER_OF_CHANNELS; i++) {
stat = kvTimeDomainAddHandle(tDomain, hnd[i]);
if (stat < 0) {
printf("Failed to add handle %u to tDomain, ", i);
printf("status == %d\n", stat);
}
}
// Request some data from the domain and try to interpret it.
stat = kvTimeDomainGetData(tDomain, &tData; sizeof(tData));
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}
if (tData.nMagiSyncGroups > 1) {
printf("Consider connecting the Kvaser MagiSync&tm; enabled ");
printf("interfaces through the same USB root hub!");
}
if (tData.nMagiSyncGroups == 1) {
printf("All my Kvaser MagiSync&tm; enabled interfaces are ");
printf("connected through the same USB root hub and hence ");
printf("perfectly synchronized!");
}
if (tData.nMagiSyncedMembers == NUMBER_OF_CHANNELS) {
printf("All handles have the Kvaser MagiSync&tm; feature enabled!");
}
// Reset the time on all handles in tDomain.
stat = kvTimeDomainResetTime(tDomain);
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}
...
// Final cleanup
stat = kvTimeDomainDelete(tDomain);
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}
else {
printf("All resources used by tDomain and its members ");
printf("returned to system");
}

# kvScriptStart Example

The following code fragment shows how to load scripts from PC and from SD card, start scripts and how to stop running scripts.

canStatus stat;
CanHandle hnd;
...
// Load a script that is stored locally (on SD) into script slot 0
stat = kvScriptLoadFileOnDevice(hnd, 0, "script0.txe");
if (stat < 0) {
printf("Load failed (0), status == %d\n", stat);
}
// Load another script stored on the host (PC) into script slot 3
stat = kvScriptLoadFile(hnd, 3, "script3.txe");
if (stat < 0) {
printf("Load on device failed (3), status == %d\n", stat);
}
// Start script in slot 0
stat = kvScriptStart(hnd, 0);
if (stat < 0) {
printf("Start failed (0), status == %d\n", stat);
}
// Start script in slot 3
stat = kvScriptStart(hnd, 3);
if (stat < 0) {
printf("Start failed (3), status == %d\n", stat);
}
...
// Try to stop script in slot 3
stat = kvScriptStop(hnd, 3, kvSCRIPT_STOP_NORMAL);
if (stat < 0) {
printf("Stop (3) failed, status == %d\n", stat);
// For some reason this failed (script is busy?) so we request it to stop.
// Note that this is an request and that the script might still be running
// when this call returns.
stat = kvScriptStop(hnd, 3, kvSCRIPT_STOP_FORCED);
if (stat < 0) {
printf("Forced stop failed (3), status == %d\n", stat);
}
Sleep(100); // make sure it has stopped.
}
...
// Load a script that is stored locally (on SD) into script slot 3
stat = kvScriptLoadFileOnDevice(hnd, 3, "script3x.txe");
if (stat < 0) {
printf("Load failed (3), status == %d\n", stat);
}
// Start script in slot 3
stat = kvScriptStart(hnd, 3);
if (stat < 0) {
printf("Start failed (3), status == %d\n", stat);
}

# kvScriptEnvvarXxx Example

The following code fragment shows how to use environment variables, envvars for communication between a PC and a script.

CanHandle hnd;
kvEnvHandle env_hnd;
canStatus stat;
int envvar_size;
int envvar_type;
int int_val = 0;
float float_val = 0;
char buffer_val[10] = {0,0,0,0,0,0,0,0,0,0};
...
// Open an int envvar called "ev1_int" and set its value to 55
env_hnd = kvScriptEnvvarOpen(hnd, "ev1_int", &envvar_type, &envvar_size);
if (env_hnd < 0) {
printf("Open failed, env_hnd = %d\n", env_hnd);
} else {
printf("envvar_size = %d, envvar_type = %d\n", envvar_size, envvar_type);
stat = kvScriptEnvvarSetInt(env_hnd, 55);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
// Read back the value of "ev1_int" and close.
stat = kvScriptEnvvarGetInt(env_hnd, &int_val);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
} else {
printf("int_val = %d\n", int_val);
}
stat = kvScriptEnvvarClose(env_hnd);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
}
// Open a float envvar called "ev1_float" and set its value to 55.55
env_hnd = kvScriptEnvvarOpen(hnd, "ev1_float", &envvar_type, &envvar_size);
if (env_hnd < 0) {
printf("Open failed, env_hnd = %d\n", env_hnd);
} else {
printf("envvar_size = %d, envvar_type = %d\n", envvar_size, envvar_type);
stat = kvScriptEnvvarSetFloat(env_hnd, (float)55.55);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
// Read back the value of "ev1_int" and close.
stat = kvScriptEnvvarGetFloat(env_hnd, &float_val);
if (stat < 0) {
printf("Failed, stat == %d\n", stat);
}
printf("float_val = %f\n", float_val);
stat = kvScriptEnvvarClose(env_hnd);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
}
// Open a buffer envvar called "ev1_buffer" and set its value to ""hello1234""
env_hnd = kvScriptEnvvarOpen(hnd, "ev1_float", &envvar_type, &envvar_size);
if (env_hnd < 0) {
printf("Open failed, env_hnd = %d\n", env_hnd);
} else {
printf("envvar_size = %d, envvar_type = %d\n", envvar_size, envvar_type);
// Set value, starting at index 0 in buffer and length 10
stat = kvScriptEnvvarSetData(env_hnd, "hello1234", 0, 10);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
// Read back the value of "ev1_buffer" and close.
stat = kvScriptEnvvarGetData(env_hnd, buffer_val, 0, 10);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
printf("buffer_val = %s\n", buffer_val);
stat = kvScriptEnvvarClose(env_hnd);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
}

# kvScriptSendEvent Example

The following code fragment shows how to send a key to a script.

CanHandle hnd;
canStatus stat;
int slot_number;
...
// Send key 'a' to scripts running in slot 0...3
for (slot_number = 0; slot_number < 4; slot_number++) {
stat = kvScriptSendEvent(hnd, slot_number, kvEVENT_TYPE_KEY, 'a', 0);
}

# Bit Rate Examples

To calculate the bit timing parameters, you can use the bit timing calculator that is included with CANlib SDK. Look in the BIN directory.

// 500 kbit/s, using library-supplied default values
stat = canSetBusParams(hnd, canBITRATE_500K, 0, 0, 0, 0, 0);

// 125 kbit/s, tseg1=4, tseg=3, sjw=2, one sample
// tseg1=4 means 4+1=5 quanta will precede the sampling point.
// tseg2=3 means 3 quanta will follow the sampling point.
// The sampling point will occur when 5/(5+3) = 62,5% of the bit has passed.
stat = canSetBusParams(hnd, 125000, 4, 3, 2, 1, 0);

// 83333 kbit/s, sampling point 75%, sjw=2, one sample
stat = canSetBusParams(hnd, 83333, 5, 2, 2, 1, 0);

// 83333 kbit/s, sampling point 75%, sjw=2, one sample
stat = canSetBusParamsC200(hnd, 0x4b, 0x14);

See also
  canSetBusParams()
  canSetBusParamsC200()
  canGetBusParams()