1 /*
   2  * $RCSfile: Sample.java,v $
   3  * $Date: 2009/11/30 21:34:08 $
   4  * $Revision: 1.11 $
   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.util.*;
  21 // }}}
  22 
  23 class Sample {
  24     public static final String VERSION = "1.11, 30 November 2009";
  25     protected static final boolean SAVE_DATA_TO_FILE = true;
  26     protected static final boolean CHECK_FOR_ZERO_DATA = true;
  27 
  28     public static void main( String args[] ) {
  29 /*API*/ USBDeviceManager deviceManager = new USBDeviceManager();
  30         System.out.println(
  31             "USB-AI16-16A sample program version: " + VERSION + "\n"
  32 /*API*/     + "  AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
  33 /*API*/     + "  AIOUSB library version: " + deviceManager.getAIOUSBVersion() + ", " + deviceManager.getAIOUSBVersionDate() + "\n"
  34             + "  JRE version: " + System.getProperty( "java.version" ) + "\n"
  35             + "  OS version: " + System.getProperty( "os.name" )
  36                     + " " + System.getProperty( "os.arch" )
  37                     + " " + System.getProperty( "os.version" ) + "\n"
  38             + "  This program demonstrates controlling a USB-AI16-16A device on\n"
  39             + "  the USB bus. For simplicity, it uses the first such device found\n"
  40             + "  on the bus."
  41         );
  42         try {
  43 /*API*/     deviceManager.open().printDevices();
  44 /*API*/     Vector<USBDevice> devices = deviceManager.getDeviceByProductID( deviceManager.USB_AI16_16A, deviceManager.USB_AI12_128E );
  45             if( devices.size() > 0 ) {
  46                 USB_AI16_Family device = ( USB_AI16_Family ) devices.get( 0 );
  47 /*API*/         device.reset().setCommTimeout( 1000 );
  48 
  49                 /*
  50                  * demonstrate reading EEPROM
  51                  */
  52                 System.out.println( "EEPROM contents:\n"
  53 /*API*/             + Arrays.toString( device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE ) ) );
  54 
  55                 /*
  56                  * demonstrate A/D configuration
  57                  */
  58 /*API*/         device.adc()
  59 /*API*/             .setGainCodeAndDiffMode( AnalogInputSubsystem.GAIN_CODE_5V, false )
  60 /*API*/             .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
  61 /*API*/             .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN )
  62 /*API*/             .setOversample( 50 )
  63 /*API*/             .setDiscardFirstSample( true );
  64                 System.out.println( "A/D successfully configured" );
  65 
  66                 /*
  67                  * demonstrate A/D calibration
  68                  */
  69                 System.out.print( "Calibrating A/D, may take a few seconds ... " );
  70 /*API*/         device.adc().calibrate( true, false, null );
  71                 System.out.println( "successful" );
  72 
  73                 /*
  74                  * demonstrate reading A/D inputs
  75                  */
  76                 int startChannel = 0
  77 /*API*/             , numChannels = device.adc().getNumChannels();
  78 /*API*/         char[] counts = device.adc().read( startChannel, numChannels );
  79                 System.out.println( "Counts read from channel " + startChannel
  80                     + " to channel " + ( startChannel + numChannels - 1 )
  81 /*API*/             + " using " + device.adc().getOversample() + " oversamples" );
  82                 for( int index = 0; index < counts.length; index++ )
  83                     System.out.println( "Channel " + ( startChannel + index ) + " = " + ( int ) counts[ index ]
  84 /*API*/                 + " (" + device.adc().countsToVolts( startChannel + index, counts[ index ] ) + " volts)" );
  85 
  86                 /*
  87                  * demonstrate bulk A/D reads
  88                  */
  89                 BufferedOutputStream output = null;
  90                 if( SAVE_DATA_TO_FILE )
  91                     output = new BufferedOutputStream( new FileOutputStream( new File( "bulkdata" ) ) );
  92                 int numSamples = 1000000
  93                     , samplesReceived = 0;
  94 /*API*/         device.adc()
  95 /*API*/             .setStreamingBlockSize( 100000 )
  96 /*API*/             .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
  97 /*API*/             .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN | AnalogInputSubsystem.TRIG_MODE_TIMER )
  98 /*API*/             .setClock( 100000 )
  99 /*API*/             .readBulkStart( 1, 1, numSamples );
 100                 do {
 101                     Thread.sleep( 100 );
 102 /*API*/             char[] data = device.adc().readBulkNext( 20000 );
 103                     if( data.length > 0 ) {
 104                         if( SAVE_DATA_TO_FILE ) {
 105                             for( int index = 0; index < data.length; index++ ) {
 106                                 output.write( data[ index ] & 0xff );
 107                                 output.write( ( data[ index ] >> 8 ) & 0xff );
 108                             }   // for( int index ...
 109                         }   // if( SAVE_DATA_TO_FILE )
 110 
 111                         if( CHECK_FOR_ZERO_DATA ) {
 112                             /*
 113                              * on the bipolar ranges, with nothing connected to the inputs, the
 114                              * readings should be around half of full scale, far from zero; if
 115                              * any readings are zero, it likely indicates some sort of serious
 116                              * hardware, firmware or software problem; print one warning per block
 117                              * to avoid flooding the screen with messages
 118                              */
 119                             for( int index = 0; index < data.length; index++ ) {
 120                                 if( data[ index ] == 0 ) {
 121                                     System.err.println( "Warning: zero data at index " + ( samplesReceived + index ) );
 122                                     break;      // from for(); only one warning per block
 123                                 }   // if( data[ ...
 124                             }   // for( int index ...
 125                         }   // if( CHECK_FOR_ZERO_DATA )
 126 
 127                         samplesReceived += data.length;
 128                         numSamples -= data.length;
 129                         System.out.println( "Bulk read " + samplesReceived + " samples, " + numSamples + " remaining" );
 130                         if( numSamples <= 0 )
 131                             break;              // from do()
 132                     }   // if( data.length ...
 133                 } while( true );
 134 /*API*/         device.adc().setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN );
 135                 if( SAVE_DATA_TO_FILE )
 136                     output.close();
 137             }   // if( devices.size() ...
 138 /*API*/     deviceManager.close();
 139         } catch( Exception ex ) {
 140             System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
 141 /*API*/     if( deviceManager.isOpen() )
 142 /*API*/         deviceManager.close();
 143         }   // catch( ...
 144     }   // main()
 145 
 146 }   // class Sample
 147 
 148 /* end of file */