#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#define ALARM_INTERVAL_IN_S (1)
#define WRITE_WAIT_INFINITE (unsigned long)(-1)
static unsigned int msgCounter = 0;
static int willExit = 0;
{
char buf[50];
buf[0] = '\0';
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
static void sighand(int sig)
{
static unsigned int last;
switch (sig) {
case SIGINT:
willExit = 1;
break;
case SIGALRM:
if (msgCounter - last) {
printf("msg/s = %d, total=%u\n",
(msgCounter - last) / ALARM_INTERVAL_IN_S, msgCounter);
}
last = msgCounter;
alarm(ALARM_INTERVAL_IN_S);
break;
}
}
static void printUsageAndExit(char *prgName)
{
printf("Usage: '%s <channel>'\n", prgName);
exit(1);
}
int main(int argc, char *argv[])
{
char msg[8] = "Kvaser!";
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("Writing messages on channel %d\n", channel);
signal(SIGALRM, sighand);
signal(SIGINT, sighand);
alarm(ALARM_INTERVAL_IN_S);
siginterrupt(SIGINT, 1);
if (hnd < 0) {
printf("canOpenChannel %d", channel);
check("", hnd);
return -1;
}
check("canSetBusParams", stat);
goto ErrorExit;
}
check("canBusOn", stat);
goto ErrorExit;
}
while ((stat ==
canOK) && !willExit) {
long id = channel + 100;
if (errno == 0) {
check("\ncanWriteWait", stat);
}
else {
perror("\ncanWriteWait error");
}
msgCounter++;
}
}
sighand(SIGALRM);
ErrorExit:
alarm(0);
check("canBusOff", stat);
check("canClose", stat);
check("canUnloadLibrary", stat);
return 0;
}