コード例 #1
0
ファイル: sample2.c プロジェクト: HBNT-GitHub/AIOUSB
int main(int argc, char *argv[] )
{
    unsigned long productId, nameSize, numDIOBytes, numCounters;
    const int MAX_NAME_SIZE = 20;
    char name[ MAX_NAME_SIZE + 2 ];
    int stopval;

    unsigned long deviceIndex = 0;
    AIOUSB_BOOL deviceFound = AIOUSB_FALSE;
    printf(
           "  AIOUSB library version %s, %s\n"
           "  This program demonstrates controlling a USB-IIRO or USB-IDIO device on\n"
           "  the USB bus. For simplicity, it uses the first such device found\n"
           "  on the bus.\n"
           , AIOUSB_GetVersion(), AIOUSB_GetVersionDate()
           );

    /*
     * Must call AIOUSB_Init() before any meaningful AIOUSB functions;
     * AIOUSB_GetVersion() above is an exception
     */
    unsigned long result = AIOUSB_Init();

    /*
     * call GetDevices() to obtain "list" of devices found on the bus
     */
    unsigned long deviceMask = GetDevices();
    if( deviceMask != 0 ) {
        /*
         * at least one ACCES device detected, but we want one of a specific type
         */
        AIOUSB_ListDevices(); /* print list of all devices found on the bus */

        while( deviceMask != 0 ) {
            if( ( deviceMask & 1 ) != 0 ) {
                // found a device, but is it the correct type?
                nameSize = MAX_NAME_SIZE;
                result = QueryDeviceInfo( deviceIndex, &productId, &nameSize, name, &numDIOBytes, &numCounters );
                if( result == AIOUSB_SUCCESS ) {
                    if( (productId >= USB_IIRO_16 && productId <= USB_IIRO_4 ) || 
                        (productId >= USB_IDIO_16 && productId <= USB_IDIO_4 )) {
                        // found a USB-AI16-16A family device
                        deviceFound = AIOUSB_TRUE;
                        break;				// from while()
                    }
                } else
                    printf( "Error '%s' querying device at index %lu\n"
                            , AIOUSB_GetResultCodeAsString( result ), deviceIndex );
            }
            deviceIndex++;
            deviceMask >>= 1;
        }
    }
コード例 #2
0
ファイル: sample2.c プロジェクト: HBNT-GitHub/AIOUSB
int main( int argc, char **argv ) {
    const int DEVICES_REQUIRED = 1;
    const int MAX_DIO_BYTES = 4;
    const int MASK_BYTES = ( MAX_DIO_BYTES + BITS_PER_BYTE - 1 ) / BITS_PER_BYTE;
    const int MAX_NAME_SIZE = 20;
    int devicesFound = 0;
    int index = 0;
    typedef enum { 
      SUCCESS = 0,
      USB_ERROR = -1,
      NO_DEVICE_FOUND = -2
    } EXIT_CODE;
    EXIT_CODE exit_code = SUCCESS;

    struct DeviceInfo {
        unsigned char outputMask[ MASK_BYTES ];
        unsigned char readBuffer[ MAX_DIO_BYTES ];		// image of data read from board
        unsigned char writeBuffer[ MAX_DIO_BYTES ];		// image of data written to board
        char name[ MAX_NAME_SIZE + 2 ];
        unsigned long productID;
        unsigned long nameSize;
        unsigned long numDIOBytes;
        unsigned long numCounters;
        uint64_t serialNumber;
        int index;
    } deviceTable[ DEVICES_REQUIRED ];
    struct DeviceInfo *device;

    printf(
           "USB-DIO-32 sample program version 1.17, 26 November 2009\n"
           "  AIOUSB library version %s, %s\n"
           "  This program demonstrates communicating with %d USB-DIO-32 devices on\n"
           "  the same USB bus. For simplicity, it uses the first %d such devices\n"
           "  found on the bus.\n",
           AIOUSB_GetVersion(),
           AIOUSB_GetVersionDate(),
           DEVICES_REQUIRED,
           DEVICES_REQUIRED
           );

    unsigned long result = AIOUSB_Init(); /* Call AIOUSB_Init() before any meaningful AIOUSB functions; */
    if( result != AIOUSB_SUCCESS ) {
        printf("Can't initialize AIOUSB USB device\n");
        exit_code = USB_ERROR;
    }

    unsigned long deviceMask = GetDevices(); /**< @ref GetDevices */
    if( deviceMask == 0 ) {
        printf( "No ACCES devices found on USB bus\n" );
        exit_code = USB_ERROR;
        goto exit_sample;
    }

    AIOUSB_ListDevices();

    /*
     * search for required number of USB-DIO-32 devices
     */


    while( deviceMask != 0 && devicesFound < DEVICES_REQUIRED ) {
        if( ( deviceMask & 1 ) != 0 ) {
            // found a device, but is it the correct type?
            device = &deviceTable[ devicesFound ];
            device->nameSize = MAX_NAME_SIZE;
            result = QueryDeviceInfo( index, &device->productID,
                                      &device->nameSize,
                                      device->name,
                                      &device->numDIOBytes,
                                      &device->numCounters
                                      );
            if( result == AIOUSB_SUCCESS ) {
                if( device->productID == USB_DIO_32 ) { // found a USB-DIO-32
                    device->index = index;
                    devicesFound++;
                }
            } else
              printf( "Error '%s' querying device at index %d\n",
                      AIOUSB_GetResultCodeAsString( result ),
                      index
                      );
        }
        index++;
        deviceMask >>= 1;
    }

    if( devicesFound < DEVICES_REQUIRED ) {
        printf( "Error: You need at least %d devices connected to run this sample\n", DEVICES_REQUIRED );
        goto exit_sample;
    }
    unsigned port, pattern;
    AIOUSB_BOOL correct;

    for( index = 0; index < devicesFound; index++ ) {
        device = &deviceTable[ index ];
        result = GetDeviceSerialNumber( device->index, &device->serialNumber );
        if( result == AIOUSB_SUCCESS )
            printf( "Serial number of device at index %d: %llx\n", device->index, ( long long ) device->serialNumber );
        else
            printf( "Error '%s' getting serial number of device at index %d\n", AIOUSB_GetResultCodeAsString( result ), device->index );
    }

    /*
     * DIO configuration
     */
    device = &deviceTable[ 0 ]; /* select first device */
    AIOUSB_SetCommTimeout( device->index, 1000 ); /* set timeout for all USB operations */
    /*
     * set all ports to output mode (we could just write "device->outputMask[ 0 ] = 0x0f"
     * here since there are only 4 ports)
     */
    memset( device->outputMask, 0xff, MASK_BYTES );
    
    for( port = 0; port < device->numDIOBytes; port++ )
        device->writeBuffer[ port ] = 0x11 * ( port + 1 );
    

    /* for ( port =0 ; port < 0xff ; port ++ ) {  */
    device->outputMask[0] = (unsigned char )0xff;

    for( port = 0; port <= 0xff; port ++ ) { 
        device->writeBuffer[0] = port;
        result = DIO_ConfigureRaw( device->index, AIOUSB_FALSE, device->outputMask, device->writeBuffer );
        usleep(10000);
    }

    for( port = 0; port <= 0xff; port ++ ) { 
        device->writeBuffer[1] = port;
        result = DIO_ConfigureRaw( device->index, AIOUSB_FALSE, device->outputMask, device->writeBuffer );
        usleep(10000);
    }

    for( port = 0; port <= 0xff; port ++ ) { 
        device->writeBuffer[2] = port;
        result = DIO_ConfigureRaw( device->index, AIOUSB_FALSE, device->outputMask, device->writeBuffer );
        usleep(10000);
    }

    for( port = 0; port <= 0xff; port ++ ) { 
        device->writeBuffer[3] = port;
        result = DIO_ConfigureRaw( device->index, AIOUSB_FALSE, device->outputMask, device->writeBuffer );
        usleep(10000);
    }

    for ( port = 0; port < 4 ; port ++ )  {
        device->writeBuffer[port] = 0xff;
    }
    result = DIO_ConfigureRaw( device->index, AIOUSB_FALSE, device->outputMask, device->writeBuffer );

    for ( int i =0 ; i <= 0xf ; i ++ ) { 
            device->outputMask[0] = (unsigned char )i;
            result = DIO_ConfigureRaw( device->index, AIOUSB_FALSE, device->outputMask, device->writeBuffer );
            if ( result != AIOUSB_SUCCESS ) 
                break;
            usleep(100000);
    }

    if( result == AIOUSB_SUCCESS )
        printf( "Device at index %d successfully configured\n", device->index );
    else
        printf( "Error '%s' configuring device at index %d\n", AIOUSB_GetResultCodeAsString( result ), device->index );


exit_sample:
    AIOUSB_Exit();


    return ( int ) 0;
} 
コード例 #3
0
ファイル: burst_test.c プロジェクト: nereiras/AIOUSB
int 
main(int argc, char *argv[] ) 
{
    struct opts options = AIO_OPTIONS;
    AIOContinuousBuf *buf = 0;
    struct timespec foo , bar;

    AIORET_TYPE retval = AIOUSB_SUCCESS;
    int *indices;
    int num_devices;
    process_aio_cmd_line( &options, argc, argv );

    int tobufsize = (options.num_scans+1)*options.num_channels*20;
    uint16_t *tobuf = (uint16_t *)malloc(sizeof(uint16_t)*tobufsize);

    unsigned short *tmp = (unsigned short *)malloc(sizeof(unsigned short)*(options.num_scans+1)*options.num_channels);
    if( !tmp ) {
      fprintf(stderr,"Can't allocate memory for temporary buffer \n");
      exit(1);
    }

    AIOUSB_Init();
    AIOUSB_ListDevices();

    AIOUSB_FindDevices( &indices, &num_devices, find_ai_board );
    aio_list_devices( &options, indices, num_devices ); /* will exit if no devices found */

    buf = (AIOContinuousBuf *)NewAIOContinuousBufForCounts( options.index, options.num_scans, options.num_channels );
    if( !buf ) {
      fprintf(stderr,"Can't allocate memory for temporary buffer \n");
      exit(1);
    }

    AIOContinuousBufSetDeviceIndex( buf, options.index ); /* Assign the first matching device for this sample */

    if( options.reset ) {
        fprintf(stderr,"Resetting device at index %d\n",buf->DeviceIndex );
        AIOContinuousBufResetDevice( buf );
        exit(0);
    }
    FILE *fp = fopen(options.outfile,"w");
    if( !fp ) {
      fprintf(stderr,"Unable to open '%s' for writing\n", options.outfile );
      exit(1);
    }

    /**
     * 2. Setup the Config object for Acquisition, either the more complicated 
     *    part in comments (BELOW) or using a simple interface.
     */
    /* New simpler interface */
    AIOContinuousBufInitConfiguration( buf );

    if ( options.slow_acquire ) {
        unsigned char bufData[64];
        unsigned long bytesWritten = 0;
        GenericVendorWrite( 0, 0xDF, 0x0000, 0x001E, bufData, &bytesWritten  );
    }

    AIOContinuousBufSetOversample( buf, 0 );
    AIOContinuousBufSetStartAndEndChannel( buf, options.start_channel, options.end_channel );
    if( !options.number_ranges ) { 
        AIOContinuousBufSetAllGainCodeAndDiffMode( buf , options.gain_code , AIOUSB_FALSE );
    } else {
        for ( int i = 0; i < options.number_ranges ; i ++ ) {
            AIOContinuousBufSetChannelRange( buf, 
                                             options.ranges[i]->start_channel, 
                                             options.ranges[i]->end_channel,
                                             options.ranges[i]->gaincode
                                             );
        }
    }
    AIOContinuousBufSaveConfig(buf);
    
    if ( retval < AIOUSB_SUCCESS ) {
        printf("Error setting up configuration\n");
        exit(1);
    }
  
    options.block_size = ( options.block_size < 0 ? 1024*64 : options.block_size );
    if ( options.clock_rate < 1000 ) { 
        AIOContinuousBufSetStreamingBlockSize( buf, 512 );
    } else  {
        AIOContinuousBufSetStreamingBlockSize( buf, options.block_size );
    }

    /**
     * 3. Setup the sampling clock rate, in this case 
     *    10_000_000 / 1000
     */ 
    AIOContinuousBufSetClock( buf, options.clock_rate );
    /**
     * 4. Start the Callback that fills up the 
     *    AIOContinuousBuf. This fires up an thread that 
     *    performs the acquistion, while you go about 
     *    doing other things.
     */ 

    AIOContinuousBufInitiateCallbackAcquisition(buf);

    /**
     * in this example we read bytes in blocks of our core num_channels parameter. 
     * the channel order
     */
    if ( options.with_timing ) 
        clock_gettime( CLOCK_MONOTONIC_RAW, &bar );


    int scans_remaining;
    int read_count = 0;
    int scans_read = 0;
    while ( AIOContinuousBufGetRunStatus(buf) == RUNNING || read_count < options.num_scans ) {

        if ( (scans_remaining = AIOContinuousBufCountScansAvailable(buf) ) > 0 ) { 

            if ( scans_remaining ) { 
                if ( options.with_timing )
                    clock_gettime( CLOCK_MONOTONIC_RAW, &foo );

                scans_read = AIOContinuousBufReadIntegerScanCounts( buf, tobuf, tobufsize, AIOContinuousBufNumberChannels(buf)*AIOContinuousBufCountScansAvailable(buf) );

                if ( options.with_timing )
                    clock_gettime( CLOCK_MONOTONIC_RAW, &bar );

                read_count += scans_read;

                if ( options.verbose )
                    fprintf(stdout,"Waiting : total=%u, readpos=%d, writepos=%d, scans_read=%d\n", read_count, 
                            (int)AIOContinuousBufGetReadPosition(buf), (int)AIOContinuousBufGetWritePosition(buf), scans_read);

                for( int scan_count = 0; scan_count < scans_read ; scan_count ++ ) { 
                    if( options.with_timing )
                        fprintf(fp ,"%d,%d,", (int)bar.tv_sec, (int)(( bar.tv_sec - foo.tv_sec )*1e9 + (bar.tv_nsec - foo.tv_nsec )));
                    for( int ch = 0 ; ch < AIOContinuousBufNumberChannels(buf); ch ++ ) {
                        fprintf(fp,"%u,",tobuf[scan_count*AIOContinuousBufNumberChannels(buf)+ch] );
                        if( (ch+1) % AIOContinuousBufNumberChannels(buf) == 0 ) {
                            fprintf(fp,"\n");
                        }
                    }
                }
            }
        } else {
            usleep(100);
        }
        
    }

    fclose(fp);
    fprintf(stdout,"Test completed...exiting\n");
    AIOUSB_Exit();
    return 0;
}
コード例 #4
0
ファイル: read_channels_test.c プロジェクト: nereiras/AIOUSB
int main( int argc, char **argv ) 
{
    struct opts options = AIO_OPTIONS;
    AIORET_TYPE result = AIOUSB_SUCCESS;
    int *indices;
    int num_devices;
    ADCConfigBlock *config;
    AIOUSBDevice *dev;
    USBDevice *usb;
    double *volts;
    process_aio_cmd_line( &options, argc, argv );

    result = AIOUSB_Init();
    if ( result != AIOUSB_SUCCESS ) {
        fprintf(stderr,"Error calling AIOUSB_Init(): %d\n", (int)result );
        exit(result );
    }

    AIOUSB_ListDevices();

    process_aio_cmd_line( &options, argc, argv );

    AIOUSB_FindDevices( &indices, &num_devices, find_ai_board );
    
    if( (result = aio_list_devices( &options, indices, num_devices ) != AIOUSB_SUCCESS )) 
        exit(result);

    if ( (config = NewADCConfigBlockFromJSON( options.adcconfig_json )) == NULL )
        exit(AIOUSB_ERROR_INVALID_ADCCONFIG);

    if ( (result = aio_override_adcconfig_settings( config, &options )) != AIOUSB_SUCCESS )
        exit(result);


    /* Save the config for the device index  in question */
    dev = AIODeviceTableGetDeviceAtIndex( options.index , (AIORESULT*)&result );
    if ( result != AIOUSB_SUCCESS ) {
        fprintf(stderr,"Error getting device at index %d\n", options.index );
        exit(result);
    }

    usb = AIOUSBDeviceGetUSBHandle( dev );

    /* Copy the modified config settings back to the 
     * device ave config to the device 
     */
    result = ADCConfigBlockCopy( AIOUSBDeviceGetADCConfigBlock( dev ), config );
    result = USBDevicePutADCConfigBlock( usb, config );
    /* or do this     
     * ADC_SetConfig( options.index, config->registers, &config->size ); */

    volts = (double*)malloc((ADCConfigBlockGetEndChannel( config )-ADCConfigBlockGetStartChannel( config )+1)*sizeof(double));
    
    for ( int i = 0, channel = 0; i < options.num_scans; i ++ , channel = 0) {
        if ( options.counts ) { /* --counts will write out the raw values */
            ADC_GetScan( options.index, (unsigned short*)volts );
            unsigned short *counts = (unsigned short *)volts;
            for ( int j = ADCConfigBlockGetStartChannel( config ); j < ADCConfigBlockGetEndChannel( config ) ; j ++ , channel ++) {
                printf("%u,", counts[channel] );
            }
            printf("%u\n", counts[channel] );


        } else {
            ADC_GetScanV( options.index, volts );
            for ( int j = ADCConfigBlockGetStartChannel( config ); j < ADCConfigBlockGetEndChannel( config ) ; j ++ , channel ++) {
                printf("%.3f,", volts[channel] );
            }
            printf("%f\n", volts[channel] );
        }

    }

    return 0;
}
コード例 #5
0
ファイル: dio_sample.c プロジェクト: nereiras/AIOUSB
int main( int argc, char **argv ) 
{
    struct opts options = AIO_OPTIONS;
    AIORET_TYPE result = AIOUSB_SUCCESS;
    int *indices;
    int num_devices;
    ADCConfigBlock *config;
    AIOUSBDevice *dev;

    process_aio_cmd_line( &options, argc, argv );

    result = AIOUSB_Init();
    if ( result != AIOUSB_SUCCESS ) {
        fprintf(stderr,"Error calling AIOUSB_Init(): %d\n", (int)result );
        exit(result );
    }

    AIOUSB_ListDevices();

    process_aio_cmd_line( &options, argc, argv );

    AIOUSB_FindDevices( &indices, &num_devices, find_ai_board );
    
    if( (result = aio_list_devices( &options, indices, num_devices ) != AIOUSB_SUCCESS )) 
        exit(result);

    if ( (config = NewADCConfigBlockFromJSON( options.adcconfig_json )) == NULL )
        exit(AIOUSB_ERROR_INVALID_ADCCONFIG);

    if ( (result = aio_override_adcconfig_settings( config, &options )) != AIOUSB_SUCCESS )
        exit(result);


    /* Save the config for the device index  in question */
    dev = AIODeviceTableGetDeviceAtIndex( options.index , (AIORESULT*)&result );
    if ( result != AIOUSB_SUCCESS ) {
        fprintf(stderr,"Error getting device at index %d\n", options.index );
        exit(result);
    }

    unsigned short tmp = 0;
    unsigned short mask = 0xffff; 
    
    /**
     * @brief Make all ports outputs and 0 value as the initial tristate
     */

    printf("Configuring all ports for output\n" );
    result = DIO_Configure( options.index, AIOUSB_FALSE, &mask, &tmp );

    result = DIO_ReadAll( options.index, &tmp );     /* Verify that all the values are initially 0 */
    if ( result != AIOUSB_SUCCESS ) {
        fprintf(stderr,"Error performing DIO_ReadAll(): %d\n", result );
        exit(1);
    }
    if ( tmp != 0 )  {
        fprintf(stderr, "Error, by default all ports should have value 0, and not %u\n", tmp );
    } else {
        printf("DIO_ReadAll() gave value %u == %u(expected)\n", tmp, 0 );
    }

    for ( int i = 0 ; i < dev->DIOBytes*8 ; i ++ ) { 
        result = DIO_Write1(options.index,i, (i % 2 == 0 ? AIOUSB_FALSE : AIOUSB_TRUE ));
        if ( result != AIOUSB_SUCCESS ) {
            fprintf(stderr,"Error running on index %d , retval=%d\n", i, result );
            exit(1);
        }
    }
    printf("Successfully performed DIO_Write1()\n");
    tmp = 0;

    result = DIO_ReadAll( options.index, &tmp );
    if ( result != AIOUSB_SUCCESS ) {
        fprintf(stderr,"Error performing DIO_ReadAll(): %d\n", result );
    } else if ( tmp != 0xaaaa ) {
        fprintf(stderr,"Values expected from DIO_ReadAll() %#x != %#x\n", 0xaaaa, tmp );
    } else {
        printf("DIO_ReadAll() gave value %#x == %#x(expected)\n", tmp, 0xaaaa );
    }

    return result;
}