![]() |
RTXI 1.3
|
00001 /* 00002 * SIGIO example 00003 * Part of Comedilib 00004 * 00005 * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org> 00006 * 00007 * This file may be freely modified, distributed, and combined with 00008 * other software, as long as proper attribution is given in the 00009 * source code. 00010 */ 00011 00012 #define _GNU_SOURCE 00013 00014 #include <stdio.h> 00015 #include <comedilib.h> 00016 #include <fcntl.h> 00017 #include <unistd.h> 00018 #include <stdlib.h> 00019 #include <errno.h> 00020 #include <getopt.h> 00021 #include <ctype.h> 00022 #include <signal.h> 00023 #include <string.h> 00024 #include <sys/time.h> 00025 #include "examples.h" 00026 00027 comedi_t *device; 00028 00029 00030 void print_time(void); 00031 00032 void sigio_handler(int sig) 00033 { 00034 print_time(); 00035 } 00036 00037 void print_time(void) 00038 { 00039 struct timeval tv; 00040 static struct timeval oldtime={0}; 00041 int dsec,dusec; 00042 00043 gettimeofday(&tv,NULL); 00044 00045 dsec=tv.tv_sec-oldtime.tv_sec; 00046 dusec=tv.tv_usec-oldtime.tv_usec; 00047 if(dusec<0){ 00048 dsec--; 00049 dusec+=1000000; 00050 } 00051 printf("%d.%06d +%d.%06d\n",(int)tv.tv_sec,(int)tv.tv_usec,dsec,dusec); 00052 00053 oldtime=tv; 00054 } 00055 00056 int out_subd; 00057 00058 void config_output(void) 00059 { 00060 int i; 00061 00062 for(i=0;i<8;i++){ 00063 comedi_dio_config(device,out_subd,i,COMEDI_OUTPUT); 00064 } 00065 } 00066 00067 int count; 00068 00069 #define BUFSZ 1024 00070 sampl_t buf[BUFSZ]; 00071 00072 void do_cmd_1(comedi_t *dev, int subdevice); 00073 void do_cmd_2(comedi_t *dev); 00074 void do_cmd(comedi_t *dev,comedi_cmd *cmd); 00075 00076 int main(int argc, char *argv[]) 00077 { 00078 struct sigaction sa; 00079 int ret; 00080 sigset_t sigset; 00081 int flags; 00082 struct parsed_options options; 00083 00084 init_parsed_options(&options); 00085 parse_options(&options, argc, argv); 00086 00087 device = comedi_open(options.filename); 00088 if(!device){ 00089 perror(options.filename); 00090 exit(1); 00091 } 00092 00093 out_subd = 2; 00094 00095 config_output(); 00096 00097 fcntl(comedi_fileno(device), F_SETOWN, getpid()); 00098 flags = fcntl(comedi_fileno(device),F_GETFL); 00099 ret = fcntl(comedi_fileno(device),F_SETFL,flags|O_ASYNC); 00100 //ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC); 00101 if(ret<0)perror("fcntl"); 00102 00103 memset(&sa,0,sizeof(sa)); 00104 sa.sa_handler = &sigio_handler; 00105 ret = sigaction(SIGIO,&sa,NULL); 00106 if(ret<0)perror("sigaction"); 00107 00108 sigemptyset(&sigset); 00109 sigaddset(&sigset,SIGIO); 00110 ret = sigprocmask(SIG_UNBLOCK,&sigset,NULL); 00111 if(ret<0)perror("sigprocmask"); 00112 00113 #if 0 00114 { 00115 struct sched_param p; 00116 memset(&p,0,sizeof(p)); 00117 p.sched_priority = 1; 00118 ret = sched_setscheduler(0,SCHED_FIFO,&p); 00119 if(ret<0)perror("sched_setscheduler"); 00120 } 00121 #endif 00122 00123 do_cmd_1(device, options.subdevice); 00124 00125 return 0; 00126 } 00127 00128 void do_cmd(comedi_t *dev,comedi_cmd *cmd) 00129 { 00130 int total=0; 00131 int ret; 00132 int go; 00133 00134 ret=comedi_command_test(dev,cmd); 00135 00136 printf("test ret=%d\n",ret); 00137 if(ret<0){ 00138 printf("errno=%d\n",errno); 00139 comedi_perror("comedi_command_test"); 00140 return; 00141 } 00142 00143 dump_cmd(stdout,cmd); 00144 00145 ret=comedi_command_test(dev,cmd); 00146 00147 printf("test ret=%d\n",ret); 00148 if(ret<0){ 00149 printf("errno=%d\n",errno); 00150 comedi_perror("comedi_command_test"); 00151 return; 00152 } 00153 00154 dump_cmd(stdout,cmd); 00155 00156 ret=comedi_command(dev,cmd); 00157 00158 printf("ret=%d\n",ret); 00159 if(ret<0){ 00160 printf("errno=%d\n",errno); 00161 comedi_perror("comedi_command"); 00162 return; 00163 } 00164 00165 go=1; 00166 while(go){ 00167 ret=read(comedi_fileno(dev),buf,BUFSZ); 00168 if(ret<0){ 00169 if(errno==EAGAIN){ 00170 printf("EAGAIN\n"); 00171 usleep(10000); 00172 }else{ 00173 go = 0; 00174 perror("read"); 00175 } 00176 }else if(ret==0){ 00177 go = 0; 00178 }else{ 00179 //int i; 00180 00181 total+=ret; 00182 //printf("read %d %d\n",ret,total); 00183 //printf("count = %d\n",count); 00184 //print_time(); 00185 } 00186 } 00187 } 00188 00189 unsigned int chanlist[0]; 00190 /* 00191 * This part of the demo measures channels 1, 2, 3, 4 at a rate of 00192 * 10 khz, with the inter-sample time at 10 us (100 khz). The number 00193 * of scans measured is 10. This is analogous to the old mode2 00194 * acquisition. 00195 */ 00196 void do_cmd_1(comedi_t *dev, int subdevice) 00197 { 00198 comedi_cmd cmd; 00199 00200 memset(&cmd,0,sizeof(cmd)); 00201 00202 /* the subdevice that the command is sent to */ 00203 cmd.subdev = subdevice; 00204 00205 /* flags */ 00206 cmd.flags = TRIG_WAKE_EOS; 00207 00208 cmd.start_src = TRIG_NOW; 00209 cmd.start_arg = 0; 00210 00211 cmd.scan_begin_src = TRIG_TIMER; 00212 cmd.scan_begin_arg = msec_to_nsec(100); 00213 00214 #if 1 00215 cmd.convert_src = TRIG_TIMER; 00216 cmd.convert_arg = 1; 00217 #else 00218 cmd.convert_src = TRIG_ANY; 00219 cmd.convert_arg = 0; 00220 #endif 00221 00222 cmd.scan_end_src = TRIG_COUNT; 00223 cmd.scan_end_arg = 1; 00224 00225 cmd.stop_src = TRIG_NONE; 00226 cmd.stop_arg = 0; 00227 00228 cmd.chanlist = chanlist; 00229 cmd.chanlist_len = 1; 00230 00231 chanlist[0]=CR_PACK(0,0,0); 00232 00233 do_cmd(dev,&cmd); 00234 } 00235