Java HID client

I've got a simple Java HID client running under Ubuntu 10.04 (Lucid).

I found the project at http://code.google.com/p/javahidapi/ I used mercurial to run

hg clone https://code.google.com/p/javahidapi/

I needed to install the openjdk-6-jdk, libusb-1.0-0 and libusb-1.0-0-dev and then ran make in the project root.

Yesterday I got HIDAPITest to run by manually setting permissions on the mbed usb device. I created a usb groups, added myself to the group, logged in and out and set the group of the relevant usb device to be usb.

Today I created a file 10-usb.rules in /etc/udev/rules.d/ with the following contents

BUS=="usb", GROUP="usb"

which sets the group for all usb devices to be usb, so my account can now read and write to all the devices.

I've slightly modified the example to write all the data sent by the mbed to standard err. Here's the code:

package spike;

import java.io.IOException;

import com.codeminders.hidapi.*;

public class HIDAPITest
{
    private static final long READ_UPDATE_DELAY_MS = 50L;

    static
    {
        System.loadLibrary("hidapi-jni");
    }

    // MBED
    static final int VENDOR_ID = 0x1234;
    static final int PRODUCT_ID = 6;
    private static final int BUFSIZE = 2048; 
    
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        listDevices();
        readDevice();
    }

    private static void readDevice()
    {
        HIDDevice dev;
        try
        {
            dev = HIDManager.openById(VENDOR_ID, PRODUCT_ID, null);
            System.err.print("Manufacturer: " + dev.getManufacturerString() + "\n");
            System.err.print("Product: " + dev.getProductString() + "\n");
            System.err.print("Serial Number: " + dev.getSerialNumberString() + "\n");
            try
            {
                byte[] buf = new byte[BUFSIZE];
                dev.enableBlocking();
                while(true)
                {
                    int n = dev.read(buf);
                    for(int i=0; i<n; i++)
                    {
                        int v = buf[i];
                        if (v<0) v = v+256;
                        String hs = Integer.toHexString(v);
                        if (v<16) 
                            System.err.print("0");
                        System.err.print(hs + " ");
                    }
                    System.err.println("");
                    
                    try
                    {
                        Thread.sleep(READ_UPDATE_DELAY_MS);
                    } catch(InterruptedException e)
                    {
                        //Ignore
                    }
                }
            } finally
            {
                dev.close();
            }
        } catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void listDevices()
    {
        String property = System.getProperty("java.library.path");
        System.err.println(property);
        
        try
        {
            HIDDeviceInfo[] devs = HIDManager.listDevices();
            System.err.println("Devices:\n\n");
            for(int i=0;i<devs.length;i++)
            {
                System.err.println(""+i+".\t"+devs[i]);
                System.err.println("---------------------------------------------\n");
            }
        }
        catch(IOException e)
        {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }

}

I haven't yet sent data back to the mbed, so I can't quite justify an entry in the USB bindings matrix.


Please log in to post comments.