Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data from USB Port

Tags:

java

usb

usb4java

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 documentation documentation

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
    System.out.println(device);
    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
    System.out.println(device.getUsbDeviceDescriptor());

    // Process all configurations
    for (UsbConfiguration configuration: (List<UsbConfiguration>) device
        .getUsbConfigurations())
    {
        // Dump configuration descriptor
        System.out.println(configuration.getUsbConfigurationDescriptor());

        // Process all interfaces
        for (UsbInterface iface: (List<UsbInterface>) configuration
            .getUsbInterfaces())
        {
            // Dump the interface descriptor
            System.out.println(iface.getUsbInterfaceDescriptor());

            // Process all endpoints
            for (UsbEndpoint endpoint: (List<UsbEndpoint>) iface
                .getUsbEndpoints())
            {
                // Dump the endpoint descriptor
                System.out.println(endpoint.getUsbEndpointDescriptor());
            }
        }
    }

    System.out.println();

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


/**
 * 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());
    System.out.println();

    // Dump the root USB hub
    dumpDevice(services.getRootUsbHub());
}
like image 899
Xinren Avatar asked Nov 09 '22 05:11

Xinren


1 Answers

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) {
            System.out.print(b);
        }
    } 
}

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

Hope it helps.

like image 117
Ramon Rius Avatar answered Nov 14 '22 21:11

Ramon Rius