]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/readburst.c
139e55891af09602ef7d9136c8f5ea5d0566f2d7
[lincan.git] / lincan / utils / readburst.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <signal.h>
7
8 #include "../include/can.h"
9
10 int fd;
11
12 /*--- handler on SIGINT signal : the program quit with CTL-C ---*/
13 void sortie(int sig)
14         {
15         close(fd);
16         printf("Terminated by user\n");
17         exit(0);
18         }
19
20 int main(void)
21         {
22         int n,ret;
23         unsigned long i=0;
24         struct canmsg_t readmsg={0,0,5,0,0,{0,}};
25         struct sigaction act;
26
27         /*------- register handler on SIGINT signal -------*/
28         act.sa_handler=sortie;
29         sigemptyset(&act.sa_mask);
30         act.sa_flags=0;
31         sigaction(SIGINT,&act,0);
32         /*---------------------------------------*/     
33
34         if ((fd=open("/dev/can0",O_RDWR)) < 0) 
35                 {
36                 perror("open");
37                 printf("Error opening /dev/can0\n");
38                 exit(1);        
39                 }
40
41         while (1)
42                 {
43                 readmsg.flags=0;
44                 readmsg.cob=0;
45                 readmsg.timestamp=0;
46                 ret=read(fd,&readmsg,sizeof(struct canmsg_t));
47                 if(ret <0) 
48                         {
49                         printf("Error reading message\n");
50                         }
51                 else 
52                         {
53                         printf("Received message #%lu: id=%lX dlc=%u",i,readmsg.id,readmsg.length);
54                         for(n=0 ; n<readmsg.length ; n++)
55                                 printf(" %.2X",(unsigned char)readmsg.data[n]);
56                         printf("\n");
57                         i++;
58                         }
59                 }
60         return 0;
61 }