]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - lincan/utils/readburst.c
The original version of Arnaud Westenberg Linux CAN-bus driver
[lincan.git] / lincan / utils / readburst.c
diff --git a/lincan/utils/readburst.c b/lincan/utils/readburst.c
new file mode 100644 (file)
index 0000000..139e558
--- /dev/null
@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <signal.h>
+
+#include "../include/can.h"
+
+int fd;
+
+/*--- handler on SIGINT signal : the program quit with CTL-C ---*/
+void sortie(int sig)
+       {
+       close(fd);
+       printf("Terminated by user\n");
+       exit(0);
+       }
+
+int main(void)
+       {
+       int n,ret;
+       unsigned long i=0;
+       struct canmsg_t readmsg={0,0,5,0,0,{0,}};
+       struct sigaction act;
+
+       /*------- register handler on SIGINT signal -------*/
+       act.sa_handler=sortie;
+       sigemptyset(&act.sa_mask);
+       act.sa_flags=0;
+       sigaction(SIGINT,&act,0);
+       /*---------------------------------------*/     
+
+       if ((fd=open("/dev/can0",O_RDWR)) < 0) 
+               {
+               perror("open");
+               printf("Error opening /dev/can0\n");
+               exit(1);        
+               }
+
+       while (1)
+               {
+               readmsg.flags=0;
+               readmsg.cob=0;
+               readmsg.timestamp=0;
+               ret=read(fd,&readmsg,sizeof(struct canmsg_t));
+               if(ret <0) 
+                       {
+                       printf("Error reading message\n");
+                       }
+               else 
+                       {
+                       printf("Received message #%lu: id=%lX dlc=%u",i,readmsg.id,readmsg.length);
+                       for(n=0 ; n<readmsg.length ; n++)
+                               printf(" %.2X",(unsigned char)readmsg.data[n]);
+                       printf("\n");
+                       i++;
+                       }
+               }
+       return 0;
+}