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, 30 November 2009";
25
26
27
28
29
30 protected static final boolean DEMO_EEPROM_WRITE = false;
31
32 public static void main( String args[] ) {
33 USBDeviceManager deviceManager = new USBDeviceManager();
34 System.out.println(
35 "USB-DIO-32 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-DIO-32 device on\n"
43 + " the USB bus. For simplicity, it uses the first such device found\n"
44 + " on the bus."
45 );
46 try {
47 deviceManager.open().printDevices();
48 Vector<USBDevice> devices = deviceManager.getDeviceByProductID( deviceManager.USB_DIO_32 );
49 if( devices.size() > 0 ) {
50 USB_DIO_32_Family device = ( USB_DIO_32_Family ) devices.get( 0 );
51 device.reset()
52 .setCommTimeout( 1000 );
53
54
55
56
57 if( DEMO_EEPROM_WRITE ) {
58 System.out.print( "Writing to EEPROM ... " );
59 final int offset = 0x100;
60 String message = "USB-DIO-32 sample program";
61 byte[] prevData = device.customEEPROMRead( offset, message.length() );
62 device.setCommTimeout( 10000 )
63 .customEEPROMWrite( offset, message.getBytes() );
64 String readMessage = new String( device.customEEPROMRead( offset, message.length() ) );
65 device.customEEPROMWrite( offset, prevData )
66 .setCommTimeout( 1000 );
67 System.out.println(
68 readMessage.equals( message )
69 ? "successful"
70 : "failed: data read back did not compare" );
71 }
72
73
74
75
76 System.out.println( "EEPROM contents:\n"
77 + Arrays.toString( device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE ) ) );
78
79
80
81
82 System.out.print( "Configuring digital I/O ... " );
83 boolean[] outputs = new boolean[ device.dio().getNumPorts() ];
84 Arrays.fill( outputs, true );
85 boolean[] values = new boolean[ device.dio().getNumChannels() ];
86 Arrays.fill( values, false );
87 device.dio().configure( false, outputs, values );
88 System.out.println( "successful" );
89
90
91
92
93 System.out.print( "Turning all outputs on:" );
94 for( int channel = 0; channel < device.dio().getNumChannels(); channel++ ) {
95 System.out.print( " " + channel );
96 device.dio().write( channel, true );
97 Thread.sleep( 100 );
98 }
99 System.out.print( "\nTurning all outputs off:" );
100 for( int channel = 0; channel < device.dio().getNumChannels(); channel++ ) {
101 System.out.print( " " + channel );
102 device.dio().write( channel, false );
103 Thread.sleep( 100 );
104 }
105 System.out.println();
106
107
108
109
110 System.out.print( "Turning all outputs on ... " );
111 Arrays.fill( values, true );
112 device.dio().write( 0, values );
113 System.out.println( "successful" );
114 Thread.sleep( 2000 );
115 System.out.print( "Turning all outputs off ... " );
116 Arrays.fill( values, false );
117 device.dio().write( 0, values );
118 System.out.println( "successful" );
119 } else
120 System.out.println( "No USB-DIO-32 devices found on USB bus" );
121 deviceManager.close();
122 } catch( Exception ex ) {
123 System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
124 if( deviceManager.isOpen() )
125 deviceManager.close();
126 }
127 }
128
129 }
130
131