canAccept / canSetAcceptanceFilter Example
To set the mask to 0xF0 and the code to 0x60:
if (stat < 0) printf("canAccept failed, stat = %d\n", stat);
if (stat < 0) printf("canAccept failed, stat = %d\n", stat);
or, alternatively,
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.
.
.
.
if (stat < 0) {
printf("canBusOff failed, status=%d\n", stat);
exit(1);
}
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.
.
.
.
if (stat < 0) {
printf("canBusParams failed, status=%d\n", stat);
exit(1);
}
if (stat < 0) {
printf("canBusOn failed, status=%d\n", stat);
exit(1);
}
canClose Example
This example closes the specified handle.
if (stat < 0) {
printf("Close failed.");
}
canGetErrorText Example
Here's a procedure that prints an error message when the passed status code means failure.
{
char buf[100];
buf[0] = '\0';
printf("Failed, stat=%d (%s)\n", (int)stat, buf);
exit(1);
}
}
canInitializeLibrary Example
This example initializes the library.
canOpenChannel Example
This example opens channel 0 for exclusive usage by this application.
if (hnd < 0) {
printf("Open failed, stat=%d\n", hnd);
}
canRead Example
The following code fragment reads the next available CAN message, if any.
long id;
unsigned char data[8];
unsigned int dlc, flags;
unsigned long timestamp;
stat =
canRead(hnd, &
id, data, &dlc, &flags, ×tamp);
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.
.
.
.
if (stat < 0) {
printf("canSetBusParams failed, status=%d\n", stat);
exit(1);
}
if (stat < 0) {
printf("canBusOn failed, status=%d\n", stat);
exit(1);
}
If we had used this call instead:
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
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
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.
if (stat < 0) ... ;
if (chanCount < 0 || chanCount > 300) {
printf("ChannelCount = %d but I don't believe it.\n", chanCount);
exit(1);
} else {
printf("%d channels.\n", chanCount);
}
for (i=0; i < chanCount; i++) {
printf("== Channel %d ===============================\n", i);
if (stat < 0) ...;
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.
unsigned char data[8];
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}
canProbeVersion Example
canWaitForEvent Example
while (!ready) {
if (canWaitForEvent(hnd, 100) ==
canOK) {
}
}
}
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
int i;
...
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}
for (i = 0; i < NUMBER_OF_CHANNELS; i++) {
if (stat < 0) {
printf("Failed to add handle %u to tDomain, ", i);
printf("status == %d\n", stat);
}
}
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}
printf("Consider connecting the Kvaser MagiSync&tm; enabled ");
printf("interfaces through the same USB root hub!");
}
printf("All my Kvaser MagiSync&tm; enabled interfaces are ");
printf("connected through the same USB root hub and hence ");
printf("perfectly synchronized!");
}
printf("All handles have the Kvaser MagiSync&tm; feature enabled!");
}
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}
...
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.
...
if (stat < 0) {
printf("Load failed (0), status == %d\n", stat);
}
if (stat < 0) {
printf("Load on device failed (3), status == %d\n", stat);
}
if (stat < 0) {
printf("Start failed (0), status == %d\n", stat);
}
if (stat < 0) {
printf("Start failed (3), status == %d\n", stat);
}
...
if (stat < 0) {
printf("Stop (3) failed, status == %d\n", stat);
if (stat < 0) {
printf("Forced stop failed (3), status == %d\n", stat);
}
Sleep(100);
}
...
if (stat < 0) {
printf("Load failed (3), status == %d\n", stat);
}
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.
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};
...
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);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
} else {
printf("int_val = %d\n", int_val);
}
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
}
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);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
if (stat < 0) {
printf("Failed, stat == %d\n", stat);
}
printf("float_val = %f\n", float_val);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
}
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);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
printf("buffer_val = %s\n", buffer_val);
if (stat < 0) {
printf("Failed, stat = %d\n", stat);
}
}
kvScriptSendEvent Example
The following code fragment shows how to send a key to a script.
int slot_number;
...
for (slot_number = 0; slot_number < 4; slot_number++) {
}
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.
- See also
- canSetBusParams()
-
canSetBusParamsC200()
-
canGetBusParams()