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.1, 29 January 2010";
25
26 public static void main( String args[] ) {
27 USBDeviceManager deviceManager = null;
28 try {
29 deviceManager = new USBDeviceManager();
30 deviceManager.open();
31 System.out.println(
32 "USB-DA12-8A sample program version: " + VERSION + "\n"
33 + " AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
34 + " AIOUSB library version: " + deviceManager.getAIOUSBVersion() + ", " + deviceManager.getAIOUSBVersionDate() + "\n"
35 + " JRE version: " + System.getProperty( "java.version" ) + "\n"
36 + " OS version: " + System.getProperty( "os.name" )
37 + " " + System.getProperty( "os.arch" )
38 + " " + System.getProperty( "os.version" ) + "\n"
39 + " This program demonstrates controlling a USB-DA12-8A device on\n"
40 + " the USB bus. For simplicity, it uses the first such device found\n"
41 + " on the bus."
42 );
43 deviceManager.printDevices();
44 USBDevice[] devices = deviceManager.getDeviceByProductID( USBDeviceManager.USB_DA12_8A_REV_A, USBDeviceManager.USB_DA12_8A );
45 if( devices.length > 0 ) {
46 USB_DA12_8A_Family device = ( USB_DA12_8A_Family ) devices[ 0 ];
47 device.setCommTimeout( 1000 );
48
49
50
51
52 System.out.println( "Found device \'" + device.getName() + "\' with serial number " + Long.toHexString( device.getSerialNumber() ) );
53
54
55
56
57 final int numChannels = device.dac().getNumChannels();
58 final int TEST_CHANNEL = 0;
59 final char counts = DA12_AnalogOutputSubsystem.MAX_COUNTS / 2;
60 device.dac().writeCounts( TEST_CHANNEL, counts );
61 System.out.println( ( int ) counts + " D/A counts successfully output to channel " + TEST_CHANNEL );
62
63
64
65
66 char[] countPoints = new char[ numChannels * 2 ];
67 for( int channel = 0; channel < numChannels; channel++ ) {
68 countPoints[ channel * 2 ] = ( char ) channel;
69 countPoints[ channel * 2 + 1 ] = ( char ) ( ( channel + 1 ) * DA12_AnalogOutputSubsystem.MAX_COUNTS / numChannels );
70 }
71 device.dac().writeCounts( countPoints );
72 System.out.println( "Multiple D/A counts successfully output to " + numChannels + " channels" );
73
74
75
76
77
78
79
80 final int HARDWARE_RANGE = DA12_AnalogOutputSubsystem.RANGE_10V;
81 final double volts = 5;
82 device.dac().setRange( HARDWARE_RANGE );
83 device.dac().writeVolts( TEST_CHANNEL, volts );
84 System.out.println( volts + " volts (" + ( int ) device.dac().voltsToCounts( TEST_CHANNEL, volts ) + " D/A counts)"
85 + " successfully output to channel " + TEST_CHANNEL );
86
87
88
89
90 OutputVoltagePoint[] voltPoints = new OutputVoltagePoint[ numChannels ];
91 for( int channel = 0; channel < numChannels; channel++ )
92 voltPoints[ channel ] = new OutputVoltagePoint( channel, ( channel + 1 ) * 10.0 / numChannels );
93 device.dac().writeVolts( voltPoints );
94 System.out.println( "Multiple volts successfully output to " + numChannels + " channels" );
95 } else
96 System.out.println( "No USB-DA12-8A devices found on USB bus" );
97 deviceManager.close();
98 } catch( Exception ex ) {
99 System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
100 if(
101 deviceManager != null
102 && deviceManager.isOpen()
103 )
104 deviceManager.close();
105 }
106 }
107 }
108
109