2 * selet.c - Example of using select() with Comedi
5 * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
7 * This file may be freely modified, distributed, and combined with
8 * other software, as long as proper attribution is given in the
13 * An example for using select() with asynchronous input. This
14 * example requires an asynchronous input subdevice that can
15 * handle TRIG_TIMER as a scan_begin_src.
19 #include <comedilib.h>
36 const int n_chans = 1;
37 const int n_scans = 10;
39 unsigned int chanlist[4];
43 void prepare_cmd(comedi_t *dev,comedi_cmd *cmd, int subdevice);
44 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
46 int main(int argc, char *argv[])
50 struct parsed_options options;
52 init_parsed_options(&options);
53 parse_options(&options, argc, argv);
55 device = comedi_open(options.filename);
57 perror(options.filename);
61 fcntl(comedi_fileno(device), F_SETFL, O_NONBLOCK);
63 for(i = 0; i < n_chans; i++){
64 chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
67 prepare_cmd(device, &cmd, options.subdevice);
74 void do_cmd(comedi_t *dev,comedi_cmd *cmd)
80 struct timeval timeout;
82 ret=comedi_command_test(dev,cmd);
84 printf("test ret=%d\n",ret);
86 printf("errno=%d\n",errno);
87 comedi_perror("comedi_command_test");
93 ret=comedi_command_test(dev,cmd);
95 printf("test ret=%d\n",ret);
97 printf("errno=%d\n",errno);
98 comedi_perror("comedi_command_test");
102 dump_cmd(stdout,cmd);
104 ret=comedi_command(dev,cmd);
106 printf("ret=%d\n",ret);
108 printf("errno=%d\n",errno);
109 comedi_perror("comedi_command");
116 FD_SET(comedi_fileno(device),&rdset);
118 timeout.tv_usec = 50000;
119 ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
120 printf("select returned %d\n",ret);
126 }else if(FD_ISSET(comedi_fileno(device),&rdset)){
127 /* comedi file descriptor became ready */
128 printf("comedi file descriptor ready\n");
129 ret=read(comedi_fileno(dev),buf,sizeof(buf));
130 printf("read returned %d\n",ret);
141 //printf("read %d %d\n",ret,total);
142 for(i=0;i<ret/sizeof(sampl_t);i++){
143 printf("%d\n",buf[i]);
147 /* unknown file descriptor became ready */
148 printf("unknown file descriptor ready\n");
154 * This part of the demo measures channels 1, 2, 3, 4 at a rate of
155 * 10 khz, with the inter-sample time at 10 us (100 khz). The number
156 * of scans measured is 10. This is analogous to the old mode2
159 void prepare_cmd(comedi_t *dev, comedi_cmd *cmd, int subdevice)
161 memset(cmd,0,sizeof(*cmd));
163 /* the subdevice that the command is sent to */
164 cmd->subdev = subdevice;
167 cmd->flags = TRIG_WAKE_EOS;
170 /* each event requires a trigger, which is specified
171 by a source and an argument. For example, to specify
172 an external digital line 3 as a source, you would use
173 src=TRIG_EXT and arg=3. */
175 /* In this case, we specify using TRIG_NOW to start
176 * acquisition immediately when the command is issued.
177 * The argument of TRIG_NOW is "number of nsec after
178 * NOW", but no driver supports it yet. Also, no driver
179 * currently supports using a start_src other than
181 cmd->start_src = TRIG_NOW;
184 /* The timing of the beginning of each scan is controlled
185 * by scan_begin. TRIG_TIMER specifies that scan_start
186 * events occur periodically at a rate of scan_begin_arg
187 * nanoseconds between scans. */
188 cmd->scan_begin_src = TRIG_TIMER;
189 cmd->scan_begin_arg = msec_to_nsec(100);
191 /* The timing between each sample in a scan is controlled
192 * by convert. Like above, TRIG_TIMER specifies that
193 * convert events occur periodically at a rate of convert_arg
194 * nanoseconds between scans. */
195 cmd->convert_src = TRIG_TIMER;
196 cmd->convert_arg = msec_to_nsec(1);
198 /* The end of each scan is almost always specified using
199 * TRIG_COUNT, with the argument being the same as the
200 * number of channels in the chanlist. You could probably
201 * find a device that allows something else, but it would
203 cmd->scan_end_src = TRIG_COUNT;
204 cmd->scan_end_arg = n_chans; /* number of channels */
206 /* The end of acquisition is controlled by stop_src and
207 * stop_arg. The src will typically be TRIG_COUNT or
208 * TRIG_NONE. Specifying TRIG_COUNT will stop acquisition
209 * after stop_arg number of scans, or TRIG_NONE will
210 * cause acquisition to continue until stopped using
211 * comedi_cancel(). */
212 cmd->stop_src = TRIG_COUNT;
213 cmd->stop_arg = n_scans;
215 /* the channel list determined which channels are sampled.
216 In general, chanlist_len is the same as scan_end_arg. Most
217 boards require this. */
218 cmd->chanlist = chanlist;
219 cmd->chanlist_len = n_chans;