1

我必须从将信号连续发送到 USB 端口并将其显示在监视器上的设备中读取数据(信号)。

作为 POC,我尝试从连接到我的系统的 USB 闪存驱动器中读取数据。我正在为 Windows 使用 JUSB 库。但是,当我更改我的设备驱动程序设置时,如下面的 JUSB 文档中所述,我的设备配置消失了(我的 device.getConfiguration(); 为空)。

http://www.steelbrothers.ch/jusb/ -- 文件 --> 附录 D

谁能帮我弄清楚我哪里出错了,或者建议我一些其他好的 API 来使用 JAVA 从 USB 设备读取数据。

代码如下,

try{
        Device device = null;
      DeviceImpl dev;
      for(int k=0; k < busses.length ; k++){
         System.out.println("\n\nBus[ " + ((USB)busses[k]).getBusNum() + " ]  ");  
         for(int i = 0; i < 5; i++){
              dev = (DeviceImpl)busses[k].getDevice(i);
              device = busses[k].getDevice(i);
            System.out.print(" [ " + i + " ] :  ");
            if(dev != null){
                   if(dev.getAddress() == 0)        System.out.println(" [ROOT]          numOfPort:" + dev.getNumPorts()+"  Address:" + dev.getAddress());
                   else {
                       if(dev.getNumPorts() > 0)   System.out.println(" [EXTERNAL HUB]  numOfPort:" + dev.getNumPorts()+"  Address:" + dev.getAddress());
                       else System.out.println(" [USB DEVICE]    on Port "+dev.getHubPortNum() +  "  Address           : " + dev.getAddress());
                       System.out.println("                                      uniqueID          : " + dev.getUniqueDeviceID());
                       System.out.println("                                      driverKeyName     : " + dev.getDriverKeyName());
                       System.out.println("                                      friendlyDeviceName: " + dev.getFriendlyDeviceName());
                       if (dev instanceof Device) System.out.print("                                      Object Type       : Device");
                       if (dev instanceof DeviceImpl) System.out.print(", DeviceImpl");
                       if (dev instanceof JUSB) System.out.println(", JUSB");
                       if (dev instanceof NonJUSB) System.out.println(", NonJUSB");
                       System.out.println("***************confoig: "+dev.configuration);//getConfiguration(0));
                      boolean brk = false;
                       StringTokenizer uniqueNameValPairs = new StringTokenizer(dev.getUniqueDeviceID(),"&");
                       while(uniqueNameValPairs.hasMoreTokens()){
                           StringTokenizer strToken = new StringTokenizer(uniqueNameValPairs.nextToken(),"_");
                           while(strToken.hasMoreTokens()){
                               if(strToken.nextToken().equals("03f0")){
                                  System.out.println("breaking");
                                 readData(dev);
                                 brk = true;
                                  break;
                               }
                           }
                       }



static void readData(DeviceImpl device){
    try{
        if (device != null)
           {
               // Obtain the current Configuration of the device and the number of 
               // Interfaces available under the current Configuration.
               Configuration config = device.getConfiguration();
               if(null == config)
                   config = device.configuration;
               System.out.println("config: "+config);
               int total_interface = config.getNumInterfaces();

               // Traverse through the Interfaces
               for (int k=0; k<total_interface; k++)
               {
                   // Access the currently 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();
                       System.out.println("ep.getInputStream(): "+ep.getInputStream());
                       // If the endpoint is an input endpoint, obtain its
                       // InputStream and read in data.
                       if (input)
                       {
                           InputStream in;
                           System.out.println("ep.getInputStream()111: "+ep.getInputStream());
                           BufferedReader brIn = new BufferedReader(new InputStreamReader(ep.getInputStream()));
                           System.out.println("**************Input stream: "+brIn);
                           String inStr = null;
                           while( (inStr = brIn.readLine()) != null){
                               System.out.println(inStr);
                           }
                           // Read in data here
                          // in.close();
                       }
                       // If the Endpoint is and output Endpoint, obtain its 
                       // OutputStream and write out data.
                       else
                       {
                           OutputStream out;
                           out = ep.getOutputStream();
                           System.out.println("**************Output stream: "+out);
                           // Write out data here.
                           out.close();
                       }
                   }
               }
            }
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        //System.exit(0);
    }
}
4

0 回答 0