2012-04-28 18 views
3

我試圖通過USB將數據發送到用jUSB庫串行電纜。我在Windows上的NetBeans IDE中進行編碼。「USB主機支持不可用」消息的根本原因是什麼?

什麼是消息背後的問題:「USB主機支持是不可用」在下面的代碼:

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()); 
     } 
    } 
} 
+0

的選擇可能是讓操作系統安裝的電纜驅動程序,然後使用它作爲一個串口,而不是作爲一個USB設備。有可能是這樣的驅動程序阻礙了您的工作,或者您的安裝中缺少啓用原始USB訪問權限所需的代理驅動程序。 – 2012-04-28 16:21:44

回答

1

我用Google搜索該錯誤信息,我發現:

  • 一個論壇帖子從2005年開始說在Linux上的這可能是由於別的東西搶了USB控制器的專用:http://bytes.com/topic/java/answers/16736-jusb

  • 一個的the source code在線副本,這表明這種情況如果和getHost的嘗試創建(特定平臺)HostFactory失敗。不幸的是,代碼吃意外的異常(*),所以你需要使用Java調試器來找出真正的代碼是什麼。

(*的代碼捕獲在maybeGetHost等地Exception和扔掉的診斷!這是一個重大的禁忌,和一個大紅旗在圖書館的整體代碼質量。如果我是你,我會尋找一個更好的質量圖書館使用。)

相關問題