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.

















33 comments:

  1. Hello,
    When i try to run your script it gives me this error:

    run:
    com.lucasF.JCommUSB_2_0.USBException: The operation completed successfully.

    at com.lucasF.JCommUSB_2_0.USBDevice.getDevicePathNat(Native Method)
    at com.lucasF.JCommUSB_2_0.USBDevice.getAttachedDevicePath(USBDevice.java:197)
    at test.UsbJava.main(UsbJava.java:11)
    BUILD SUCCESSFUL (total time: 1 second)


    Can you give me an email at scobee@keptprivate.com with a solution ?
    regards

    ReplyDelete
  2. When I ran your script in netbeans i get this error

    run:
    Exception in thread "main" java.lang.UnsatisfiedLinkError: no lucasAndresForni_Win32 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1028)
    at com.lucasF.JCommUSB_2_0.USBDevice.(USBDevice.java:45)
    at usb_comm.Main.main(Main.java:20)
    Java Result: 1

    ReplyDelete
  3. hi:
    the solution the error you describe is to copy the file lucasAndresForni_Win32.dll to the System32 folder
    regards

    ReplyDelete
  4. Hi

    I am not a java pro but I am involved in a project where I need to read/send data from a device interfaced via USB.Does your api help me? If yes then can you plz help upto the step where the device would be ready to read and accept data from my program. I am little confused when I open your folder because there are hell of the folders considering that I am not java pro.
    Thanks in Advance

    ReplyDelete
  5. Hi

    Somehow I have managed to run the program but Now I am getting these errors.I have also copied "lucasAndresForni_Win32.dll" to system32 flder.
    run:
    java.lang.NoClassDefFoundError: javaapplication4/Main

    Caused by: java.lang.ClassNotFoundException: javaapplication4.Main

    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    Could not find the main class: javaapplication4.Main. Program will exit.

    Exception in thread "main" Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    Plz help me in this.

    ReplyDelete
  6. Hi Muhammad:
    access to any device. You must create the driver with the program WinJavaInf.exe

    Copy "lucasAndresForni_Win32.dll" to system32 folder.

    ReplyDelete
  7. Hi

    I'm unable to create the Driver for the device. I found the two necessary ID's (VID:06A3, PID:0464, it's a joystick), but when i try to save the INF file, i get an error "the file "C:\Users\Bene\AppData\Local\Temp\lucasAndresForni_Win32.dll" already exists".
    i tried different compatibily modes (i'm running windows 7), i already copied the file "lucasAndresForni_Win32.dl" to system32 folder, nothing changes...

    Thanks for your help.

    greez

    ReplyDelete
    Replies
    1. same problem with me. i'm using windows 7 - 64 bit

      Delete
  8. download the latest version of driver has solved that problem ( https://sourceforge.net/projects/javausbapiforwi/ )

    ReplyDelete
    Replies
    1. But this problem remain with latest software too Please help..

      Delete
  9. Hi LucasF,
    My purpose is to read weight from a USB Weighing scale in browser through applet. I found this API and tried. As a first step of testing Right now I have connected USB mouse and put in VID and PID in the file Usb_Descriptor.c Now created the dll file lucasAndresForni_Win32.dll created using WinJavaInf.exe and put in system32 dir.

    When I run the demo program given by you, I get following error:

    ====================
    When I run the program I get following error:

    com.lucasF.JCommUSB_2_0.USBException: The operation completed successfully.

    at com.lucasF.JCommUSB_2_0.USBDevice.getDevicePathNat(Native Method)
    at com.lucasF.JCommUSB_2_0.USBDevice.getAttachedDevicePath(USBDevice.jav
    a:197)
    at UsbJava.main(UsbJava.java:10)
    ======================

    Am I missing something. At the moment should I expect from the program when I run it to return some information related to device patch and connection status?

    Any pointer will be greatly appreciated.

    ReplyDelete
  10. Is there any more information out there about how to setup the device to use this USBDevise class? I'm finding the information very sketchy, especially for someone like me who is not familiar with how device drivers work?

    I get the same error as above "The operation completed successfully". A call to USBDevice.getNumAttachedDevices() returns 0, which seems to tell me that there are no devices loaded?

    ReplyDelete
  11. It should uninstall the previous drivers and create the new drivers and then install.
    I can not find another way to do it.
    if anyone knows would appreciate your help.

    ReplyDelete
  12. Is the file lucasAndresForni_Win32.dll free distributable and has any license agreement.

    Regards
    Xavier

    ReplyDelete
  13. Hi.
    I'm trying to use this API to reprogram Freescale and Renesas' MCUs.
    Can you explain the methods writePipeBulkInterrupt and readPipeBulkInterrupt?

    In my case I have binary files that I want to write in the device. How can I do that?

    In my case the API works, I've created the drivers for the USB programmer and I get a "true" when I try to connect.

    ReplyDelete
  14. thank you for sharing this great and useful post. i'll soon write a blog and i would love to include your ideas with a link back to you. Hope it would be okey. Thanks!

    ReplyDelete
  15. Will your .dll work on a 64-bit system?

    ReplyDelete
  16. Hello I tried to run WinJavaInf.exe without success. I download the version 2010-07-19. What did i miss?
    thanks in advance
    Silvina

    ReplyDelete
  17. I have the same problem as Bene and downloading the latest version of your package for creating the driver didn't help.
    :( it would be nice you fix it!
    regards,
    Luis.

    ReplyDelete
  18. Hello, i have try your code, and all function correctly but when i execute thi code:

    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(myUSB.readPipeBulkInterrupt(1, 2, baData, iArrayOffset, iNumBytes));

    byte[] bytesIn = new byte[64];
    myUSB.readPipeBulkInterrupt(0, 1, bytesIn, 0, 64);

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

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

    the console prin this message:

    com.lucasF.JCommUSB_2_0.USBException: Accesso alla rete negato.

    at com.lucasF.JCommUSB_2_0.USBDevice.readPipeBulkInterruptNat(Native Method)
    at com.lucasF.JCommUSB_2_0.USBDevice.readPipeBulkInterrupt(USBDevice.java:570)
    at UsbJava.main(UsbJava.java:17)
    Device Path of Device at index 0 = \\?\usb#vid_04fc&pid_0232#5&29121321&0&2#{5a44bde9-d92c-4a97-b2b8-63ba19e74635}

    can you help me?

    ReplyDelete
  19. Hello there, Is there any example on the read function?

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. I'm unable to create the Driver for the device.I try to save the INF file, i get an error "the file "C:\Users\Bene\AppData\Local\Temp\lucasAndresForni_Win32.dll" already exists".
    i tried different compatibily modes (i'm running windows 7 64Bits).

    Thanks for your help.

    ReplyDelete
  22. Hi Lucas,
    Thanks for a wonderful API, nice work!!
    I was able to run your project successfully in eclipse on windows 7, But the program terminates with following error message in console,

    com.lucasF.JCommUSB_2_0.USBException: The operation completed successfully.

    Does it requires some other version of OS? Please help with probable cause of error and resolution for same

    Thanks!!

    ReplyDelete
  23. Hey... This is a wonderful post... Do you have a dll which supports 64 bit?

    I am getting this error when I try to execute a program like you wrote using your jar and dll:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Windows\System32\lucasAndresForni_Win32.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform

    Do you have a solution for this? Please let me know.

    ReplyDelete
  24. Pretty nice post. I simply stumbled upon your weblog and needed.To say that I even have actually treasured surfboarding around your diary posts.
    After all i will be subscribing on your feed and that i am hoping you write once more soon!
    restore deleted email

    ReplyDelete
  25. it doesn't work in 64-bit.. is there any workaround??

    ReplyDelete
  26. hiii...

    i'm trying with Windows xp 32 bit doesn't work, also with windows 7 64 bit. can you explain me why....

    tq

    ReplyDelete
  27. mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmany thanks

    ReplyDelete
  28. Download Java in http://java.joydownload.com/

    ReplyDelete