1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 deviceManager = new USBDeviceManager();
33 deviceManager.open();
34 System.out.println(
35 "USB-AI16-16A sample program version: " + VERSION + "\n"
36 + " AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
37 + " 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 + Arrays.toString( USB_AI16_Family.getSupportedProductNames() )
46 );
47 deviceManager.printDevices();
48 USBDevice[] devices = deviceManager.getDeviceByProductID( USB_AI16_Family.getSupportedProductIDs() );
49 if( devices.length > 0 ) {
50 USB_AI16_Family device = ( USB_AI16_Family ) devices[ 0 ];
51 device.reset()
52 .setCommTimeout( 1000 );
53
54
55
56
57 System.out.println( "EEPROM contents:\n"
58 + Arrays.toString( device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE ) ) );
59
60
61
62
63 device.adc()
64 .setRangeAndDiffMode( AnalogInputSubsystem.RANGE_5V, false )
65 .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
66 .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN )
67 .setOverSample( 50 )
68 .setDiscardFirstSample( true );
69 System.out.println( "A/D successfully configured" );
70
71
72
73
74 if( device.adc().isAutoCalPresent( false ) ) {
75 System.out.print( "Calibrating A/D, may take a few seconds ... " );
76 device.adc().calibrate( true, false, null );
77 System.out.println( "successful" );
78 }
79
80
81
82
83 int startChannel = 0
84 , numChannels = device.adc().getNumMUXChannels();
85 char[] counts = device.adc().readCounts( startChannel, numChannels );
86 System.out.println( "Read from channel " + startChannel
87 + " to channel " + ( startChannel + numChannels - 1 )
88 + " 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 + " (" + voltageFormat.format( device.adc().countsToVolts( startChannel + index, counts[ index ] ) ) + " volts)" );
93
94
95
96
97 AI16_DataSet dataSet = device.adc().read( startChannel, numChannels );
98 dataSet.print( System.out );
99
100
101
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 device.adc()
109 .setStreamingBlockSize( 100000 )
110 .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
111 .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN | AnalogInputSubsystem.TRIG_MODE_TIMER )
112 .setClock( 1000000 )
113 .readBulkStart( 1, 1, numSamples );
114 do {
115 Thread.sleep( 100 );
116 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 }
123 }
124
125 if( CHECK_FOR_ZERO_DATA ) {
126
127
128
129
130
131
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;
137 }
138 }
139 }
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;
146 }
147 } while( true );
148 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 deviceManager.close();
154 } catch( Exception ex ) {
155 System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
156 if(
157 deviceManager != null
158 && deviceManager.isOpen()
159 )
160 deviceManager.close();
161 }
162 }
163 }
164
165