Java – Reading data from USB Port


currently I am working on a project for university where i have to create a program which controls the humidity in a terrarium.
On this purpose i've got an hygrometer.

First of all the program has to import the raw data from the hygrometer, but i have no idea how it works.
The documentation says there is an USB interface for it, but i can only find the way how to parse the raw data.
I also wrote an Email to the company which sells this hygrometer. They said theres an external software which imports and handle with this data. However I am not allowed to use external softwares.
Therefore I am forced to read the raw data directly from the USB Port. I tried to work with usb4java but i was only able to find all connected usb devices.
I have no idea how to go on. Please help me

Code below

public class DumpDevices
 * Dumps the specified USB device to stdout.
 * @param device
 *            The USB device to dump. 

private static void dumpDevice(final UsbDevice device)
    // Dump information about the device itself
    final UsbPort port = device.getParentUsbPort();
    if (port != null)
        System.out.println("Connected to port: " + port.getPortNumber());
        System.out.println("Parent: " + port.getUsbHub());

    // Dump device descriptor

    // Process all configurations
    for (UsbConfiguration configuration: (List<UsbConfiguration>) device
        // Dump configuration descriptor

        // Process all interfaces
        for (UsbInterface iface: (List<UsbInterface>) configuration
            // Dump the interface descriptor

            // Process all endpoints
            for (UsbEndpoint endpoint: (List<UsbEndpoint>) iface
                // Dump the endpoint descriptor


    // Dump child devices if device is a hub
    if (device.isUsbHub())
        final UsbHub hub = (UsbHub) device;
        for (UsbDevice child: (List<UsbDevice>) hub.getAttachedUsbDevices())

 * Main method.
 * @param args
 *            Command-line arguments (Ignored)
 * @throws UsbException
 *             When an USB error was reported which wasn't handled by this
 *             program itself.
public static void main(final String[] args) throws UsbException
    // Get the USB services and dump information about them
    final UsbServices services = UsbHostManager.getUsbServices();
    System.out.println("USB Service Implementation: "
        + services.getImpDescription());
    System.out.println("Implementation version: "
        + services.getImpVersion());
    System.out.println("Service API version: " + services.getApiVersion());

    // Dump the root USB hub

Best Solution

I think usb4java is very good but quite complex. You might accomplish reading data from usb port very easily with jssc

Example of reading data from usb:

SerialPort serialPort = new SerialPort("/dev/tty.usbmodem1435");
serialPort.openPort();//Open serial port
serialPort.setParams(4800, 8, 1, 0);//Set params.
while(true) {
    byte[] buffer = serialPort.readBytes(10);
    if(buffer!=null) {
        for(byte b:buffer) {

Notice that '/dev/tty.usbmodem1435' is just an example name, you should use the port name you are interested on.

Hope it helps.

Related Question