#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#define ALARM_INTERVAL_IN_S (1)
#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)
{
static unsigned int last;
switch (sig) {
case SIGINT:
break;
case SIGALRM:
if (msgCounter != last) {
printf("rx : %u total: %u\n", msgCounter - last, msgCounter);
}
last = msgCounter;
alarm(ALARM_INTERVAL_IN_S);
break;
}
}
int main(int argc, char *argv[])
{
int channel;
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]);
}
}
printf("Reading messages on channel %d\n", channel);
signal(SIGALRM, sighand);
signal(SIGINT, sighand);
siginterrupt(SIGINT, 1);
if (hnd < 0) {
printf("canOpenChannel %d", channel);
check("", hnd);
return -1;
}
check("canSetBusParams", stat);
goto ErrorExit;
}
check("canSetBusParamsFd", stat);
goto ErrorExit;
}
check("canBusOn", stat);
goto ErrorExit;
}
alarm(ALARM_INTERVAL_IN_S);
do {
long id;
unsigned char msg[64];
unsigned int dlc;
unsigned int flag;
unsigned long time;
stat =
canReadWait(hnd, &
id, &msg, &dlc, &flag, &time, READ_WAIT_INFINITE);
char *can_std;
unsigned int i;
msgCounter++;
printf("(%u) ERROR FRAME flags:0x%x time:%lu\n", msgCounter, flag, time);
continue;
}
can_std = "FD+";
}
else {
can_std = "FD ";
}
}
else {
can_std = "STD";
}
printf("CH:%2d %s:%s:%2u:%08lx", channel,
can_std,
dlc,id);
printf("ESI ");
}
printf(" flags:0x%x time:%lu", flag, time);
for (i = 0; i < dlc; i++) {
unsigned char byte = msg[i];
if ((i % 16) == 0) {
printf("\n ");
}
printf(" %02x ", byte);
}
printf("\n");
}
else {
if (errno == 0) {
check("\ncanReadWait", stat);
}
else {
perror("\ncanReadWait error");
}
}
sighand(SIGALRM);
ErrorExit:
alarm(0);
check("canBusOff", stat);
check("canClose", stat);
check("canUnloadLibrary", stat);
return 0;
}