#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#define READ_WAIT_INFINITE (unsigned long)(-1)
static unsigned int msgCounter = 0;
{
char buf[50];
buf[0] = '\0';
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
static void printUsageAndExit(char *prgName)
{
printf("Usage: '%s <channel>'\n", prgName);
exit(1);
}
static void sighand(int sig, siginfo_t *info, void *ucontext)
{
(void) sig;
(void) info;
(void) ucontext;
}
static const char* busStatToStr(const unsigned long flag)
{
switch (flag) {
return "canSTAT_ERROR_PASSIVE";
return "canSTAT_BUS_OFF";
return "canSTAT_ERROR_WARNING";
return "canSTAT_ERROR_ACTIVE";
default:
return "";
}
}
{
printf(
"CAN Status Event: %s\n", busStatToStr(data->
info.
status.busStatus));
break;
printf("CAN Error Event\n");
break;
printf("CAN Tx Event\n");
break;
printf("CAN Rx Event\n");
break;
}
return;
}
int main(int argc, char *argv[])
{
int channel;
struct sigaction sigact;
if (argc != 2) {
printUsageAndExit(argv[0]);
}
{
char *endPtr = NULL;
errno = 0;
channel = strtol(argv[1], &endPtr, 10);
if ( (errno != 0) || ((channel == 0) && (endPtr == argv[1])) ) {
printUsageAndExit(argv[0]);
}
}
sigact.sa_flags = SA_SIGINFO;
sigemptyset(&sigact.sa_mask);
sigact.sa_sigaction = sighand;
if (sigaction(SIGINT, &sigact, NULL) != 0) {
perror("sigaction SIGINT failed");
return -1;
}
printf("Reading CAN messages on channel %d\n", channel);
if (hnd < 0) {
printf("canOpenChannel %d", channel);
check("", hnd);
return -1;
}
check("canSetNotify", stat);
check("canSetBusParams", stat);
goto ErrorExit;
}
check("canBusOn", stat);
goto ErrorExit;
}
do {
long id;
unsigned char msg[8];
unsigned int dlc;
unsigned int flag;
unsigned long time;
stat =
canReadWait(hnd, &
id, &msg, &dlc, &flag, &time, READ_WAIT_INFINITE);
msgCounter++;
printf("(%u) ERROR FRAME", msgCounter);
}
else {
unsigned j;
printf("(%u) id:%ld dlc:%u data: ", msgCounter, id, dlc);
if (dlc > 8) {
dlc = 8;
}
for (j = 0; j < dlc; j++) {
printf("%2.2x ", msg[j]);
}
}
printf(" flags:0x%x time:%lu\n", flag, time);
}
else {
if (errno == 0) {
check("\ncanReadWait", stat);
}
else {
perror("\ncanReadWait error");
}
}
ErrorExit:
check("canBusOff", stat);
usleep(50*1000);
check("canClose", stat);
check("canUnloadLibrary", stat);
return 0;
}