Thursday, May 6, 2010

Java USB API

Access USB devices from Java applications for Windows XPExample for Freescale JM60 (AN3164) with Eclipse: SoucerForge
  • Set the descriptors in the firmware:





  • Generate INF with WinJavaInf.exe
  • Programming the microcontroller and connect.
  • Install Driver.


  • Create New Java Project in Eclipse

  • Copy "usbJava.jar in folder project and Add to Build Path.


  • Create new Class:
import com.lucasF.JCommUSB_2_0.USBDevice;

public class UsbJava {

public static void main(String[] args) {

String szPath = "";
try{
szPath = USBDevice.getAttachedDevicePath(0);

System.out.println("Device Path of Device at index 0 = "+ szPath);
USBDevice myUSB = new USBDevice(szPath);

System.out.println("connected?"+myUSB.deviceIsConnected());

}catch (Exception e) {
e.printStackTrace();
}
}
}




USBDevice class sentence for simple read-write :

writePipeBulkInterrupt(int IntIndex,int EndpointIndex, byte[] bufferData,int iArrayOffset,int iNumBytes)
readPipeBulkInterrupt(int IntIndex, int EndpointIndex,
byte[] bufferData,int iArrayOffset,int iNumBytes)

Warning: Endpoint number is not equal to EndpointIndex
  • Example Execute.