回到考试中心首页|兰州学生网LOGO
首页  |  外语考试  |  IT认证  |  基础教育  |  甘肃省专升本  |  学历考试  |  硕士学历  |  公务员  |  会计考试  |  职业资格  |  医学医药  |  建筑工程
会员登录
热门文章
·二级建造师模拟试题一套
·公务员面试自我介绍范文
·践行社会主义荣辱观 党课讲稿
·八荣八耻,从我做起!
·临床执业医师考试真题完全版(一)
·学习八荣八耻心得体会(完善版)
·八荣八耻演讲稿
·八荣八耻歌(歌词)
·英文自我介绍
·中学:教师资格认证考试全真模拟试卷和答案
·八荣八耻中学生演讲稿
·小学教师资格考试教育学仿真试题之一
·八荣八耻手抄报内容
·《八荣八耻歌》歌词 伴奏及曲牌
·暑期实践报告
广告推荐
在Java应用程序中访问USB设备
作者: 来源: 责任编辑: 更新日期:2006-3-27 19:50:03 浏览:
在Java应用程序中访问USB设备

  用 jUSB API 访问一台 USB 设备的正常过程如下:

  ·通过从 HostFactory 得到 USB Host 进行 Bootstrap。

  ·从 Host 访问 USB Bus,然后从这个 Bus 访问 USB root hub(即 USB Device)。

  ·得到 hub 上可用的 USB 端口数量,遍历所有端口以找到正确的 Device。


  ·访问附加到特定端口上的 USB Device。可以用一台 Device 的 PortIdentifier 直接从 Host 访问它,也可以通过从 root hub 开始遍历 USB Bus 找到它。

  ·用 ControlMessage 与该 Device 直接交互,或者从该 Device 的当前 Configuration 中要求一个 Interface,并与该 Interface 上可用的 Endpoint 进行 I/O 。

  清单 1 展示了如何用 jUSB API 获得 USB 系统中的内容。这个程序编写为只是查看 root hub 上可用的 USB 设备,但是很容易将它改为遍历整个 USB 树。这里的逻辑对应于上述步骤 1 到步骤 4。

  清单 1. 用 jUSB API 获得 USB 系统的内容

import usb.core.*;

public class ListUSB
{
 public static void main(String[] args)
 {
  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;

   // 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.
     }
    }
   }
  } catch (Exception e)
  {
   System.out.println(e.getMessage());
  }
 }

  清单 2 展示了在应用程序成功地找到了 Device 的条件下,如何与 Interface 和 EndPoint 进行批量 I/O。 这个代码段也可以修改为执行控制或者中断 I/O。它对应于上述步骤 5。

  清单 2. 用 jUSB API 执行批量 I/O

if (device != null)
{
 // 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 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();

   // 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();
   }
   // If the Endpoint is and output Endpoint, obtain its
   // OutputStream and write out data.
   else
   {
    OutputStream out;
    out = ep.getOutputStream();
    // Write out data here.
    out.close();
   }
  }
 }
}

  jUSB 项目在 2000年 6月到 2001年 2月期间非常活跃。该 API 的最新的版本 0.4.4发表于 2001年 2月 14日。从那以后只提出了很少的改进,原因可能是 IBM 小组成功地成为了 Java 语言的候选扩展标准。不过,基于 jUSB 已经开发出一些第三方应用程序,包括 JPhoto 项目(这是一个用 jUSB 连接到数码照相机的应用程序)和 jSyncManager 项目(这是一个用 jUSB 与使用 Palm 操作系统的 PDA 同步的应用程序)。

本文章共5页,当前在第3页  1  2  3  4  5  

打印本文 收藏本文 返回顶部 关闭窗口
   考试中心分类导航
      外语考试 | 英语四六级 | 专四专八 | 职称英语 | 口译笔译 | 商务英语 | 实用英语 | 托福 | 雅思 | GRE | LSAT | PETS | GMAT
      IT认证 | 计算机等级 | 软件程序员 | 微软认证 | 思科认证 | Oracle | Linux | JAVA
      甘肃省专升本 | 专升本动态 | 招生计划 | 考试大纲 | 专升本试题    基础教育 | 中考 | 高考    学历考试 | 成人高考 | 自学考试
      硕士学历 | 考研 | MBA/EMBA | 在职硕士 | 法律硕士 | 会计硕士 | 工程硕士    公务员 | 考试动态 | 考试指导 | 考试真题 | 模拟题 | 技巧心得
      医学医药 | 执业护士 | 执业医师 | 执业药师    会计考试 | 资产评估 | 经济师 | 从业资格 | 会计职称 | 注会 | 注税 | 高级会计 | ACCA/CAT | 精算师
      职业资格 | 保险考试 | 报关员 | 外销员 | 物流师 | 报检员 | 司法考试 | 律师考试 | 商务师 | 人力资源 | 电子商务 | 导游资格 | 证券考试 | 教师资格
      建筑工程 | 岩土师 | 室内设计师 | 质量资格 | 房产估价 | 土地估价 | 建造师 | 造价师 | 建筑师 | 结构师 | 监理师 | 咨询师 | 城市规划 | 安全工程师
设为首页   |    收藏本站   |    网站帮助   |    网站地图   |    意见反馈   |    关于我们   |    广告服务   |    联系办法
陇ICP备05005179 不良信息举报