uread.c
Go to the documentation of this file.
00001 #include <fcntl.h>          /* for open */
00002 #include <stdio.h>
00003 #include <unistd.h>         /* for read, close */
00004 #include <sys/ioctl.h>      /* for ioctl */
00005 #include <sys/types.h>      /* for open */
00006 #include <sys/stat.h>       /* for open */
00007 
00008 #include <signal.h>
00009 
00010 #include "driver/urbtc.h"          /* Linux specific part */
00011 #include "driver/urobotc.h"        /* OS independent part */
00012 
00013 #undef __BIG_ENDIAN
00014 
00015 void exit_program();
00016 
00017 int quit_flag = 1;
00018 int fd;
00019 
00020 int main(int argc, char **argv)
00021 {
00022   struct uin buf;
00023   int i;
00024   char *dev = "/dev/urbtc0";
00025   
00026   signal(SIGINT, exit_program);
00027 
00028   if (argc>1)
00029         dev = argv[1];
00030 
00031   if ((fd = open(dev, O_RDWR)) == -1) {
00032     fprintf(stderr, "%s: Open error\n", dev);
00033     exit(1);
00034   }
00035 
00036   if (ioctl(fd, URBTC_CONTINUOUS_READ) < 0){
00037     fprintf(stderr, "ioctl: URBTC_CONTINUOUS_READ error\n");
00038     exit(1);
00039   }
00040 
00041   if (ioctl(fd, URBTC_BUFREAD) < 0){
00042     fprintf(stderr, "ioctl: URBTC_CONTINUOUS_READ error\n");
00043     exit(1);
00044   }
00045 
00046   while(quit_flag) {
00047     if ((i = read(fd, &buf, sizeof(buf))) != sizeof(buf)) {
00048       fprintf(stderr, "Warning: read size mismatch (%d!=%d).\n", i, sizeof(buf));
00049       continue;
00050     }
00051     for (i=0; i<4; i++) {
00052 #if __BYTE_ORDER == __BIG_ENDIAN
00053       buf.ad[i] = (0xff & buf.ad[i])<<8 | (0xff00 & buf.ad[i])>>8;
00054 #endif
00055       buf.ad[i] = buf.ad[i] >> 5;
00056     }
00057 
00058 #if __BYTE_ORDER == __BIG_ENDIAN
00059     buf.time = (0xff & buf.time)<<8 | (0xff00 & buf.time)>>8;
00060 #endif
00061 
00062     printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %x %x %d %d %x\n",
00063            buf.time, buf.ad[0], buf.ad[1], buf.ad[2], buf.ad[3],
00064            buf.ct[0], buf.ct[1], buf.ct[2], buf.ct[3],
00065            buf.da[0], buf.da[1], buf.da[2], buf.da[3],
00066            buf.din, buf.dout, buf.intmax, buf.interval, buf.magicno);
00067   }
00068 
00069   close(fd);
00070 
00071   return 0;
00072 }
00073 
00074 void exit_program(sig, code, scp, addr)
00075 int sig;
00076 int code;
00077 struct sigcontext *scp;
00078 char *addr;
00079 {
00080   quit_flag = 0;
00081   fprintf(stderr, "kill signal is received\n");
00082 }


cirkit_unit03_driver
Author(s): CIR-KIT
autogenerated on Thu Jun 6 2019 21:08:20