从连接到USB端口的设备读取数据

时间:2012-06-25 06:51:40

标签: java usb jusb

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

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

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

任何人都可以帮我弄清楚我出错的地方或建议我使用JAVA从USB设备读取数据的其他好API。

代码如下,

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);
    }
}

0 个答案:

没有答案