3

我正在尝试使用 jUSB 库通过 USB 到串行电缆发送数据。我在 Windows 上的 NetBeans IDE 中进行编码。

消息背后的问题是什么:以下代码中的“USB Host support is available”:

package usb.core;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;    
import usb.core.*;

public class Main {
    public static void main(String[] args) throws IOException {
        try {
            // Bootstrap by getting the USB Host from the HostFactory.
            Host host = HostFactory.getHost();

            // Obtain a list of the USB buses available on the Host.
            Bus[] bus = host.getBusses();
            int total_bus = bus.length;
            System.out.println(total_bus);
            // Traverse through all the USB buses.
            for (int i = 0; i < total_bus; i++) {
                // Access the root hub on the USB bus and obtain the
                // number of USB ports available on the root hub.
                Device root = bus[i].getRootHub();
                int total_port = root.getNumPorts();

                // Traverse through all the USB ports available on the 
                // root hub. It should be mentioned that the numbering 
                // starts from 1, not 0.
                for (int j = 1; j <= total_port; j++) {
                    // Obtain the Device connected to the port.
                    Device device = root.getChild(j);
                    if (device != null) {
                        // USB device available, do something here.
                        // Obtain the current Configuration of the device
                        // and the number of interfaces available under the
                        // current Configuration.
                        Configuration config = device.getConfiguration();
                        int total_interface = config.getNumInterfaces();

                        // Traverse through the Interfaces
                        for (int k = 0; k < total_interface; k++) {
                            // Access the current Interface and obtain the 
                            // number of endpoints available on the Interface 
                            Interface itf = config.getInterface(k, 0);
                            int total_ep = itf.getNumEndpoints();

                            // Traverse through all the endpoints.
                            for (int l = 0; l < total_ep; l++) {
                                // Access the endpoint and
                                // obtain its I/O type
                                Endpoint ep = itf.getEndpoint(l);
                                String io_type = ep.getType();
                                boolean input = ep.isInput();

                                // If the endpoint is an input endpoint,
                                // obtain its InputStream and read in data.
                                if (input) {
                                    InputStream in;
                                    in = ep.getInputStream();
                                    // Read in data here
                                    in.close();
                                }
                                else {
                                    // If the Endpoint is an output Endpoint,
                                    // obtain its OutputStream and
                                    // write out data.
                                    OutputStream out;
                                    out = ep.getOutputStream();
                                    // Write out data here.
                                    out.close();
                                }
                            }
                        }
                    }
                }
            }

        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
4

1 回答 1

1

我用谷歌搜索了那个错误信息,我发现:

  • 2005 年的一个论坛帖子说,在 Linux 上,这可能是由于其他东西抢占了 USB 控制器的独占使用:http: //bytes.com/topic/java/answers/16736-jusb

  • 源代码的在线副本,这表明如果 getHost 尝试创建(特定于平台的)HostFactory 失败,则会发生这种情况。不幸的是,代码会遇到意外异常 (*),因此您需要使用 Java 调试器来确定真正的代码是什么。

Exception(* 代码在其他地方捕获maybeGetHost并丢弃诊断程序!这是一个主要的禁忌,也是图书馆整体代码质量的一个大红旗。如果我是你,我会寻找更好的质量要使用的库。)

于 2012-04-28T16:15:34.170 回答