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.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 USBDeviceManager deviceManager = new USBDeviceManager();
30 System.out.println(
31 "USB-AI16-16A sample program version: " + VERSION + "\n"
32 + " AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
33 + " 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 deviceManager.open().printDevices();
44 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 device.reset().setCommTimeout( 1000 );
48
49
50
51
52 System.out.println( "EEPROM contents:\n"
53 + Arrays.toString( device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE ) ) );
54
55
56
57
58 device.adc()
59 .setGainCodeAndDiffMode( AnalogInputSubsystem.GAIN_CODE_5V, false )
60 .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
61 .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN )
62 .setOversample( 50 )
63 .setDiscardFirstSample( true );
64 System.out.println( "A/D successfully configured" );
65
66
67
68
69 System.out.print( "Calibrating A/D, may take a few seconds ... " );
70 device.adc().calibrate( true, false, null );
71 System.out.println( "successful" );
72
73
74
75
76 int startChannel = 0
77 , numChannels = device.adc().getNumChannels();
78 char[] counts = device.adc().read( startChannel, numChannels );
79 System.out.println( "Counts read from channel " + startChannel
80 + " to channel " + ( startChannel + numChannels - 1 )
81 + " 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 + " (" + device.adc().countsToVolts( startChannel + index, counts[ index ] ) + " volts)" );
85
86
87
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 device.adc()
95 .setStreamingBlockSize( 100000 )
96 .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
97 .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN | AnalogInputSubsystem.TRIG_MODE_TIMER )
98 .setClock( 100000 )
99 .readBulkStart( 1, 1, numSamples );
100 do {
101 Thread.sleep( 100 );
102 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 }
109 }
110
111 if( CHECK_FOR_ZERO_DATA ) {
112
113
114
115
116
117
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;
123 }
124 }
125 }
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;
132 }
133 } while( true );
134 device.adc().setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN );
135 if( SAVE_DATA_TO_FILE )
136 output.close();
137 }
138 deviceManager.close();
139 } catch( Exception ex ) {
140 System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
141 if( deviceManager.isOpen() )
142 deviceManager.close();
143 }
144 }
145
146 }
147
148