...
int main(int argc, char **argv)
{
MainWindow w;
w.zapisz(51);
w.show();
// return a.exec();
int fd;
int got;
int c;
char *pname;
extern char *optarg;
extern int optind;
int bit[16];
int nl;
int j;
for(nl=0;nl<16;nl++)
{bit[nl]=0;
}
canmsg_t rx[RXBUFFERSIZE];
char device[50] = "/dev/can0";
int messages_to_read = 1;
#ifdef USE_RT_SCHEDULING
int ret;
int max_rr_priority, min_rr_priority;
int max_ff_priority, min_ff_priority;
#else
int max_priority;
#endif
pname = *argv;
#ifdef USE_RT_SCHEDULING
max_rr_priority = sched_get_priority_max(SCHED_RR);
min_rr_priority = sched_get_priority_min(SCHED_RR);
max_ff_priority = sched_get_priority_max(SCHED_FIFO);
min_ff_priority = sched_get_priority_min(SCHED_FIFO);
#else
max_priority = 1;
#endif
/* parse command line */
while ((c = getopt(argc, argv, "B:b:dD:fhHlp:s:t:nPV")) != EOF) {
switch (c) {
case 'b':
baud = atoi(optarg);
break;
case 'B':
buffers = atoi(optarg);
case 's':
sleeptime = atoi(optarg);
break;
case 'd':
debug = true;
break;
case 'H':
horchmode = TRUE;
break;
case 'l': /* set listen only mode */
listenmode = TRUE;
break;
case 'f': /* set selfreception mode */
selfreception = TRUE;
break;
case 'n':
blocking = FALSE;
messages_to_read = RXBUFFERSIZE;
break;
case 't': /* set time stamp mode */
settsmode = TRUE;
tsmode = atoi(optarg);
break;
case 'P':
plugfest = TRUE;
break;
case 'p':
{
#ifdef USE_RT_SCHEDULING
struct sched_param mysched;
/* use real time round-robin or real time first-in first-out */
priority = atoi(optarg);
if (priority < min_rr_priority ) {
fprintf(stderr, "Priority < %d not allowed\n",
min_rr_priority);
}
if (priority > max_rr_priority) {
fprintf(stderr, "Priority > %d not allowed\n",
max_rr_priority);
}
mysched.sched_priority = priority;
/* sched_get_priority_max(SCHED_RR) - 1; */
ret = sched_setscheduler(0, SCHED_RR, &mysched);
if ( debug == true ) {
printf("sched_setscheduler() = %d\n", ret);
}
if(ret == -1) {
printf("No permission to change process priorities\n");
}
/* lock all currently and in future
allocated memory blocks in physical ram */
ret = mlockall(MCL_CURRENT | MCL_FUTURE);
if ( debug == true ) {
printf("mlockall() = %d\n", ret);
}
#endif
}
break;
case 'V':
printf("%s %s %s\n", argv[0], " V " VERSION ", " __DATE__ ,
#if defined(CANFD)
" (CAN FD data structure used)"
#else
""
#endif
);
printf(" can4linux.h header version %d.%d\n",
CAN4LINUXVERSION >> 8, CAN4LINUXVERSION & 0xFF);
exit(0);
break;
/* not used, device name is parameter */
case 'D':
if (
/* path ist starting with '.' or '/', use it as it is */
optarg[0] == '.'
||
optarg[0] == '/'
) {
snprintf(device, 50, "%s", optarg);
} else {
snprintf(device, 50, "/dev/%s", optarg);
}
break;
case 'h':
default: usage(pname); exit(0);
}
}
/* look for additional arguments given on the command line */
if ( argc - optind > 0 ) {
/* at least one additional argument, the device name is given */
char *darg = argv[optind];
if (
/* path ist starting with '.' or '/', use it as it is */
darg[0] == '.'
||
darg[0] == '/'
) {
snprintf(device, 50, "%s", darg);
} else {
snprintf(device, 50, "/dev/%s", darg);
}
} else {
snprintf(device, 50, "%s", STDDEV);
}
if (debug == true) {
printf("%s %s\n", argv[0], " V " VERSION ", " __DATE__ );
printf("(c) 2012-2014 H.J. Oertel\n");
printf(" using canmsg_t with %d bytes\n", (int)sizeof(canmsg_t));
#if defined(CANFD)
printf(" CAN FD data structure used\n");
#endif
printf(" open CAN device \"%s\" in %sblocking mode\n",
device, blocking ? "" : "non-");
#ifdef USE_RT_SCHEDULING
printf(" possible process RR priority is \"-p %d - %d\"\n",
min_rr_priority, max_rr_priority);
{
int pol;
struct sched_param sp;
pol = sched_getscheduler(0);
sched_getparam(0, &sp);
printf("using real time %s policy and priority %d\n",
(pol == SCHED_OTHER) ? "OTHER" :
(pol == SCHED_RR) ? "RR" :
(pol == SCHED_FIFO) ? "FIFO" :
#ifdef SCHED_BATCH
(pol == SCHED_BATCH) ? "BATCH" :
#endif
#ifdef SCHED_IDLE
(pol == SCHED_IDLE) ? "IDLE" :
#endif
"???", sp.sched_priority);
}
#endif /* USE_RT_SCHEDULING */
}
sleeptime *= 1000;
if(blocking == TRUE) {
/* fd = open(device, O_RDWR); */
fd = open(device, O_RDONLY);
} else {
fd = open(device, O_RDONLY | O_NONBLOCK);
}
if( fd < 0 ) {
fprintf(stderr,"Error opening CAN device %s\n", device);
perror("open");
exit(1);
}
if (baud > 0) {
if ( debug == TRUE ) {
printf("change Bit-Rate to %d Kbit/s\n", baud);
}
set_bitrate(fd, baud);
}
if (listenmode == TRUE) {
if ( debug == TRUE ) {
printf(" Change to Listen Only Mode\n");
}
set_lomode(fd);
}
if (selfreception == TRUE) {
if ( debug == TRUE ) {
printf(" Switch on selfreception of sent frames\n");
}
set_selfreception(fd, 1);
}
if (settsmode == TRUE) {
if ( debug == TRUE ) {
printf(" Change to Time Stamp Mode %d\n", tsmode);
}
if (tsmode == 4) {
/* mode 4 uses absulte time stamp of the driver,
* but displays a *ctime string */
set_tsmode(fd, 1);
} else {
set_tsmode(fd, tsmode);
}
}
/* all parameters are valid, before going into the receive loop
* set up the console stdin in raw mode to be able to change
* something at run time */
atexit(clean);
/* Installing Signal handler */
if (signal (SIGINT, clean_up) == SIG_IGN)
signal (SIGINT, SIG_IGN);
// set_terminal_mode();
/* printf("waiting for msg at %s\n", device); */
while(1) {
#if 0
int i;
/* check for anything at stdin */
ioctl(0, FIONREAD, &i);
while (i--) { /* input available */
int c;
/* get it */
c = getchar();
switch (c) {
case 'c':
cut_mark();
break;
case 'p':
plugfest = !plugfest;
break;
case 'h':
horchmode = !horchmode;
break;
case 'f':
selfreception = !selfreception;
break;
case 'q':
exit(0);
break;
default:
break;
}
}
#endif
got=read(fd, &rx[0], messages_to_read);
if( got > 0) {
int i;
int j;
char *format;
for(i = 0; i < got; i++) {
if(horchmode) {
if (tsmode == 4) {
time_t nowtime;
struct tm *nowtm;
char tmbuf[64];
nowtime = rx[i].timestamp.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
printf("%s.%06lu", tmbuf, rx[i].timestamp.tv_usec);
} else {
printf("%12lu.%06lu",
rx[i].timestamp.tv_sec,
rx[i].timestamp.tv_usec);
}
if (plugfest) {
display_participant(rx[i].id);
}
if(rx[i].id != 0xffffffff) {
printf(" %9u/0x%08x",
rx[i].id, rx[i].id);
} else {
printf("%20s", "error");
}
} else {
printf("Received with ret=%d: %12lu.%06lu id=%u/0x%08x\n",
got,
rx[i].timestamp.tv_sec,
rx[i].timestamp.tv_usec,
rx[i].id, rx[i].id);
printf("\tlen=%d", rx[i].length);
printf(" flags=0x%02x", rx[i].flags );
}
/* check if an CAN FD frame was received
* and change frame formatting rules for display */
if(rx[i].flags & MSG_CANFD ) {
format = " : %c%c%c [%2d]:";
} else {
format = " : %c%c%c (%2d): ramka danych";
}
printf(format,
/* extended/base */
(rx[i].flags & MSG_EXT) ?
(rx[i].flags & MSG_SELF) ? 'E' : 'e'
:
(rx[i].flags & MSG_SELF) ? 'B' : 'b',
/* remote/data */
(rx[i].flags & MSG_RTR) ? 'R' : 'D',
(rx[i].flags & MSG_RBRS) ? 'F' : ' ',
rx[i].length );
if( !(rx[i].flags & MSG_RTR) ) {
int length;
length = rx[i].length;
/* check canfd flag for length calculation */
if( rx[i].flags & MSG_CANFD ) {
if(length > 64) length = 64;
} else {
/* classic CAN */
/* restrict display to 8 byte */
if(length > 11) length = 11;
}
for(j = 0; j < length; j++) {
printf(" %02x", rx[i].data[j]);
}
}
...TBC