简体   繁体   English

javax.usb:连接的设备数始终为零

[英]javax.usb : Number of attached devices is always zero

I have searched and searched SO, Google, etc. and everywhere I get code that is approximately similar to the one below. 我已经搜索并搜索了SO,Google等,并且到处都可以看到与下面的代码大致相似的代码。 For example, I tried this . 例如,我尝试过这个 I tried running it with root and otherwise. 我尝试用root或其他方式运行它。 But, the number of attached devices is always zero. 但是,连接的设备数始终为零。 Can you give me a clue as to why this could be happening ? 您能给我一个线索为什么会发生这种情况吗?

This is on Ubuntu-12.04, 32-bit. 这是在32位Ubuntu-12.04上。

Java code: Java代码:

    package com.me.test;

    import java.io.UnsupportedEncodingException;
    import java.util.List;
    import javax.usb.UsbDevice;
    import javax.usb.UsbDisconnectedException;
    import javax.usb.UsbException;
    import javax.usb.UsbHostManager;
    import javax.usb.UsbHub;
    import javax.usb.UsbServices;

    public class ListUsbDevices {
        public static void main(String[] args) throws SecurityException, UsbException, UnsupportedEncodingException, UsbDisconnectedException {
            UsbServices services = UsbHostManager.getUsbServices();
            UsbHub rootHub = services.getRootUsbHub();

            List<UsbDevice> devices = rootHub.getAttachedUsbDevices();
            if (devices.size()>0) {
                System.out.println("USB devices found.");
            } else {
                System.out.println("No USB devices found.");
            }

            for (UsbDevice device : devices) {
                System.out.println("\tProduct String " + device.getProductString());
                System.out.println("\tManufacturer String " + device.getManufacturerString());
                System.out.println("\tSerial Number " + device.getSerialNumberString());
            }


        }
    }

lsusb output: lsusb输出:

    user@host:~$ sudo lsusb
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 004 Device 010: ID 03eb:2013 Atmel Corp. 
    user@host:~$ sudo lsusb -s 4:10 -v
    Bus 004 Device 010: ID 03eb:2013 Atmel Corp. 
    Device Descriptor:
      bLength                18
      bDescriptorType         1
      bcdUSB               2.00
      bDeviceClass            0 (Defined at Interface level)
      bDeviceSubClass         0 
      bDeviceProtocol         0 
      bMaxPacketSize0        32
      idVendor           0x03eb Atmel Corp.
      idProduct          0x2013 
      bcdDevice           10.00
      iManufacturer           1 AppliedSensor
      iProduct                2 iAQ Stick
      iSerial                 0 
      bNumConfigurations      1
      Configuration Descriptor:
        bLength                 9
        bDescriptorType         2
        wTotalLength           41
        bNumInterfaces          1
        bConfigurationValue     1
        iConfiguration          0 
        bmAttributes         0x80
          (Bus Powered)
        MaxPower              100mA
        Interface Descriptor:
          bLength                 9
          bDescriptorType         4
          bInterfaceNumber        0
          bAlternateSetting       0
          bNumEndpoints           2
          bInterfaceClass         3 Human Interface Device
          bInterfaceSubClass      0 No Subclass
          bInterfaceProtocol      0 None
          iInterface              0 
            HID Device Descriptor:
              bLength                 9
              bDescriptorType        33
              bcdHID               1.11
              bCountryCode            0 Not supported
              bNumDescriptors         1
              bDescriptorType        34 Report
              wDescriptorLength      53
             Report Descriptors: 
               ** UNAVAILABLE **
          Endpoint Descriptor:
            bLength                 7
            bDescriptorType         5
            bEndpointAddress     0x81  EP 1 IN
            bmAttributes            3
              Transfer Type            Interrupt
              Synch Type               None
              Usage Type               Data
            wMaxPacketSize     0x0010  1x 16 bytes
            bInterval              10
          Endpoint Descriptor:
            bLength                 7
            bDescriptorType         5
            bEndpointAddress     0x02  EP 2 OUT
            bmAttributes            3
              Transfer Type            Interrupt
              Synch Type               None
              Usage Type               Data
            wMaxPacketSize     0x0010  1x 16 bytes
            bInterval              10
    Device Status:     0x0000
      (Bus Powered)

The javax.usb stuff can depend a lot on the flavor of linux you are running, and your machine's configuration in general. javax.usb的内容在很大程度上取决于您所运行的linux的风格以及计算机的总体配置。

You should try downloading and running the example programs, here on sourceforge , on your machine. 您应该尝试在计算机上的sourceforge上下载并运行示例程序。 If those do not work, then you should assume you'll need to change either the configuration of your machine, or something about your runtime. 如果这些方法不起作用,则应假定您需要更改计算机的配置或运行时的相关信息。

I was having some problems with this library, and got the following recommendations . 我在使用该库时遇到了一些问题,并获得了以下建议 This information may be quite outdated, but it may help point you in the right direction: 这些信息可能已经过时了,但可能会帮助您指出正确的方向:

Once downloaded and built, you need to ensure that libjusb.so gets copied over to $JAVA_HOME/jre/lib/i386, and that it has read and evecute permissions for your user. 下载并构建好之后,您需要确保将libjusb.so复制到$ JAVA_HOME / jre / lib / i386,并确保它具有用户的读取和执行权限。 Also ensure that you have the usbdevfs virtual device mounted, and that your user has proper R/W access to it. 另外,请确保已安装usbdevfs虚拟设备,并且您的用户对其具有正确的R / W访问权限。

Lastly, depending on what USB device you want to talk to, you may need to disable hotplug (or at least blacklist any installed modules that may also attempt to talk to your device). 最后,根据您要与之交谈的USB设备,您可能需要禁用热插拔(或至少将所有可能尝试与您的设备对话的已安装模块列入黑名单)。 If the device already has support in Linux, hotplug will load it's associated module and give it exclusive I/O access before you can grab the device from jUSB. 如果该设备已经在Linux中受支持,则热插拔将加载它的关联模块并为其提供独占的I / O访问权,然后才能从jUSB上获取该设备。

The above quoted text is from Brad Barclay. 以上引用的文字来自Brad Barclay。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM