RTXI 1.3
comedilib/demo/receiver.c
Go to the documentation of this file.
00001 /*
00002  * Digital I/O 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  * Requirements:  A board with a digital I/O subdevice.  Not just
00013  *    a 'digital input' or 'digital output' subdevice, but one in
00014  *    which the channels can be configured between input and output.
00015  */
00016 
00017 #define _GNU_SOURCE
00018 
00019 #include <stdio.h>
00020 #include <comedilib.h>
00021 #include <fcntl.h>
00022 #include <unistd.h>
00023 #include <stdlib.h>
00024 #include <errno.h>
00025 #include <getopt.h>
00026 #include <ctype.h>
00027 #include <signal.h>
00028 #include <string.h>
00029 #include <sys/time.h>
00030 #include "examples.h"
00031 
00032 int pin_clk = 2;
00033 int pin_data = 10;
00034 
00035 comedi_t *device;
00036 
00037 #define BUFSZ 1024
00038 sampl_t buf[BUFSZ];
00039 
00040 void prepare_cmd(comedi_t *dev, comedi_cmd *cmd, int subdevice);
00041 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
00042 
00043 int main(int argc, char *argv[])
00044 {
00045         comedi_t *dev;
00046         comedi_cmd cmd;
00047         int ret;
00048         struct parsed_options options;
00049 
00050         init_parsed_options(&options);
00051         options.channel = -1;
00052         parse_options(&options, argc, argv);
00053 
00054         dev = comedi_open(options.filename);
00055         if(!dev){
00056                 perror(options.filename);
00057                 exit(1);
00058         }
00059         device = dev;
00060 
00061         if(options.channel >= 0) pin_data = options.channel;
00062 
00063         ret = fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK);
00064         if(ret<0)perror("fcntl");
00065 
00066         prepare_cmd(dev, &cmd, options.subdevice);
00067 
00068         do_cmd(dev,&cmd);
00069 
00070         return 0;
00071 }
00072 
00073 static int c=0;
00074 static unsigned int bits =0;
00075 
00076 void do_cmd(comedi_t *dev,comedi_cmd *cmd)
00077 {
00078         unsigned int *chanlist;
00079         int n_chans;
00080         int total=0;
00081         int ret;
00082         int go;
00083         struct timeval timeout;
00084         fd_set rdset;
00085 
00086         chanlist = cmd->chanlist;
00087         n_chans = cmd->chanlist_len;
00088 
00089         ret=comedi_command_test(dev,cmd);
00090 
00091         //printf("test ret=%d\n",ret);
00092         if(ret<0){
00093                 printf("errno=%d\n",errno);
00094                 comedi_perror("comedi_command_test");
00095                 return;
00096         }
00097 
00098         dump_cmd(stdout,cmd);
00099 
00100         cmd->chanlist =         chanlist;
00101         cmd->chanlist_len =     n_chans;
00102 
00103         ret=comedi_command_test(dev,cmd);
00104 
00105         printf("test ret=%d\n",ret);
00106         if(ret<0){
00107                 printf("errno=%d\n",errno);
00108                 comedi_perror("comedi_command_test");
00109                 return;
00110         }
00111 
00112         dump_cmd(stdout,cmd);
00113 
00114         cmd->chanlist =         chanlist;
00115         cmd->chanlist_len =     n_chans;
00116 
00117         ret=comedi_command(dev,cmd);
00118 
00119         printf("ret=%d\n",ret);
00120         if(ret<0){
00121                 printf("errno=%d\n",errno);
00122                 comedi_perror("comedi_command");
00123                 return;
00124         }
00125 
00126         go=1;
00127         while(go){
00128                 FD_ZERO(&rdset);
00129                 FD_SET(comedi_fileno(dev),&rdset);
00130                 timeout.tv_sec=0;
00131                 timeout.tv_usec=50000;
00132                 ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
00133                 if(ret<0){
00134                         perror("select");
00135                 }else if(ret==0){
00136                         if(c){
00137                                 fprintf(stderr,"\n");
00138                                 c=0;
00139                                 bits=0;
00140                         }
00141                 }else if(FD_ISSET(comedi_fileno(dev),&rdset)){
00142                         ret=read(comedi_fileno(dev),buf,BUFSZ);
00143                         if(ret<0){
00144                                 if(errno==EAGAIN){
00145                                         go = 0;
00146                                         perror("read");
00147                                 }
00148                         }else if(ret==0){
00149                                 go = 0;
00150                         }else{
00151                                 int i;
00152 
00153                                 total+=ret;
00154                                 for(i=0;i<ret/sizeof(sampl_t);i++){
00155                                         fprintf(stderr,"%d",buf[i]>0xa000);
00156                                         c++;
00157                                         if(c>=32){
00158                                                 fprintf(stderr,"\n");
00159                                                 c=0;
00160                                         }
00161 #if 0
00162                                         //printf("%d %d\n",buf[i],buf[i]>0xa000);
00163                                         //printf("%d",buf[i]>0xa000);
00164                                         bits<<=1;
00165                                         bits|=(buf[i]>0xa000);
00166                                         c++;
00167                                         if(c>=33){
00168 #if 0
00169                                                 struct timeval now;
00170 
00171                                                 gettimeofday(&now,NULL);
00172                                                 printf(" %08x %ld.%06ld\n",bits,now.tv_sec,now.tv_usec);
00173                                                 c=0;
00174                                                 bits=0;
00175 #else
00176                                                 printf(" %08x\n",bits);
00177                                                 c=0;
00178                                                 bits=0;
00179 #endif
00180                                         }
00181                                         if(!bits)c=0;
00182 #endif
00183                                 }
00184                                 fflush(stdout);
00185                                 fflush(stderr);
00186                         }
00187                 }
00188         }
00189 }
00190 
00191 unsigned int chanlist[16];
00192 /*
00193  * This part of the demo measures channels 1, 2, 3, 4 at a rate of
00194  * 10 khz, with the inter-sample time at 10 us (100 khz).  The number
00195  * of scans measured is 10.  This is analogous to the old mode2
00196  * acquisition.
00197  */
00198 void prepare_cmd(comedi_t *dev,comedi_cmd *cmd, int subdevice)
00199 {
00200         memset(cmd,0,sizeof(comedi_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_EXT;
00212         cmd->scan_begin_arg =   CR_EDGE | CR_INVERT | pin_clk;
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(pin_data,0,0);
00232 }
00233 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines