I've updated the manual in the section configuration and
[comedilib.git] / testing / select.c
1
2 #include <stdio.h>
3 #include <comedilib.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <sys/ioctl.h>
7 #include <errno.h>
8 #include <getopt.h>
9 #include <ctype.h>
10 #include <math.h>
11 #include <sys/time.h>
12 #include <string.h>
13 #include "comedi_test.h"
14
15
16 #define BUFSZ 10000
17
18 int test_read_select(void)
19 {
20         comedi_cmd cmd;
21         char buf[BUFSZ];
22         unsigned int chanlist[1];
23         int go;
24         int total=0;
25         int ret;
26         int chunks=0;
27         int length=100000;
28         fd_set rdset;
29         struct timeval timeout;
30         unsigned int flags;
31
32         flags = comedi_get_subdevice_flags(device,subdevice);
33         if(!(flags&SDF_CMD) || (comedi_get_read_subdevice(device)!=subdevice)){
34                 printf("not applicable\n");
35                 return 0;
36         }
37
38         if(comedi_get_cmd_generic_timed(device, subdevice, &cmd, 1, 1)<0){
39                 printf("E: comedi_get_cmd_generic_timed failed\n");
40                 return 0;
41         }
42
43         if(realtime)cmd.flags |= TRIG_RT;
44
45         cmd.chanlist = chanlist;
46         cmd.scan_end_arg = 1;
47         cmd.stop_arg = length;
48         cmd.chanlist_len = 1;
49         chanlist[0] = CR_PACK(0,0,0);
50
51         //fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK);
52
53         comedi_command(device,&cmd);
54
55         go=1;
56         while(go){
57                 FD_ZERO(&rdset);
58                 FD_SET(comedi_fileno(device),&rdset);
59                 timeout.tv_sec = 0;
60                 timeout.tv_usec = 10000;
61                 ret = select(comedi_fileno(device)+1,&rdset,NULL,NULL,&timeout);
62                 if(ret<0){
63                         perror("select");
64                 }else if(ret==0){
65                         if(verbose)printf("timeout\n");
66                 }else{
67                         ret = read(comedi_fileno(device),buf,BUFSZ);
68                         if(verbose)printf("read==%d\n",ret);
69                         if(ret<0){
70                                 if(errno==EAGAIN){
71                                         printf("E: got EAGAIN!\n");
72                                 }else{
73                                         go = 0;
74                                         perror("read");
75                                 }
76                         }else if(ret==0){
77                                 go = 0;
78                         }else{
79                                 total += ret;
80                                 chunks++;
81                         }
82                 }
83         }
84
85         return 0;
86 }
87