int init_daq(double min_range, double max_range, int range_update) { int i = 0; if (!DEV_OPEN) { it = comedi_open("/dev/comedi0"); if (it == NULL) { comedi_perror("comedi_open"); ADC_OPEN = FALSE; DEV_OPEN = FALSE; return -1; } DEV_OPEN = TRUE; } subdev_ai = comedi_find_subdevice_by_type(it, COMEDI_SUBD_AI, subdev_ai); if (subdev_ai < 0) { return -2; ADC_OPEN = FALSE; } subdev_ao = comedi_find_subdevice_by_type(it, COMEDI_SUBD_AO, subdev_ao); if (subdev_ao < 0) { HAS_AO = FALSE; } else { HAS_AO = TRUE; } printf("Subdev AI %i ", subdev_ai); channels_ai = comedi_get_n_channels(it, subdev_ai); printf("Analog Channels %i ", channels_ai); maxdata_ai = comedi_get_maxdata(it, subdev_ai, i); printf("Maxdata %i ", maxdata_ai); ranges_ai = comedi_get_n_ranges(it, subdev_ai, i); printf("Ranges %i ", ranges_ai); ad_range = comedi_get_range(it, subdev_ai, i, ranges_ai - 1); if (range_update) { ad_range->min = min_range; ad_range->max = max_range; } printf(": ad_range .min = %.3f, max = %.3f\n", ad_range->min, ad_range->max); if (HAS_AO) { printf("Subdev AO %i ", subdev_ao); channels_ao = comedi_get_n_channels(it, subdev_ao); printf("Analog Channels %i ", channels_ao); maxdata_ao = comedi_get_maxdata(it, subdev_ao, i); printf("Maxdata %i ", maxdata_ao); ranges_ao = comedi_get_n_ranges(it, subdev_ao, i); printf("Ranges %i ", ranges_ao); da_range = comedi_get_range(it, subdev_ao, i, ranges_ao - 1); printf(": da_range .min = %.3f, max = %.3f\n", da_range->min, da_range->max); } ADC_OPEN = TRUE; comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); return 0; }
static int init_board(void) { dev = comedi_open("/dev/comedi0"); printf("Comedi device (6071) handle: %p.\n", dev); if (!dev){ printf("Unable to open (6071) %s.\n", "/dev/comedi0"); return 1; } subdevai = comedi_find_subdevice_by_type(dev, COMEDI_SUBD_AI, 0); if (subdevai < 0) { comedi_close(dev); printf("AI subdev (6071) %d not found.\n", COMEDI_SUBD_AI); return 1; } comedi_get_krange(dev, subdevai, 0, AI_RANGE, &krangeai); maxdatai = comedi_get_maxdata(dev, subdevai, 0); subdevao = comedi_find_subdevice_by_type(dev, COMEDI_SUBD_AO, 0); if (subdevao < 0) { comedi_close(dev); printf("AO subdev (6071) %d not found.\n", COMEDI_SUBD_AO); return 1; } comedi_get_krange(dev, subdevao, 0, AO_RANGE, &krangeao); maxdatao = comedi_get_maxdata(dev, subdevao, 0); return 0; }
int init_dio(void) { int i = 0; if (!DEV_OPEN) { it = comedi_open(comedi_dev); if (it == NULL) { comedi_perror("comedi_open"); DIO_OPEN = FALSE; DEV_OPEN = FALSE; return -1; } DEV_OPEN = TRUE; } subdev_dio = comedi_find_subdevice_by_type(it, COMEDI_SUBD_DIO, subdev_dio); if (subdev_dio < 0) { return -1; DIO_OPEN = FALSE; } printf("Subdev %i ", subdev_dio); channels_dio = comedi_get_n_channels(it, subdev_dio); printf("Digital Channels %i ", channels_dio); maxdata_dio = comedi_get_maxdata(it, subdev_dio, i); printf("Maxdata %i ", maxdata_dio); ranges_dio = comedi_get_n_ranges(it, subdev_dio, i); printf("Ranges %i \n", ranges_dio); DIO_OPEN = TRUE; return 0; }
int cal() { int subdevai, subdevao; // printf("calibrating.."); fflush(stdout); devc = comedi_open("/dev/comedi0"); if (!devc) { printf("Unable to open (6071) %s.\n", "/dev/comedi0"); return 1; } subdevai = comedi_find_subdevice_by_type(devc, COMEDI_SUBD_AI, 0); subdevao = comedi_find_subdevice_by_type(devc, COMEDI_SUBD_AO, 0); comcal = comedi_parse_calibration_file("/usr/var/lib/comedi/calibrations/ni_pcimio_pci-6289_comedi0"); comedi_get_softcal_converter(subdevai,0, 0, COMEDI_TO_PHYSICAL, comcal, &conv_ai); comedi_get_softcal_converter(subdevao,0, 0, COMEDI_FROM_PHYSICAL, comcal, &conv_ao); //comedi_cleanup_calibration_file(cal); comedi_close(devc); //printf("done\n");fflush(stdout); return 0; }
/*! \fn newViewDialog::slotPluginSelected() */ void newViewDialog::slotPluginSelected() { QString name = pluginsList->currentItem()->text(); okPushButton->setEnabled( TRUE ); QListIterator<pluginData> it(pl); while( it.hasNext() ){ pluginData data = it.next(); if ( data.name == name ){ maxSelect = data.numChannels; int comediSubdevice = comedi_find_subdevice_by_type(comediDevice,data.type_comedi,0); if( comediSubdevice >= 0){ maxChannels = comedi_get_n_channels(comediDevice, comediSubdevice); okPushButton->setEnabled(TRUE); status->setText(QString("comedi subdevice\ntype %1 found!").arg(data.type_comedi)); } else{ //comedi_perror( QString("error in %1 line %2").arg(__func__).arg(__LINE__).toStdString().c_str() ); okPushButton->setEnabled(FALSE); maxChannels = 0; status->setText(QString("comedi subdevice\ntype %1 not found!").arg(data.type_comedi)); } if( maxChannels < 0 ){ maxChannels = 0; comedi_perror( QString("error in %1 line %2").arg(__func__).arg(__LINE__).toStdString().c_str() ); } if(channelSelectors.count() < maxSelect) for( int i = channelSelectors.count(); i < maxSelect; i++ ) { QSpinBox *spinBox = new QSpinBox(); spinBox->setRange( 0, maxChannels-1 ); channelsListL->addWidget( spinBox ); channelSelectors << spinBox ; } else if(channelSelectors.count() > maxSelect) { channelSelectors.last(); for( int i = maxSelect; i< channelSelectors.count(); i++ ) delete channelSelectors.takeLast(); } } } }
int init_daq(void) { int i = 0, range_index = 0; comedi_range *ad_range; if (!DEV_OPEN) { it = comedi_open(comedi_dev); if (it == NULL) { comedi_perror("comedi_open"); ADC_OPEN = FALSE; DEV_OPEN = FALSE; HAVE_DIO = FALSE; HAVE_AI = FALSE; return -1; } DEV_OPEN = TRUE; } subdev_ai = comedi_find_subdevice_by_type(it, COMEDI_SUBD_AI, subdev_ai); if (subdev_ai < 0) { return -1; ADC_OPEN = FALSE; } printf("Subdev %i ", subdev_ai); channels_ai = comedi_get_n_channels(it, subdev_ai); printf("Analog Channels %i ", channels_ai); maxdata_ai = comedi_get_maxdata(it, subdev_ai, i); printf("Maxdata %i ", maxdata_ai); ranges_ai = comedi_get_n_ranges(it, subdev_ai, i); printf("Ranges %i ", ranges_ai); for (range_index = 0; range_index < ranges_ai; range_index++) { ad_range = comedi_get_range(it, subdev_ai, i, range_index); printf(": range %i, min = %.1f, max = %.1f ", range_index, ad_range->min, ad_range->max); } printf("\n"); ADC_OPEN = TRUE; comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); return 0; }
int main(int argc, char *argv[]) { int ret; int stype; int i; struct parsed_options options; init_parsed_options(&options); options.subdevice = -1; parse_options(&options, argc, argv); device = comedi_open(options.filename); if(!device){ comedi_perror(options.filename); exit(-1); } if(options.subdevice < 0) { options.subdevice = comedi_find_subdevice_by_type(device, COMEDI_SUBD_DIO, 0); if(options.subdevice < 0){ fprintf(stderr,"No dio subdevice found.\n"); exit(-1); } } stype = comedi_get_subdevice_type(device, options.subdevice); if(stype != COMEDI_SUBD_DIO){ printf("%d is not a digital I/O subdevice\n", options.subdevice); exit(-1); } printf("configuring pin %d for output...\n", chan_dat); ret = comedi_dio_config(device, options.subdevice, chan_dat, COMEDI_OUTPUT); printf("configuring pin %d for output...\n", chan_clk); ret = comedi_dio_config(device, options.subdevice, chan_clk, COMEDI_OUTPUT); for(i = 0; i < 0x100; i++){ write_bits(options.subdevice, i); } //write_bits(0xa5); return 0; }
int main(int argc, char *argv[]) { int ret; comedi_insn insn; lsampl_t d[5]; comedi_t *device; int freq; struct parsed_options options; init_parsed_options(&options); options.freq = -1; // we hijack this option to switch it on or off options.n_scan = -1; options.value = -1; parse_options(&options, argc, argv); if ((options.value==-1)&&(options.n_scan==-1)&&(options.freq==-1)) { fprintf(stderr, "Usage: %s OPTIONS duty_cycle\n" "options: \n" " -N 0 switches PWM off\n" " -N 1 switches PWM on\n" " -N 2 enquires the max value for the duty cycle\n" " -F FREQ sets the PWM frequency\n", argv[0]); } device = comedi_open(options.filename); if(!device){ comedi_perror(options.filename); exit(-1); } options.subdevice = comedi_find_subdevice_by_type(device,COMEDI_SUBD_PWM,0); if (options.verbose) printf("PWM subdevice autodetection gave subdevice number %d\n", options.subdevice); if(options.n_scan==2) { printf("%d\n",comedi_get_maxdata(device,options.subdevice,0)); comedi_close(device); exit(0); } insn.insn=INSN_CONFIG; insn.data=d; insn.subdev=options.subdevice; insn.chanspec=CR_PACK(0,0,0); if(options.n_scan==1) { d[0] = INSN_CONFIG_ARM; d[1] = 0; insn.n=2; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could not switch on:%d\n",ret); comedi_perror(options.filename); exit(-1); } } if(options.n_scan==0) { d[0] = INSN_CONFIG_DISARM; d[1] = 0; insn.n=1; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could not switch off:%d\n",ret); comedi_perror(options.filename); exit(-1); } } if(options.freq>0) { freq = options.freq; d[0] = INSN_CONFIG_PWM_SET_PERIOD; d[1] = 1E9/freq; insn.n=2; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could set frequ:%d\n",ret); comedi_perror(options.filename); exit(-1); } } d[0] = INSN_CONFIG_GET_PWM_STATUS; insn.n=2; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could not get status:%d insn=%d\n", ret, d[0]); comedi_perror(options.filename); exit(-1); } if (options.verbose) { if (d[1]) fprintf(stderr, "PWM is on.\n"); else fprintf(stderr, "PWM is off.\n"); } d[0] = INSN_CONFIG_PWM_GET_PERIOD; insn.n=2; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could get frequ:%d\n",ret); comedi_perror(options.filename); exit(-1); } freq = 1E9 / d[1]; if (options.verbose) fprintf(stderr,"PWM frequency is %d\n", freq); if (options.value>=0) if(comedi_data_write(device, options.subdevice, options.channel, 0, 0, options.value)<0) { fprintf(stderr,"error setting the pwm duty cycle on "); comedi_perror(options.filename); exit(1); } return 0; }
/* -------------------------------------------------------------------- *\ * DSET METHODS \* -------------------------------------------------------------------- */ static long dsetInit_devAiSyncComedi(aiRecord *pai) { CONTEXT *p_myContext; int myMinorOfDeviceFile; int myPinNumber; int myChannel = 0; int mySubdevice = 0; int myRange = 0; int myAnalogReference = AREF_DIFF; int myNumberOfRanges; char p_myDeviceFile[BUFLEN]; comedi_t *p_myComediFileHandle; int i; packageInfo(); dsetLog(3, __FILE__ "[%d] -> %s(%s)\n", __LINE__, __func__, pai->name ); sscanf(pai->inp.value.instio.string, "modf=%d pn=%d", &myMinorOfDeviceFile, &myPinNumber); sprintf(p_myDeviceFile, "/dev/comedi%d", myMinorOfDeviceFile); dsetLog(5,__FILE__ "[%d] minorOfDeviceFile >%d<\n", __LINE__, myMinorOfDeviceFile); dsetLog(5,__FILE__ "[%d] pinNumber >%d<\n", __LINE__, myPinNumber); dsetLog(5,__FILE__ "[%d] deviceFile >%s<\n", __LINE__, p_myDeviceFile); /** * Convert the pin number to the appropriate channel number. */ for (i = 0; i < MAX_NUM_CHANNELS; i++) { if (pinsToChannels[i] == myPinNumber) { myChannel = i; } } p_myComediFileHandle = comedi_open(p_myDeviceFile); if (!p_myComediFileHandle) { comedi_perror(p_myDeviceFile); dsetLog(1, __FILE__ "[%d] Error: Failed to open the file handler\n", __LINE__); sleep(SLEEPTIME_ERROR); return(ERROR); } mySubdevice = comedi_find_subdevice_by_type(p_myComediFileHandle, COMEDI_SUBD_AI, 0); if (mySubdevice < 0) { dsetLog(1, __FILE__ "[%d] Error: Couldn't find an analog input subdevice\n", __LINE__); sleep(SLEEPTIME_ERROR); return(ERROR); } dsetLog(5,__FILE__ "[%d] mySubdevice = %d\n", __LINE__, mySubdevice); myNumberOfRanges = comedi_get_n_ranges(p_myComediFileHandle, mySubdevice, myChannel); dsetLog(5,__FILE__ "[%d] # of comedi range = %d\n", __LINE__, myNumberOfRanges); p_myContext = (CONTEXT *)calloc(1, sizeof(CONTEXT)); strcpy(p_myContext->p_deviceFile, p_myDeviceFile); p_myContext->channel = myChannel; p_myContext->range = myRange; p_myContext->pinNumber = myPinNumber; p_myContext->subdevice = mySubdevice; p_myContext->analogReference = myAnalogReference; p_myContext->p_comediFileHandle = p_myComediFileHandle; pai->dpvt = p_myContext; dsetLog(3,__FILE__ "[%d] <- %s\n", __LINE__, __func__); return(SUCCESS); }
ComediScope::ComediScope( ComediRecord *comediRecordTmp, int channels, float f, int port_for_ext_data, int maxComediDevs, int first_dev_no, int req_sampling_rate, const char* defaultTextStringForMissingExtData, int fftdevnumber, int fftchannel, int fftmaxf ) : QWidget( comediRecordTmp ) { channels_in_use = channels; tb_init=1; tb_counter=tb_init; comediRecord=comediRecordTmp; // erase plot eraseFlag = 1; fftdevno = fftdevnumber; fftch = fftchannel; fftmaxfrequency = fftmaxf; // for ASCII rec_file=NULL; // filename rec_filename=new QString(); // flag if data has been recorded and we need a new filename recorded=0; if (port_for_ext_data>0) { fprintf(stderr, "Expecting a connection on TCP port %d. \n" "Start your client now, for example: \n" "telnet localhost %d\n" "Press Ctrl-C to abort.\n", port_for_ext_data, port_for_ext_data); ext_data_receive = new Ext_data_receive( port_for_ext_data, defaultTextStringForMissingExtData ); } else { ext_data_receive = NULL; } ////////////////////////////////////////////////////////////// setAttribute(Qt::WA_OpaquePaintEvent); int range = 0; int aref = AREF_GROUND; // do not produce NAN for out of range behaviour comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); // create an array which keeps the comedi devices dev = new comedi_t*[maxComediDevs]; for(int devNo=0;devNo<maxComediDevs;devNo++) { dev[devNo] = NULL; } // let's probe how many we have nComediDevices = 0; for(int devNo=0;devNo<maxComediDevs;devNo++) { char filename[128]; sprintf(filename,"/dev/comedi%d",devNo+first_dev_no); dev[devNo] = comedi_open(filename); if(dev[devNo]){ nComediDevices = devNo + 1; } else { break; } } // none detected if (nComediDevices<1) { fprintf(stderr,"No comedi devices detected!\n"); exit(1); } // create channel lists chanlist = new unsigned int*[nComediDevices]; // create command structures cmd = new comedi_cmd*[nComediDevices]; // find the subdevice which is analogue in subdevice = comedi_find_subdevice_by_type(dev[0],COMEDI_SUBD_AI,0); // check if user has specified channels or if requested // number of channels make sense if ((channels_in_use < 1)|| (channels_in_use > comedi_get_n_channels(dev[0],subdevice))) { channels_in_use = comedi_get_n_channels(dev[0],subdevice); } // create channel lists and the command structures for(int devNo=0;devNo<nComediDevices;devNo++) { chanlist[devNo] = new unsigned int[channels_in_use]; assert( chanlist[devNo]!=NULL ); for(int i=0;i<channels_in_use;i++){ chanlist[devNo][i] = CR_PACK(i,range,aref); } cmd[devNo] = new comedi_cmd; assert( dev[devNo]!=NULL ); int r = comedi_get_cmd_generic_timed(dev[devNo], subdevice, cmd[devNo], channels_in_use, (int)(1e9/req_sampling_rate)); if(r<0){ comedi_perror("comedi_get_cmd_generic_timed failed\n"); exit(-1); } /* Modify parts of the command */ cmd[devNo]->chanlist = chanlist[devNo]; cmd[devNo]->chanlist_len = channels_in_use; cmd[devNo]->scan_end_arg = channels_in_use; cmd[devNo]->stop_src=TRIG_NONE; cmd[devNo]->stop_arg=0; int ret = comedi_command_test(dev[devNo],cmd[devNo]); if(ret<0){ comedi_perror("1st comedi_command_test failed\n"); exit(-1); } ret = comedi_command_test(dev[devNo],cmd[devNo]); if(ret<0){ comedi_perror("2nd comedi_command_test failed\n"); exit(-1); } if(ret!=0){ fprintf(stderr,"Error preparing command.\n"); exit(-1); } } // the timing is done channel by channel // this means that the actual sampling rate is divided by // number of channels if ((cmd[0]->convert_src == TRIG_TIMER)&&(cmd[0]->convert_arg)) { sampling_rate=((1E9 / cmd[0]->convert_arg)/channels_in_use); } // the timing is done scan by scan (all channels at once) // the sampling rate is equivalent of the scan_begin_arg if ((cmd[0]->scan_begin_src == TRIG_TIMER)&&(cmd[0]->scan_begin_arg)) { sampling_rate=1E9 / cmd[0]->scan_begin_arg; } // initialise the graphics stuff ypos = new int**[nComediDevices]; assert(ypos != NULL); for(int devNo=0;devNo<nComediDevices;devNo++) { ypos[devNo]=new int*[channels_in_use]; assert(ypos[devNo] != NULL); for(int i=0;i<channels_in_use;i++) { ypos[devNo][i] = new int[MAX_DISP_X]; assert( ypos[devNo][i] != NULL); for(int j=0;j<MAX_DISP_X;j++) { ypos[devNo][i][j]=0; } } } xpos=0; nsamples=0; maxdata = new lsampl_t[nComediDevices]; assert( maxdata != NULL ); crange = new comedi_range*[nComediDevices]; assert( crange != NULL ); for(int devNo=0;devNo<nComediDevices;devNo++) { // we just go for the default ranges maxdata[devNo]=comedi_get_maxdata(dev[devNo],subdevice,0); crange[devNo]=comedi_get_range(dev[devNo],subdevice,0,0); } // 50Hz or 60Hz mains notch filter iirnotch = new Iir::Butterworth::BandStop<IIRORDER>**[nComediDevices]; assert( iirnotch != NULL ); adAvgBuffer = new float*[nComediDevices]; assert( adAvgBuffer != NULL ); daqData = new lsampl_t*[nComediDevices]; assert( daqData != NULL ); for(int devNo=0;devNo<nComediDevices;devNo++) { iirnotch[devNo] = new Iir::Butterworth::BandStop<IIRORDER>*[channels_in_use]; assert( iirnotch[devNo] != NULL ); // floating point buffer for plotting adAvgBuffer[devNo]=new float[channels_in_use]; assert( adAvgBuffer[devNo] != NULL ); for(int i=0;i<channels_in_use;i++) { adAvgBuffer[devNo][i]=0; iirnotch[devNo][i] = new Iir::Butterworth::BandStop<IIRORDER>; assert( iirnotch[devNo][i] != NULL ); } // raw data buffer for saving the data daqData[devNo] = new lsampl_t[channels_in_use]; assert( daqData[devNo] != NULL ); for(int i=0;i<channels_in_use;i++) { daqData[devNo][i]=0; } } setNotchFrequency(f); counter = new QTimer( this ); assert( counter != NULL ); connect( counter, SIGNAL(timeout()), this, SLOT(updateTime()) ); }
int main(int argc, char *argv[]) { comedi_cmd cmd; int err; int n, m, k; comedi_t *dev; unsigned int chanlist[100]; unsigned int maxdata; comedi_range *rng; int ret; struct parsed_options options; int fn = 256*256; sampl_t *data, *dp; float v; init_parsed_options(&options); options.subdevice = -1; options.n_chan = 100000;/* default number of samples */ parse_options(&options, argc, argv); dev = comedi_open( options.filename ); if(dev == NULL){ fprintf(stderr, "error opening %s\n", options.filename); return -1; } if(options.subdevice < 0) options.subdevice = comedi_find_subdevice_by_type(dev, COMEDI_SUBD_AI, 0); maxdata = comedi_get_maxdata(dev, options.subdevice, options.channel); rng = comedi_get_range(dev, options.subdevice, options.channel, options.range); memset(&cmd,0,sizeof(cmd)); /* cmd.subdev = options.subdevice; cmd.flags = 0; cmd.start_src = TRIG_INT; cmd.start_arg = 0; cmd.scan_begin_src = TRIG_TIMER; cmd.scan_begin_arg = 1e9 / options.freq; cmd.convert_src = TRIG_TIMER; cmd.convert_arg = 1e9 / options.freq / 50; cmd.scan_end_src = TRIG_COUNT; cmd.scan_end_arg = options.n_chan; cmd.stop_src = TRIG_COUNT; cmd.stop_arg = fn; cmd.stop_src = TRIG_NONE; cmd.stop_arg = 0; */ cmd.scan_begin_src = TRIG_TIMER; cmd.flags = TRIG_ROUND_NEAREST; err = comedi_get_cmd_generic_timed( dev, options.subdevice, &cmd, options.n_chan, 1e9 / options.freq ); cmd.start_src = TRIG_INT; cmd.start_arg = 0; cmd.scan_end_arg = options.n_chan; cmd.stop_src = TRIG_NONE; cmd.stop_arg = 0; cmd.chanlist = chanlist; cmd.chanlist_len = options.n_chan; for ( k=0; k<options.n_chan; k++ ) chanlist[k] = CR_PACK(k, options.range, options.aref); dump_cmd(stderr,&cmd); if ( cmd.scan_begin_arg > 1e9 / options.freq ) { fprintf( stderr, "frequency too high! Maximum possible is %g Hz\n", 1.0e9/cmd.scan_begin_arg ); comedi_close( dev ); return 1; } err = comedi_command_test(dev, &cmd); if (err > 0 && err != 4 ) { fprintf( stderr, "comedi_command_test returned %d\n", err ); dump_cmd(stdout,&cmd); exit(1); } err = comedi_command_test(dev, &cmd); if (err > 0 && err != 4 ) { fprintf( stderr, "comedi_command_test returned %d\n", err ); dump_cmd(stdout,&cmd); exit(1); } dump_cmd(stderr,&cmd); /* init data buffer: */ data = (sampl_t *)malloc( fn*sizeof( sampl_t ) ); if(data == NULL ){ perror("malloc\n"); exit(1); } fprintf( stderr, "execute command ...\n" ); if ((err = comedi_command(dev, &cmd)) < 0) { comedi_perror("comedi_command"); exit(1); } fprintf( stderr, "start analog input ...\n" ); ret = comedi_internal_trigger(dev, options.subdevice, 0); if(ret < 0){ perror("comedi_internal_trigger\n"); exit(1); } n = 0; while( 1 ) { m = read(comedi_fileno(dev),(void *)data,fn*sizeof( sampl_t)); if(m<0){ if ( errno != EAGAIN ) { perror("read"); exit(0); } else { fprintf( stderr, "... no more data can be read! Try later.\n" ); usleep( 100000 ); } } else { m /= sizeof( sampl_t); fprintf( stderr, "read %d samples\n",m); n+=m; } } comedi_cancel( dev, cmd.subdev ); fprintf( stderr, "finished\n" ); /* save data: */ dp = data; for ( k=0; k<n; k++ ) { v = comedi_to_phys(*dp, rng, maxdata); printf( "%g\n", v ); /* printf( "%d\n", *dp );*/ ++dp; } free( data ); comedi_close( dev ); return 0; }
static void mdlStart(SimStruct *S) { #ifndef MATLAB_MEX_FILE void *dev; int subdev; int index = (int)COMEDI_DEVICE - 1; unsigned int channel = (unsigned int)COMEDI_CHANNEL; unsigned int range = (unsigned int)COMEDI_RANGE; int n_channels; char *devname[4] = {"/dev/comedi0","/dev/comedi1","/dev/comedi2","/dev/comedi3"}; char board[50]; static char_T errMsg[256]; comedi_krange krange; double range_min, range_max; if (!ComediDev[index]) { dev = comedi_open(devname[index]); if (!dev) { sprintf(errMsg, "Comedi open failed\n"); ssSetErrorStatus(S, errMsg); printf("%s", errMsg); return; } rt_comedi_get_board_name(dev, board); printf("COMEDI %s (%s) opened.\n\n", devname[index], board); ComediDev[index] = dev; } else { dev = ComediDev[index]; } if ((subdev = comedi_find_subdevice_by_type(dev, COMEDI_SUBD_AI, 0)) < 0) { sprintf(errMsg, "Comedi find_subdevice failed (No analog input)\n"); ssSetErrorStatus(S, errMsg); printf("%s", errMsg); comedi_close(dev); return; } if (!ComediDev_AIInUse[index] && comedi_lock(dev, subdev) < 0) { sprintf(errMsg, "Comedi lock failed for subdevice %d\n", subdev); ssSetErrorStatus(S, errMsg); printf("%s", errMsg); comedi_close(dev); return; } if ((n_channels = comedi_get_n_channels(dev, subdev)) < 0) { sprintf(errMsg, "Comedi get_n_channels failed for subdevice %d\n", subdev); ssSetErrorStatus(S, errMsg); printf("%s", errMsg); comedi_unlock(dev, subdev); comedi_close(dev); return; } if ((comedi_get_krange(dev, subdev, channel, range, &krange)) < 0) { sprintf(errMsg, "Comedi get range failed for subdevice %d\n", subdev); ssSetErrorStatus(S, errMsg); printf("%s", errMsg); comedi_unlock(dev, subdev); comedi_close(dev); return; } ComediDev_InUse[index]++; ComediDev_AIInUse[index]++; range_min = (double)(krange.min)*1.e-6; range_max = (double)(krange.max)*1.e-6; printf("AI Channel %d - Range : %1.2f [V] - %1.2f [V]\n", channel, range_min, range_max); ssGetPWork(S)[0] = (void *)dev; ssGetIWork(S)[0] = subdev; ssGetRWork(S)[0] = range_min; ssGetRWork(S)[1] = range_max; #endif }
static void init(scicos_block *block) { struct ADCOMDev * comdev = (struct ADCOMDev *) malloc(sizeof(struct ADCOMDev)); *block->work = (void *)comdev; char devName[15]; char board[50]; comedi_krange krange; comdev->channel = block->ipar[0]; comdev->range = block->ipar[1]; comdev->aref = block->ipar[2]; comdev->index = block->ipar[3]; sprintf(devName,"/dev/comedi%d", comdev->index); if (!ComediDev[comdev->index]) { comdev->dev = comedi_open(devName); if (!(comdev->dev)) { fprintf(stderr, "COMEDI %s open failed\n", devName); exit_on_error(); return; } rt_comedi_get_board_name(comdev->dev, board); printf("COMEDI %s (%s) opened.\n\n", devName, board); ComediDev[comdev->index] = comdev->dev; } else comdev->dev = ComediDev[comdev->index]; if ((comdev->subdev = comedi_find_subdevice_by_type(comdev->dev, COMEDI_SUBD_AI, 0)) < 0) { fprintf(stderr, "Comedi find_subdevice failed (No analog input)\n"); comedi_close(comdev->dev); exit_on_error(); return; } if (!ComediDev_AIInUse[comdev->index] && comedi_lock(comdev->dev, comdev->subdev) < 0) { fprintf(stderr, "Comedi lock failed for subdevice %d\n", comdev->subdev); comedi_close(comdev->dev); exit_on_error(); return; } if (comdev->channel >= comedi_get_n_channels(comdev->dev, comdev->subdev)) { fprintf(stderr, "Comedi channel not available for subdevice %d\n", comdev->subdev); comedi_unlock(comdev->dev, comdev->subdev); comedi_close(comdev->dev); exit_on_error(); return; } if ((comedi_get_krange(comdev->dev, comdev->subdev, comdev->channel, comdev->range, &krange)) < 0) { fprintf(stderr, "Comedi get_range failed for subdevice %d\n", comdev->subdev); comedi_unlock(comdev->dev, comdev->subdev); comedi_close(comdev->dev); exit_on_error(); return; } #ifdef SOFTCALIB int flags; if ((flags = comedi_get_subdevice_flags(comdev->dev, comdev->subdev)) < 0) { fprintf(stderr, "Comedi get_subdevice_flags failed for subdevice %d\n", comdev->subdev); } else { if (flags & SDF_SOFT_CALIBRATED) {/* board uses software calibration */ if ((comdev->use_softcal = get_softcal_coef(devName, comdev->subdev, comdev->channel, comdev->range, 0, comdev->coefficients)) == 0) fprintf(stderr, "No software calibration found for AI Channel %d\n",comdev->channel); } } #endif ComediDev_InUse[comdev->index]++; ComediDev_AIInUse[comdev->index]++; comdev->maxdata = comedi_get_maxdata(comdev->dev, comdev->subdev, comdev->channel); comdev->range_min = (double)(krange.min)*1.e-6; comdev->range_max = (double)(krange.max)*1.e-6; printf("AI Channel %d - Range : %1.2f [V] - %1.2f [V]%s\n\n", comdev->channel, comdev->range_min, comdev->range_max, (comdev->use_softcal)?" Software calibration used":""); }
static int openBoard( int board ) { MY_BOARD_HARDWARE *pBoardHard = myHardwareBoardList + board; MY_BOARD_SOFTWARE *pBoardSoft = mySoftwareBoardList + board; fprintf( stderr, "openBoard[%d]\n", board ); fprintf( stderr, "sizeof(lsampl_t)=%d\n", sizeof(lsampl_t) ); fprintf( stderr, "sizeof(sampl_t)=%d\n", sizeof(sampl_t) ); if (pBoardHard == NULL || pBoardSoft == NULL) return 1; pBoardHard->p_comediFileHandle = comedi_open( pBoardHard->devicePath ); if (pBoardHard->p_comediFileHandle == NULL) { fprintf( stderr, "comedi_open failed for board[%d] %s\n", board, pBoardHard->devicePath ); return 1; } fprintf( stderr, "opened file\n" ); pBoardHard->subDeviceNumber = comedi_find_subdevice_by_type( pBoardHard->p_comediFileHandle, COMEDI_SUBD_AI, 0 ); if (pBoardHard->subDeviceNumber < 0) { fprintf( stderr, "subDeviceNum < 0 failed for board[%d]\n", board ); return 1; } fprintf( stderr, "get subDevice=%d\n", pBoardHard->subDeviceNumber ); int subDevFlags = comedi_get_subdevice_flags( pBoardHard->p_comediFileHandle, pBoardHard->subDeviceNumber ); if (subDevFlags & SDF_PACKED) { fprintf( stderr, "subDevice using bit per channel\n" ); } else { if (subDevFlags & SDF_LSAMPL) { pBoardHard->valueIsLsampl_t = 1; fprintf( stderr, "subDevice using long word lsampl_t per channel\n" ); } else { fprintf( stderr, "subDevice using word sampl_t per channel\n" ); } } pBoardHard->buffer_size_in_byte = comedi_get_buffer_size( pBoardHard->p_comediFileHandle, pBoardHard->subDeviceNumber ); fprintf( stderr, "board[%d] hardware buffer size in BYTES=%d\n", board, pBoardHard->buffer_size_in_byte ); if (pBoardHard->buffer_size_in_byte <= 0) { fprintf( stderr, "board buffer size faile\n" ); return 1; } if (pBoardHard->valueIsLsampl_t) { pBoardHard->buffer_size_in_sample = pBoardHard->buffer_size_in_byte / sizeof(lsampl_t); } else { pBoardHard->buffer_size_in_sample = pBoardHard->buffer_size_in_byte / sizeof(sampl_t); } pBoardHard->buffer = mmap( NULL, pBoardHard->buffer_size_in_byte, PROT_READ, MAP_SHARED, comedi_fileno(pBoardHard->p_comediFileHandle), 0 ); if (pBoardHard->buffer == MAP_FAILED) { fprintf( stderr, "mmap failed for board[%d]\n", board ); return 1; } int numChannel = comedi_get_n_channels( pBoardHard->p_comediFileHandle, pBoardHard->subDeviceNumber ); if (numChannel != pBoardSoft->numberOfActiveAnalogInputs) { fprintf( stderr, "number of channel for board[%d] from comedi=%d harccode=%d\n", board, numChannel, pBoardSoft->numberOfActiveAnalogInputs ); } #ifdef SUPPORT_CHANNEL_SPECIFIC_RANGE pBoardHard->maxDataIsChannelSpecific = comedi_maxdata_is_chan_specific( pBoardHard->p_comediFileHandle, pBoardHard->subDeviceNumber ); pBoardHard->rangeIsChannelSpecific = comedi_maxdata_is_chan_specific( pBoardHard->p_comediFileHandle, pBoardHard->subDeviceNumber ); if (pBoardHard->maxDataIsChannelSpecific) { fprintf( stderr, "board[%d] supports different max data per channel\n", board ); } if (pBoardHard->rangeIsChannelSpecific) { fprintf( stderr, "board[%d] supports different range per channel\n", board ); } #endif return 0; }
int main(int argc, char *argv[]) { int i; struct board_struct *this_board; int device_status = STATUS_UNKNOWN; calibration_setup_t setup; int retval; parsed_options_t options; memset( &setup, 0, sizeof( setup ) ); setup.sv_settling_time_ns = 99999; setup.sv_order = 10; memset( &options, 0, sizeof( options ) ); options.do_dump = 0; options.do_reset = 0; options.do_calibrate = -1; options.do_results = 0; options.do_output = 1; options.file_path = "/dev/comedi0"; parse_options( argc, argv, &options ); verbose = options.verbose; setup.dev = comedi_open( options.file_path ); if( setup.dev == NULL ) { fprintf( stderr, "comedi_open() failed, with device file name: %s\n", options.file_path ); comedi_perror("comedi_open"); exit(0); } if( options.save_file_path == NULL ) options.save_file_path = comedi_get_default_calibration_path( setup.dev ); if(!options.driver_name) options.driver_name = comedi_get_driver_name( setup.dev ); if(!options.device_name) options.device_name = comedi_get_board_name( setup.dev ); setup.ad_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_AI,0); setup.da_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_AO,0); setup.caldac_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_CALIB,0); setup.eeprom_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_MEMORY,0); for(i=0;i<n_drivers;i++){ if(!strcmp(drivers[i].name,options.driver_name)){ this_board = drivers+i; goto ok; } } fprintf(stderr, "Driver %s unknown\n", options.driver_name); return 1; ok: retval = this_board->init_setup( &setup, options.device_name ); if( retval < 0 ){ fprintf(stderr, "init_setup() failed for %s\n", options.device_name ); return 1; } device_status = setup.status; if(device_status<STATUS_DONE){ printf("Warning: device may not be not fully calibrated due to " "insufficient information.\n" "Please file a bug report at https://bugs.comedi.org/ and attach this output.\n" "This output will also allow comedi_calibrate to execute more\n" "quickly in the future.\n"); if(verbose<1){ verbose=1; printf("Forcing option: --verbose\n"); } if(device_status==STATUS_UNKNOWN){ options.do_reset=1; options.do_dump=1; options.do_calibrate=0; options.do_results=0; printf("Forcing options: --reset --dump --no-calibrate --no-results\n"); } if(device_status==STATUS_SOME){ options.do_reset=1; options.do_dump=1; options.do_calibrate=1; options.do_results=1; printf("Forcing options: --reset --dump --calibrate --results\n"); } if(device_status==STATUS_GUESS){ options.do_reset=1; options.do_dump=1; options.do_calibrate=1; options.do_results=1; printf("Forcing options: --reset --dump --calibrate --results\n"); } } if(verbose>=0){ char *s = "$Id: comedi_calibrate.c,v 1.8 2008-01-11 13:29:19 abbotti Exp $"; printf("%.*s\n",(int)strlen(s)-2,s+1); printf("Driver name: %s\n", options.driver_name); printf("Device name: %s\n", options.device_name); printf("%.*s\n",(int)strlen(this_board->id)-2,this_board->id+1); printf("Comedi version: %d.%d.%d\n", (comedi_get_version_code(setup.dev)>>16)&0xff, (comedi_get_version_code(setup.dev)>>8)&0xff, (comedi_get_version_code(setup.dev))&0xff); }