To set the mask to 0xF0 and the code to 0x60:
stat = canAccept(handle1, 0x0F0L, canFILTER_SET_MASK_STD);
if (stat < 0) printf("canAccept failed, stat = %d\n", stat);
stat = canAccept(handle1, 0x060L, canFILTER_SET_CODE_STD);
if (stat < 0) printf("canAccept failed, stat = %d\n", stat);
This example takes an open channel off-bus and then takes it on-bus again, for what purpose it may serve.
int stat;
int handle;
.
.
.
stat = canBusOff(handle);
if (stat < 0) {
printf("canBusOff failed, status=%d\n", stat);
exit(1);
}
stat = canBusOn(handle);
if (stat < 0) {
printf("canBusOn failed, status=%d\n", stat);
exit(1);
}
This example opens a channel (see canOpen), sets the bit rate to 125
kbit/s using default bit timing parameters, and goes on bus.
int stat;
int handle;
.
.
.
handle = canOpen(&hw, NULL,0);
stat = canSetBusParams(handle, BAUD_125K, 0, 0, 0, 0, 0);
if (stat < 0) {
printf("canBusParams failed, status=%d\n", stat);
exit(1);
}
stat = canBusOn(handle);
if (stat < 0) {
printf("canBusOn failed, status=%d\n", stat);
exit(1);
}
This example closes the specified handle.
int stat = canClose(hnd);
if (stat < 0) {
printf("Close failed.");
}
Here's a procedure that prints an error message when the passed status code means faílure.
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);
}
}
This example opens channel 0 for exclusive usage by this application.
int hnd;
hnd = canOpenChannel(0, canWANT_EXCLUSIVE);
if (hnd < 0) {
printf("Open failed, stat=%d\n", hnd);
}
The following code fragment reads the next available CAN message, if any.
int handle;
long id;
char data[8];
unsigned int dlc, flags;
unsigned long timestamp;
stat = canRead(handle, &id, data, &dlc, &flags, ×tamp);
if (stat != canERR_NOMSG) {
printf("Failed, status == %d\n", stat);
}
This example opens a channel (see canOpen), sets the bit rate to 125 kbit/s using default bit timing parameters, and goes on bus.
If we had used this call instead:
int stat;
int handle;
.
.
.
handle = canOpen(&hw, NULL, 0);
stat = canSetBusParams(handle, BAUD_125K, 0, 0, 0, 0, 0);
if (stat < 0) {
printf("canSetBusParams failed, status=%d\n", stat);
exit(1);
}
stat = canBusOn(handle);
if (stat < 0) {
printf("canBusOn failed, status=%d\n", stat);
exit(1);
}
the result would have been:
stat = canSetBusParams(handle, 125000, 4, 3, 1, 1, 0);
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.
The functon 'callback' will be called when the status change
or an errorframe occurs on the bus.
void callback (canNotifyData *nd)
{
printf("Notify, tag = %p\n", nd -> tag);
switch (nd -> eventType) {
case canEVENT_STATUS:
printf("canEVENT_STATUS\n");
printf("busStatus: %d\n", nd -> info.status.busStatus);
printf("TXerror : %d\n", nd -> info.status.txErrorCounter);
printf("RXerror : %d\n", nd -> info.status.txErrorCounter);
printf("Time : %ld\n", nd -> info.status.time);
break;
case canEVENT_ERROR:
printf("canEVENT_ERROR\n");
break;
case canEVENT_TX:
printf("canEVENT_TX\n");
break;
case canEVENT_RX:
printf("canEVENT_RX\n");
break;
}
return;
}
canHnd = canOpen(....);
...
stat = canSetNotify(canHnd, callback, canNOTIFY_STATUS | canNOTIFY_ERROR, &canHnd);
The tag field in canNotifyData is a void pointer. This can be used as a reference to
some data associated with a specific channel/handle.
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.
int handle, stat;
char data[8];
stat = canWrite(handle, 1234, data, 8, canMSG_EXT);
if (stat < 0) {
printf("Failed, status == %d\n", stat);
}