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.4, 25 December 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 = null;
34 try {
35 deviceManager = new USBDeviceManager();
36 deviceManager.open();
37 System.out.println(
38 "USB-IIRO-16 sample program version: " + VERSION + "\n"
39 + " AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
40 + " AIOUSB library version: " + deviceManager.getAIOUSBVersion() + ", " + deviceManager.getAIOUSBVersionDate() + "\n"
41 + " JRE version: " + System.getProperty( "java.version" ) + "\n"
42 + " OS version: " + System.getProperty( "os.name" )
43 + " " + System.getProperty( "os.arch" )
44 + " " + System.getProperty( "os.version" ) + "\n"
45 + " This program demonstrates controlling a USB-IIRO-16 device on\n"
46 + " the USB bus. For simplicity, it uses the first such device found\n"
47 + " on the bus."
48 );
49 deviceManager.printDevices();
50 USBDevice[] devices = deviceManager.getDeviceByProductID( USBDeviceManager.USB_IIRO_16 );
51 if( devices.length > 0 ) {
52 USB_DIO_Family device = ( USB_DIO_Family ) devices[ 0 ];
53 device.reset()
54 .setCommTimeout( 1000 );
55
56
57
58
59 if( DEMO_EEPROM_WRITE ) {
60 System.out.print( "Writing to EEPROM ... " );
61 final int offset = 0x100;
62 String message = "USB-IIRO-16 sample program";
63 byte[] prevData = device.customEEPROMRead( offset, message.length() );
64 device.setCommTimeout( 10000 )
65 .customEEPROMWrite( offset, message.getBytes() );
66 String readMessage = new String( device.customEEPROMRead( offset, message.length() ) );
67 device.customEEPROMWrite( offset, prevData )
68 .setCommTimeout( 1000 );
69 System.out.println(
70 readMessage.equals( message )
71 ? "successful"
72 : "failed: data read back did not compare" );
73 }
74
75
76
77
78 System.out.println( "EEPROM contents:\n"
79 + Arrays.toString( device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE ) ) );
80
81
82
83
84 System.out.print( "Configuring digital I/O ... " );
85 final int numPorts = device.dio().getNumPorts();
86 final int numOutputPorts = numPorts / 2;
87 final int numChannels = device.dio().getNumChannels();
88 final int numOutputChannels = numChannels / 2;
89 final int numInputChannels = numChannels - numOutputChannels;
90 boolean[] outputs = new boolean[ numPorts ];
91 Arrays.fill( outputs, 0, numOutputPorts, true );
92 Arrays.fill( outputs, numOutputPorts, numPorts, false );
93 boolean[] values = new boolean[ numOutputChannels ];
94 Arrays.fill( values, true );
95 device.dio().configure( false, outputs, values );
96 System.out.println( "successful" );
97
98
99
100
101 System.out.println(
102 "In the following demonstrations, you should hear the relays click\n"
103 + "and see the LED on the device blink."
104 );
105 System.out.print( " CLosing relays:" );
106 for( int channel = 0; channel < numOutputChannels; channel++ ) {
107 System.out.print( " " + channel );
108 device.dio().write( channel, false );
109 Thread.sleep( 100 );
110 }
111 System.out.print( "\n Opening relays:" );
112 for( int channel = 0; channel < numOutputChannels; channel++ ) {
113 System.out.print( " " + channel );
114 device.dio().write( channel, true );
115 Thread.sleep( 100 );
116 }
117 System.out.println();
118
119
120
121
122 System.out.print( "Closing all relays ... " );
123 Arrays.fill( values, false );
124 device.dio().write( 0, values );
125 System.out.println( "successful" );
126 Thread.sleep( 2000 );
127 System.out.print( "Opening all relays ... " );
128 Arrays.fill( values, true );
129 device.dio().write( 0, values );
130 System.out.println( "successful" );
131
132
133
134
135 Thread.sleep( 1000 );
136 System.out.print( "CLosing every other relay:" );
137 for( int channel = 0; channel < numOutputChannels; channel += 2 ) {
138 System.out.print( " " + channel );
139 device.dio().write( channel, false );
140 Thread.sleep( 100 );
141 }
142 System.out.println( "\nReading back relays:" );
143 for( int channel = 0; channel < numOutputChannels; channel++ ) {
144 System.out.println( " Channel " + channel + " = " + device.dio().read( channel ) );
145 }
146 System.out.println( "Reading isolated inputs:" );
147 for( int channel = 0; channel < numInputChannels; channel++ ) {
148 System.out.println( " Channel " + ( numOutputChannels + channel ) + " = " + device.dio().read( numOutputChannels + channel ) );
149 }
150
151
152
153
154 Arrays.fill( values, true );
155 device.dio().write( 0, values );
156 } else
157 System.out.println( "No USB-IIRO-16 devices found on USB bus" );
158 deviceManager.close();
159 } catch( Exception ex ) {
160 System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
161 if(
162 deviceManager != null
163 && deviceManager.isOpen()
164 )
165 deviceManager.close();
166 }
167 }
168 }
169
170