int bogio_read_frames(const bogio_spec *spec, bogio_buf *buf, unsigned int frames, int blocking) { int bytes, i; sampl_t *raw; /* Place to store raw samples before normalisation */ unsigned int framesize = buf->spf * sizeof(sampl_t); /* Don't read past the end of the buffer */ if (frames > buf->frames) frames = buf->frames; if (!blocking) { /* If we're not going to block, find out how many samples can be read */ unsigned int frames_ready = comedi_get_buffer_contents(spec->m_dev, spec->subdevice) / framesize; if (frames > frames_ready) frames = frames_ready; } /* Reserve a temporary buffer of the I/O device's type */ raw = (sampl_t *)alloca(frames*framesize); comedi_poll(spec->m_dev, spec->subdevice); //printf("Going to read %u bytes from fd %d, %d available... ", frames*framesize, comedi_fileno(spec->m_dev), comedi_get_buffer_contents(spec->m_dev, spec->subdevice)); fflush(stdout); /* Fill it with data */ bytes = read(comedi_fileno(spec->m_dev), raw, frames*framesize); //perror("read"); //printf("Got %d\n", bytes); /* Ensure the read completed */ if (bytes <= 0) return bytes; frames = bytes/framesize; /* Convert to bogio's native type and normalise. */ for (i = 0; i < frames * buf->spf ; i++) buf->samples[i] = (bogio_sample_t)comedi_to_phys(raw[i], spec->fsd[i], spec->m_max_sample[i]); return frames; }
int main(int argc, char *argv[]) { comedi_cmd c,*cmd=&c; struct header_info header; long int t_samples, t_bytes, t_sleep; long int ret; int dt_usec, usec_elapsed; // struct timeval start,end; struct parsed_options options; unsigned short *samples; //, *junk; struct timeval now,then; long long unsigned int exec = 0; time_t t; umask(000); signal(SIGINT,do_depart); if (geteuid() != 0) { fprintf(stderr,"Must be setuid root to renice self.\n"); exit(0); } cmd_init_parsed_options(&options); cmd_parse_options(&options, argc, argv); t_samples = options.n_chan * options.samples; t_bytes = t_samples * 2; t_sleep = (long int) DMA_OVERSCAN_MULT*(1e6/options.freq)*options.samples; printf("freq = %f, tsamp = %li, tby = %li, tsleep = %li.\n",options.freq,t_samples,t_bytes,t_sleep); /* Test for failful options */ if (options.freq <= 1/(options.cadence + 0.00005)) { fprintf(stderr,"Acquisition frequency is faster than sampling frequency! FAIL!\n"); exit(ACQERR_BADINPUT); } if (options.samples > 16777216) { fprintf(stderr,"acq_d can't take more than 2^24 samples per set! Try acq_c.\n"); exit(ACQERR_BADINPUT); } ret = nice(-20); fprintf(stderr,"I've been niced to %li.\n",ret); if (ret != -20) printf(" WARNING!! Data collection could not set nice=-20. Data loss is probable at high speed."); dt_usec = options.cadence * 1e6; gettimeofday(&then, NULL); /* open the device */ dev = comedi_open(options.devfile); if (!dev) { comedi_perror(options.devfile); exit(ACQERR_BADDEV); } // printf("%li samples in %i-byte buffer.\n\n",t_samples,comedi_get_buffer_size(dev,options.subdevice)); prepare_cmd_lib(dev,options,cmd); printf("Testing command..."); ret = comedi_command_test(dev, cmd); if (ret < 0) { comedi_perror("comedi_command_test"); if(errno == EIO){ fprintf(stderr,"Ummm... this subdevice doesn't support commands\n"); } exit(ACQERR_BADCMD); } printf("%s...",cmdtest_messages[ret]); ret = comedi_command_test(dev, cmd); printf("%s...",cmdtest_messages[ret]); ret = comedi_command_test(dev, cmd); printf("%s...\n",cmdtest_messages[ret]); if (ret < 0) { fprintf(stderr,"Bad command, and unable to fix.\n"); dump_cmd(stderr, cmd); exit(ACQERR_BADCMD); } dump_cmd(stdout,cmd); comedi_close(dev); //fprintf(stderr,"%i scan -- %i chan -- %li samples -- %li bytes\n",options.samples,options.n_chan,t_samples,t_bytes); /* Print data file header */ // sprintf(dataheader, "[system]\n" // "whoami = \"%s\"") samples = (unsigned short*) malloc(t_bytes); // junk = (unsigned short*) malloc(t_bytes); if ((samples == NULL)) { // | (junk == NULL)) { fprintf(stderr,"Error allocating sample memory!\n"); exit(ACQERR_BADMEM); } printf("Starting acquisition...\n"); /*Open the serial port*/ /*serial = (FILE*)fopen("/dev/ttyS0","w");*/ /* Do what we're here to do */ while (running) { if (exec == options.sets) { break; } exec++; serial_toggle(); /* open the device */ dev = comedi_open(options.devfile); if (!dev) { comedi_perror(options.devfile); exit(ACQERR_BADDEV); } gettimeofday(&now, NULL); usec_elapsed = (now.tv_sec - then.tv_sec) * 1e6 + (now.tv_usec - then.tv_usec); while (usec_elapsed < dt_usec) { usleep(USLEEP_GRAN); gettimeofday(&now, NULL); usec_elapsed = (now.tv_sec - then.tv_sec) * 1e6 + (now.tv_usec - then.tv_usec); } /* start the command */ ret = comedi_command(dev, cmd); if (ret < 0) { comedi_perror("comedi_command"); exit(ACQERR_BADCMD); } then = now; /* * Insert enough waiting to get through most of the sampling */ /*serial_toggle();*/ usleep(t_sleep); /* * Wait until the buffer has our data. */ int exsleeps = 0; do { usleep(USLEEP_GRAN); comedi_poll(dev,options.subdevice); ret = comedi_get_buffer_contents(dev,options.subdevice); exsleeps++; } while(ret < t_bytes); // printf("Waited %ius for additional data.\n",exsleeps); /* * Get teh dataz. */ ret = read(comedi_fileno(dev), samples, t_bytes); if (ret < t_bytes) { fprintf(stderr,"Read mismatch! Got %li of %li bytes (%li/%li samples).\n",ret,t_bytes,ret/2,t_samples); } header.num_read = ret/2; // collect & discard overscan /* ret = comedi_get_buffer_contents(dev,options.subdevice); if (realloc(junk,ret) == NULL) { fprintf(stderr,"Error in oversample realloc?\n"); exit(ACQERR_BADMEM); } if (ret > 0) { ret = read(comedi_fileno(dev),junk,ret); }*/ comedi_close(dev); // Prepare header t = time(NULL); sprintf(header.site_id,"%s",site_str); header.start_time = t; header.start_timeval = now; header.num_channels=options.n_chan; header.channel_flags=0x0F; header.num_samples=options.samples; header.sample_frequency=options.freq; header.time_between_acquisitions=options.cadence; header.byte_packing=0; header.code_version=current_code_version; // Save latest to monitor file if (strcmp(options.monfile,"") != 0) { out = open(options.monfile,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); ret = errno; if (out > 0) { ret = write(out,&header,sizeof(header)); ret = write(out,samples,2*header.num_read); close(out); } else { fprintf(stderr,"Unable to open monitor file %s: %s.\n",options.monfile,strerror(ret)); exit(ACQERR_BADOUT); } } if (options.cadence >= 1.0 && options.verbose) printf("\tSaved latest to: %s\n",options.monfile); // Append to output file if (strcmp(options.outfile,"") != 0) { out = open(options.outfile,O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); ret = errno; if (out > 0) { ret = write(out,samples,2*header.num_read); close(out); if (options.cadence >= 1.0 && options.verbose) printf("\tAppended to: %s\n",options.outfile); } else { fprintf(stderr,"Unable to open output file %s: %s.\n",options.outfile,strerror(ret)); exit(ACQERR_BADOUT); } } } if (dev != NULL) comedi_close(dev); if (out != 0) close(out); printf("\nEnd.\n"); exit(0); }
void do_cmd(comedi_t *dev,comedi_cmd *cmd) { int total=0; int ret; int go; fd_set rdset; struct timeval timeout; ret=comedi_command_test(dev,cmd); printf("test ret=%d\n",ret); if(ret<0){ printf("errno=%d\n",errno); comedi_perror("comedi_command_test"); return; } dump_cmd(stdout,cmd); ret=comedi_command_test(dev,cmd); printf("test ret=%d\n",ret); if(ret<0){ printf("errno=%d\n",errno); comedi_perror("comedi_command_test"); return; } dump_cmd(stdout,cmd); comedi_set_read_subdevice(dev, cmd->subdev); ret = comedi_get_read_subdevice(dev); if (ret < 0 || ret != cmd->subdev) { fprintf(stderr, "failed to change 'read' subdevice from %d to %d\n", ret, cmd->subdev); return; } ret=comedi_command(dev,cmd); printf("ret=%d\n",ret); if(ret<0){ printf("errno=%d\n",errno); comedi_perror("comedi_command"); return; } go=1; while(go){ FD_ZERO(&rdset); FD_SET(comedi_fileno(device),&rdset); timeout.tv_sec = 0; timeout.tv_usec = 50000; ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout); //printf("select returned %d\n",ret); if(ret<0){ perror("select"); }else if(ret==0){ /* hit timeout */ printf("timeout, polling...\n"); ret = comedi_poll(device,cmd->subdev); printf("poll returned %d\n",ret); }else if(FD_ISSET(comedi_fileno(device),&rdset)){ /* comedi file descriptor became ready */ //printf("comedi file descriptor ready\n"); ret=read(comedi_fileno(dev),buf,sizeof(buf)); printf("read returned %d\n",ret); if(ret<0){ if(errno==EAGAIN){ go = 0; perror("read"); } }else if(ret==0){ go = 0; }else{ int i; total+=ret; //printf("read %d %d\n",ret,total); for(i=0;i<ret/sizeof(sampl_t);i++){ printf("%d\n",buf[i]); } } }else{ /* unknown file descriptor became ready */ printf("unknown file descriptor ready\n"); } } }