Send and Receive CAN Frames
Sending and receiving CAN frames can be implemented in different ways. The simplest way is to disable all upper layers and use the CAN frames directly. When using an operating system (e.g. μC/OS), the RX and TX communication could be performed within two tasks.
Example: The CAN frame reception shall be done within a single RX-Task. Any received CAN frame shall be send back with the fixed CAN Identifier: 0x100.
Source code:
#include can_frm.h
#include can_bus.h
void Echo_Task(void *argp)
{
CANFRM frm; /*-1-*/
CPU_INT
16U timeout;
/* Task initialisation */
timeout = 0;
CanBusIoCtl(0, CANBUS_SET_RX_TIMEOUT
,
(void *)&timeout ); /*-2-*/
/* Endless task loop */
while (1) {
CanBusRead(0, (void *)&frm, sizeof(CANFRM)); /*-3-*/
frm.Identifier = 0x100L; /*-4-*/
CanBusWrite(0, (void *)&frm, sizeof(CANFRM)); /*-5-*/
}
}
Description
CanBusRead()
will wait for a CAN frame forever. Otherwise set a timeout and check returned error code of -3- if a timeout has occurred.End