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.3, 29 January 2010";
25 private static double CLOCK_SPEED = 1000000;
26 private static int NUM_POINTS = 1024000;
27
28 public static void main( String args[] ) {
29 new Sample();
30 }
31
32 public Sample() {
33 USBDeviceManager deviceManager = null;
34 try {
35 deviceManager = new USBDeviceManager();
36 deviceManager.open();
37 System.out.println(
38 "USB-DIO-16A 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 high speed streaming between 2 USB-DIO-16A\n"
46 + " devices on the same USB bus. For simplicity, it uses the first 2 such\n"
47 + " devices found on the bus."
48 );
49 deviceManager.printDevices();
50 USBDevice[] devices = deviceManager.getDeviceByProductID( USBDeviceManager.USB_DIO_16A );
51 if( devices.length >= 2 ) {
52
53
54
55 USB_DIO_16_Family sendDevice = ( USB_DIO_16_Family ) devices[ 0 ];
56 USB_DIO_16_Family receiveDevice = ( USB_DIO_16_Family ) devices[ 1 ];
57 DIOStreamSubsystem sendStream = sendDevice.diostream();
58 DIOStreamSubsystem receiveStream = receiveDevice.diostream();
59
60
61
62
63 sendDevice.setCommTimeout( 1000 );
64 sendStream.setStreamingBlockSize( 256 );
65 sendStream.setClock( false, CLOCK_SPEED );
66 sendStream.open( false );
67 boolean[] tristates = new boolean[ sendDevice.dio().getNumTristateGroups() ];
68 Arrays.fill( tristates, false );
69 boolean[] outputs = new boolean[ sendDevice.dio().getNumPorts() ];
70 Arrays.fill( outputs, true );
71 outputs[ outputs.length - 1 ] = false;
72 boolean[] values = new boolean[ sendDevice.dio().getNumChannels() ];
73 Arrays.fill( values, true );
74 sendDevice.dio().configure( tristates, outputs, values );
75
76
77
78
79 receiveDevice.setCommTimeout( 1000 );
80 receiveStream.setStreamingBlockSize( 256 );
81 receiveStream.stopClock();
82 receiveStream.open( true );
83 Arrays.fill( outputs, false );
84 outputs[ outputs.length - 1 ] = true;
85 receiveDevice.dio().configure( tristates, outputs, values );
86 ReceiveThread receiveThread = new ReceiveThread( receiveStream, NUM_POINTS );
87 receiveThread.start();
88
89
90
91
92 char[] sendData = new char[ NUM_POINTS ];
93 for( int point = 0; point < NUM_POINTS; point++ )
94 sendData[ point ] = ( char ) ( point & 0xffff );
95 final int samplesSent = sendStream.write( sendData );
96 if( samplesSent == sendData.length )
97 System.out.println( "Successfully sent " + sendData.length + " samples" );
98 else
99 System.out.println( "Error: sent " + sendData.length + " samples, but actual was " + samplesSent + " samples" );
100
101
102
103
104
105
106 System.out.println( "Waiting for data to be received ..." );
107 Thread.sleep( 3000 );
108 char[] receiveData = receiveThread.getData();
109 if(
110 receiveData != null
111 && Arrays.equals( receiveData, sendData )
112 )
113 System.out.println( "Successfully received " + receiveData.length + " samples" );
114 else
115 System.out.println( "Error: data received did not match data sent" );
116
117
118
119
120 sendStream.stopClock();
121 sendStream.close();
122 receiveStream.close();
123 } else
124 System.out.println( "Failed to find 2 USB-DIO-16A devices" );
125 deviceManager.close();
126 } catch( Exception ex ) {
127 System.out.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
128 if(
129 deviceManager != null
130 && deviceManager.isOpen()
131 )
132 deviceManager.close();
133 }
134 }
135
136 private class ReceiveThread extends Thread {
137 DIOStreamSubsystem stream;
138 int numPoints;
139 char[] data = null;
140
141 public ReceiveThread( DIOStreamSubsystem stream, int numPoints ) {
142 this.stream = stream;
143 this.numPoints = numPoints;
144 }
145
146 public void run() {
147 try {
148 data = stream.read( numPoints );
149 } catch( Exception ex ) {
150 System.out.println( "Error \'" + ex.toString() + "\' occurred while receiving from DIO stream" );
151 }
152 }
153
154 public char[] getData() {
155 return data;
156 }
157
158 }
159
160 }
161
162