1 /*
   2  * $RCSfile: Sample.java,v $
   3  * $Date: 2010/01/29 18:34:04 $
   4  * $Revision: 1.3 $
   5  * jEdit:tabSize=4:indentSize=4:collapseFolds=1:
   6  */
   7 
   8 // {{{ build instructions
   9 /*
  10  * to compile:
  11  *   javac -cp ../../java/aiousb.jar Sample.java
  12  * to run:
  13  *   java -cp ../../java/aiousb.jar:. Sample
  14  */
  15 // }}}
  16 
  17 // {{{ imports
  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;    // Hz
  26     private static int NUM_POINTS = 1024000;        // must be multiple of 256
  27 
  28     public static void main( String args[] ) {
  29         new Sample();
  30     }   // main()
  31 
  32     public Sample() {
  33         USBDeviceManager deviceManager = null;
  34         try {
  35 /*API*/     deviceManager = new USBDeviceManager();
  36 /*API*/     deviceManager.open();
  37             System.out.println(
  38                   "USB-DIO-16A sample program version: " + VERSION + "\n"
  39 /*API*/         + "  AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
  40 /*API*/         + "  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 /*API*/     deviceManager.printDevices();
  50 /*API*/     USBDevice[] devices = deviceManager.getDeviceByProductID( USBDeviceManager.USB_DIO_16A );
  51             if( devices.length >= 2 ) {
  52                 /*
  53                  * use first two devices found
  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 /*API*/         DIOStreamSubsystem sendStream = sendDevice.diostream();
  58 /*API*/         DIOStreamSubsystem receiveStream = receiveDevice.diostream();
  59 
  60                 /*
  61                  * set up sending device
  62                  */
  63 /*API*/         sendDevice.setCommTimeout( 1000 );
  64 /*API*/         sendStream.setStreamingBlockSize( 256 );
  65 /*API*/         sendStream.setClock( false, CLOCK_SPEED );
  66 /*API*/         sendStream.open( false );
  67 /*API*/         boolean[] tristates = new boolean[ sendDevice.dio().getNumTristateGroups() ];
  68                 Arrays.fill( tristates, false );        // all ports are not in tristate mode
  69 /*API*/         boolean[] outputs = new boolean[ sendDevice.dio().getNumPorts() ];
  70                 Arrays.fill( outputs, true );           // all ports except last one are output
  71                 outputs[ outputs.length - 1 ] = false;  // last port is input
  72 /*API*/         boolean[] values = new boolean[ sendDevice.dio().getNumChannels() ];
  73                 Arrays.fill( values, true );
  74 /*API*/         sendDevice.dio().configure( tristates, outputs, values );
  75 
  76                 /*
  77                  * set up receiving device
  78                  */
  79 /*API*/         receiveDevice.setCommTimeout( 1000 );
  80 /*API*/         receiveStream.setStreamingBlockSize( 256 );
  81 /*API*/         receiveStream.stopClock();
  82 /*API*/         receiveStream.open( true );
  83                 Arrays.fill( outputs, false );          // all ports except last one are input
  84                 outputs[ outputs.length - 1 ] = true;   // last port is output
  85 /*API*/         receiveDevice.dio().configure( tristates, outputs, values );
  86                 ReceiveThread receiveThread = new ReceiveThread( receiveStream, NUM_POINTS );
  87                 receiveThread.start();
  88 
  89                 /*
  90                  * start sending
  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                  * give receiver thread a little time to finish receiving the data and then check data;
 103                  * with the default settings it should take about one second to transmit the data; using
 104                  * a fixed delay here is obviously crude, but it's merely for demonstration purposes
 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                  * tidy up
 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 /*API*/     deviceManager.close();
 126         } catch( Exception ex ) {
 127             System.out.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
 128             if(
 129                 deviceManager != null
 130 /*API*/         && deviceManager.isOpen()
 131             )
 132 /*API*/         deviceManager.close();
 133         }   // catch( ...
 134     }   // Sample()
 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         }   // ReceiveThread()
 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             }   // catch( ...
 152         }   // run()
 153 
 154         public char[] getData() {
 155             return data;
 156         }   // getData()
 157 
 158     }   // class ReceiveThread
 159 
 160 }   // class Sample
 161 
 162 /* end of file */