TEST(ComediTest,AnalogReadings ) { lsampl_t data; int ret; int subdev = 0; int chan = 0; int range = 0; int aref = AREF_GROUND; int num_scans = options.n_scan; int num_channels = options.n_chan; int readscans; std::string prop("/sys/class/comedi/comedi0/read_buffer_pos"); dev = comedi_open(options.filename); ASSERT_TRUE( dev ); subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice); RecordProperty("NumScans", num_scans); #if 0 fprintf(stderr, "command before testing:\n"); dump_cmd(stderr, cmd); #endif prepare_cmd_lib(dev, options.subdevice, num_scans, num_channels, 1e9 / options.freq, cmd); #if 0 fprintf(stderr, "command before testing:\n"); dump_cmd(stderr, cmd); #endif /** * Setup the Command structure */ cmd->chanlist_len = 2; cmd->stop_arg = num_scans; /* verify no problems */ ret = comedi_command_test(dev, cmd); ASSERT_GE( ret, 0 ) << "Comedi Testing of command for device was ok"; /* make sure our scan number hasn't changed */ ASSERT_EQ( cmd->stop_arg , num_scans ); ASSERT_EQ( cmd->chanlist_len , 2 ); ret = comedi_command(dev, cmd); sleep(1); ASSERT_GE( ret, 0 ) << "Able to Submit comedi command\n"; /* Verify that the buffer position has in fact moved to say num_scans scans remain in it */ FILE *fp = fopen(prop.c_str(),"r"); ASSERT_TRUE( fp ) << "Able to open sysfs property file"; fscanf(fp,"%d",&readscans ); ASSERT_EQ( readscans, 4*num_scans ) << "Able to read the fifo position 4*" << num_scans << "\n"; fclose(fp); comedi_close( dev ); }
static PyObject* comediModule(PyObject* self, PyObject* args) { char *device; int subdevice; int channel; int range; int aref; int n_chan; int n_scan; float freq; if (!PyArg_ParseTuple(args, "siiiiiif", &device, &subdevice, &channel, &range, &aref, &n_chan, &n_scan, &freq)) return NULL; comedi_t *dev; comedi_cmd c,*cmd=&c; int ret; int total=0; int i; struct timeval start,end; int subdev_flags; lsampl_t raw; struct parsed_options options; init_parsed_options(&options); options.filename = device; options.subdevice = subdevice; options.channel = channel; options.range = range; options.aref = aref; options.n_chan = n_chan; options.n_scan = n_scan; options.freq = freq; /* open the device */ dev = comedi_open(options.filename); if(!dev){ comedi_perror(options.filename); exit(1); } // Print numbers for clipped inputs comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); /* Set up channel list */ for(i = 0; i < options.n_chan; i++){ chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref); range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range); maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel); } prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd); fprintf(stderr, "command before testing:\n"); dump_cmd(stderr, cmd); 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(1); } fprintf(stderr,"first test returned %d (%s)\n", ret, cmdtest_messages[ret]); dump_cmd(stderr, cmd); ret = comedi_command_test(dev, cmd); if(ret < 0){ comedi_perror("comedi_command_test"); exit(1); } fprintf(stderr,"second test returned %d (%s)\n", ret, cmdtest_messages[ret]); if(ret!=0){ dump_cmd(stderr, cmd); fprintf(stderr, "Error preparing command\n"); exit(1); } /* this is only for informational purposes */ gettimeofday(&start, NULL); fprintf(stderr,"start time: %ld.%06ld\n", start.tv_sec, start.tv_usec); /* start the command */ ret = comedi_command(dev, cmd); if(ret < 0){ comedi_perror("comedi_command"); exit(1); } subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice); const int ndim = 2; npy_intp nd[2] = {n_scan, n_chan}; PyObject *myarray; double *array_buffer; myarray = PyArray_SimpleNew(ndim, nd, NPY_DOUBLE); Py_INCREF(myarray); array_buffer = (double *)PyArray_DATA(myarray); while(1){ ret = read(comedi_fileno(dev),buf,BUFSZ); if(ret < 0){ /* some error occurred */ perror("read"); break; }else if(ret == 0){ /* reached stop condition */ break; }else{ static int col = 0; double j = 0.0; int bytes_per_sample; total += ret; if(options.verbose)fprintf(stderr, "read %d %d\n", ret, total); if(subdev_flags & SDF_LSAMPL) bytes_per_sample = sizeof(lsampl_t); else bytes_per_sample = sizeof(sampl_t); for(i = 0; i < ret / bytes_per_sample; i++){ if(subdev_flags & SDF_LSAMPL) { raw = ((lsampl_t *)buf)[i]; } else { raw = ((sampl_t *)buf)[i]; } *array_buffer++ = print_datum(raw, col, 1); col++; if(col == options.n_chan){ col=0; j++; printf("\n"); } } } } /* this is only for informational purposes */ gettimeofday(&end,NULL); fprintf(stderr,"end time: %ld.%06ld\n", end.tv_sec, end.tv_usec); end.tv_sec -= start.tv_sec; if(end.tv_usec < start.tv_usec){ end.tv_sec--; end.tv_usec += 1000000; } end.tv_usec -= start.tv_usec; fprintf(stderr,"time: %ld.%06ld\n", end.tv_sec, end.tv_usec); return myarray; }
int main(int argc, char *argv[]) { comedi_t *dev; comedi_cmd c,*cmd=&c; int ret; int total=0; int i; struct timeval start,end; int subdev_flags; lsampl_t raw; struct parsed_options options; init_parsed_options(&options); parse_options(&options, argc, argv); /* The following variables used in this demo * can be modified by command line * options. When modifying this demo, you may want to * change them here. */ options.filename = "/dev/comedi0"; options.subdevice = 0; options.channel = 0; options.range = 6; options.aref = AREF_DIFF; options.n_chan = 8; options.n_scan = 100000; options.freq = 1000.0; options.physical = 1; /* open the device */ dev = comedi_open(options.filename); if(!dev){ comedi_perror(options.filename); exit(1); } // Print numbers for clipped inputs comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); /* Set up channel list */ //for(i = 0; i < options.n_chan/2; i++){ for(i = 0; i < options.n_chan; i++){ chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref); range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range); maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel); } /* for(i = options.n_chan/2; i < options.n_chan; i++){ fprintf(stderr,"%d: %d\n", i, options.channel + i + 8); chanlist[i] = CR_PACK(options.channel + i + 8, options.range, options.aref); range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range); maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel); } */ /* prepare_cmd_lib() uses a Comedilib routine to find a * good command for the device. prepare_cmd() explicitly * creates a command, which may not work for your device. */ prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd); //prepare_cmd(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd); fprintf(stderr, "command:\n"); dump_cmd(stderr, cmd); /* this is only for informational purposes */ gettimeofday(&start, NULL); fprintf(stderr,"start time: %ld.%06ld\n", start.tv_sec, start.tv_usec); /* start the command */ ret = comedi_command(dev, cmd); if(ret < 0){ comedi_perror("comedi_command"); exit(1); } subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice); while(1){ ret = read(comedi_fileno(dev),buf,BUFSZ); if(ret < 0){ /* some error occurred */ perror("read"); continue; }else if(ret == 0){ /* reached stop condition */ break; }else{ static int col = 0; int bytes_per_sample; total += ret; if(options.verbose)fprintf(stderr, "read %d %d\n", ret, total); if(subdev_flags & SDF_LSAMPL) bytes_per_sample = sizeof(lsampl_t); else bytes_per_sample = sizeof(sampl_t); for(i = 0; i < ret / bytes_per_sample; i++){ if(subdev_flags & SDF_LSAMPL) { raw = ((lsampl_t *)buf)[i]; } else { raw = ((sampl_t *)buf)[i]; } print_datum(raw, col, options.physical); col++; if(col == options.n_chan){ printf("\n"); col=0; } } } } /* this is only for informational purposes */ gettimeofday(&end,NULL); fprintf(stderr,"end time: %ld.%06ld\n", end.tv_sec, end.tv_usec); end.tv_sec -= start.tv_sec; if(end.tv_usec < start.tv_usec){ end.tv_sec--; end.tv_usec += 1000000; } end.tv_usec -= start.tv_usec; fprintf(stderr,"time: %ld.%06ld\n", end.tv_sec, end.tv_usec); return 0; }
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); }