]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/readburst.c
The first enhanced version of Linux CAN-bus driver for OCERA project
[lincan.git] / lincan / utils / readburst.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/time.h>
8 #include <signal.h>
9
10 #include "../include/can.h"
11
12 int fd;
13
14 int can_fd_wait(int fd, int wait_sec)
15 {
16   int ret;
17   struct timeval timeout;
18   fd_set set;
19
20   FD_ZERO (&set);
21   FD_SET (fd, &set);
22   timeout.tv_sec = wait_sec;
23   timeout.tv_usec = 0;
24   while ((ret=select(FD_SETSIZE,&set, NULL, NULL,&timeout))==-1
25           &&errno==-EINTR);
26   return ret;
27 }
28
29
30 /*--- handler on SIGINT signal : the program quit with CTL-C ---*/
31 void sortie(int sig)
32         {
33         close(fd);
34         printf("Terminated by user\n");
35         exit(0);
36         }
37
38 int main(void)
39         {
40         int n,ret;
41         unsigned long i=0;
42         struct canmsg_t readmsg={0,0,5,0,0,{0,}};
43         struct sigaction act;
44
45         /*------- register handler on SIGINT signal -------*/
46         act.sa_handler=sortie;
47         sigemptyset(&act.sa_mask);
48         act.sa_flags=0;
49         sigaction(SIGINT,&act,0);
50         /*---------------------------------------*/     
51
52         if ((fd=open("/dev/can0",O_RDWR)) < 0) 
53                 {
54                 perror("open");
55                 printf("Error opening /dev/can0\n");
56                 exit(1);        
57                 }
58
59         while (1)
60                 {
61                 readmsg.flags=0;
62                 readmsg.cob=0;
63                 readmsg.timestamp=0;
64             #if 1
65                 ret=can_fd_wait(fd, 5);
66                 printf("can_fd_wait returnet %d\n",ret);
67             #endif
68                 ret=read(fd,&readmsg,sizeof(struct canmsg_t));
69                 if(ret <0) 
70                         {
71                         printf("Error reading message\n");
72                         }
73                 else if(ret == 0)
74                         {
75                         printf("No message arrived\n");
76                         }
77                 else 
78                         {
79                         printf("Received message #%lu: id=%lX dlc=%u",i,readmsg.id,readmsg.length);
80                         for(n=0 ; n<readmsg.length ; n++)
81                                 printf(" %.2X",(unsigned char)readmsg.data[n]);
82                         printf("\n");
83                         i++;
84                         }
85                 }
86         return 0;
87 }