1 /*
   2  * $RCSfile: Sample.java,v $
   3  * $Date: 2009/12/25 18:44:28 $
   4  * $Revision: 1.22 $
   5  * jEdit:tabSize=4:indentSize=4:collapseFolds=1:
   6  */
   7 
   8 // {{{ build instructions
   9 /*
  10  * to compile:
  11  *   javac -cp ../../java/aiousb.jar Sample.java
  12  * to run:
  13  *   java -cp ../../java/aiousb.jar:. Sample
  14  */
  15 // }}}
  16 
  17 // {{{ imports
  18 import com.acces.aiousb.*;
  19 import java.io.*;
  20 import java.text.*;
  21 import java.util.*;
  22 // }}}
  23 
  24 class Sample {
  25     public static final String VERSION = "1.22, 25 December 2009";
  26     protected static final boolean SAVE_DATA_TO_FILE = true;
  27     protected static final boolean CHECK_FOR_ZERO_DATA = true;
  28 
  29     public static void main( String args[] ) {
  30         USBDeviceManager deviceManager = null;
  31         try {
  32 /*API*/     deviceManager = new USBDeviceManager();
  33 /*API*/     deviceManager.open();
  34             System.out.println(
  35                   "USB-AI16-16A sample program version: " + VERSION + "\n"
  36 /*API*/         + "  AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
  37 /*API*/         + "  AIOUSB library version: " + deviceManager.getAIOUSBVersion() + ", " + deviceManager.getAIOUSBVersionDate() + "\n"
  38                 + "  JRE version: " + System.getProperty( "java.version" ) + "\n"
  39                 + "  OS version: " + System.getProperty( "os.name" )
  40                     + " " + System.getProperty( "os.arch" )
  41                     + " " + System.getProperty( "os.version" ) + "\n"
  42                 + "  This program demonstrates controlling a USB-AI16-16A family device on\n"
  43                 + "  the USB bus. For simplicity, it uses the first such device found\n"
  44                 + "  on the bus and supports these product IDs:"
  45 /*API*/         + Arrays.toString( USB_AI16_Family.getSupportedProductNames() )
  46             );
  47 /*API*/     deviceManager.printDevices();
  48 /*API*/     USBDevice[] devices = deviceManager.getDeviceByProductID( USB_AI16_Family.getSupportedProductIDs() );
  49             if( devices.length > 0 ) {
  50                 USB_AI16_Family device = ( USB_AI16_Family ) devices[ 0 ];  // get first device found
  51 /*API*/         device.reset()
  52 /*API*/             .setCommTimeout( 1000 );
  53 
  54                 /*
  55                  * demonstrate reading EEPROM
  56                  */
  57                 System.out.println( "EEPROM contents:\n"
  58 /*API*/             + Arrays.toString( device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE ) ) );
  59 
  60                 /*
  61                  * demonstrate A/D configuration
  62                  */
  63 /*API*/         device.adc()
  64 /*API*/             .setRangeAndDiffMode( AnalogInputSubsystem.RANGE_5V, false )
  65 /*API*/             .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
  66 /*API*/             .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN )
  67 /*API*/             .setOverSample( 50 )
  68 /*API*/             .setDiscardFirstSample( true );
  69                 System.out.println( "A/D successfully configured" );
  70 
  71                 /*
  72                  * demonstrate A/D calibration
  73                  */
  74 /*API*/         if( device.adc().isAutoCalPresent( false ) ) {
  75                     System.out.print( "Calibrating A/D, may take a few seconds ... " );
  76 /*API*/             device.adc().calibrate( true, false, null );
  77                     System.out.println( "successful" );
  78                 }   // if( device.adc() ...
  79 
  80                 /*
  81                  * demonstrate reading A/D inputs
  82                  */
  83                 int startChannel = 0
  84 /*API*/             , numChannels = device.adc().getNumMUXChannels();
  85 /*API*/         char[] counts = device.adc().readCounts( startChannel, numChannels );
  86                 System.out.println( "Read from channel " + startChannel
  87                     + " to channel " + ( startChannel + numChannels - 1 )
  88 /*API*/             + " using " + device.adc().getOverSample() + " oversamples:" );
  89                 DecimalFormat voltageFormat = new DecimalFormat( "0.0000000" );
  90                 for( int index = 0; index < counts.length; index++ )
  91                     System.out.println( "  Channel " + ( startChannel + index ) + " = " + ( int ) counts[ index ]
  92 /*API*/                 + " (" + voltageFormat.format( device.adc().countsToVolts( startChannel + index, counts[ index ] ) ) + " volts)" );
  93 
  94                 /*
  95                  * demonstrate reading A/D data set
  96                  */
  97 /*API*/         AI16_DataSet dataSet = device.adc().read( startChannel, numChannels );
  98 /*API*/         dataSet.print( System.out );
  99 
 100                 /*
 101                  * demonstrate bulk A/D reads
 102                  */
 103                 BufferedOutputStream output = null;
 104                 if( SAVE_DATA_TO_FILE )
 105                     output = new BufferedOutputStream( new FileOutputStream( new File( "bulkdata" ) ) );
 106                 int numSamples = 1000000
 107                     , samplesReceived = 0;
 108 /*API*/         device.adc()
 109 /*API*/             .setStreamingBlockSize( 100000 )
 110 /*API*/             .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
 111 /*API*/             .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN | AnalogInputSubsystem.TRIG_MODE_TIMER )
 112 /*API*/             .setClock( 1000000 )
 113 /*API*/             .readBulkStart( 1, 1, numSamples );
 114                 do {
 115                     Thread.sleep( 100 );
 116 /*API*/             char[] data = device.adc().readBulkNext( 4000 );
 117                     if( data.length > 0 ) {
 118                         if( SAVE_DATA_TO_FILE ) {
 119                             for( int index = 0; index < data.length; index++ ) {
 120                                 output.write( data[ index ] & 0xff );
 121                                 output.write( ( data[ index ] >> 8 ) & 0xff );
 122                             }   // for( int index ...
 123                         }   // if( SAVE_DATA_TO_FILE )
 124 
 125                         if( CHECK_FOR_ZERO_DATA ) {
 126                             /*
 127                              * on the bipolar ranges, with nothing connected to the inputs, the
 128                              * count values should be around half of full scale, far from zero; if
 129                              * any count values are zero, it likely indicates some sort of serious
 130                              * hardware, firmware or software problem; print one warning per block
 131                              * to avoid flooding the screen with messages
 132                              */
 133                             for( int index = 0; index < data.length; index++ ) {
 134                                 if( data[ index ] == 0 ) {
 135                                     System.err.println( "Warning: zero data at index " + ( samplesReceived + index ) );
 136                                     break;      // from for(); only one warning per block
 137                                 }   // if( data[ ...
 138                             }   // for( int index ...
 139                         }   // if( CHECK_FOR_ZERO_DATA )
 140 
 141                         samplesReceived += data.length;
 142                         numSamples -= data.length;
 143                         System.out.println( "Bulk read " + samplesReceived + " samples, " + numSamples + " remaining" );
 144                         if( numSamples <= 0 )
 145                             break;              // from do()
 146                     }   // if( data.length ...
 147                 } while( true );
 148 /*API*/         device.adc().setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN );
 149                 if( SAVE_DATA_TO_FILE )
 150                     output.close();
 151             } else
 152                 System.out.println( "No USB-AI16-16A devices found on USB bus" );
 153 /*API*/     deviceManager.close();
 154         } catch( Exception ex ) {
 155             System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
 156             if(
 157                 deviceManager != null
 158 /*API*/         && deviceManager.isOpen()
 159             )
 160 /*API*/         deviceManager.close();
 161         }   // catch( ...
 162     }   // main()
 163 }   // class Sample
 164 
 165 /* end of file */