下载java API及lib库地址:http://usb4java.org/index.html

1、导入所需要的库:

2、添加配置文件:文件名:javax.usb.properties;内容:javax.usb.services = org.usb4java.javax.Services

目录结构如下:

3、具体代码实现如下:

 package com.test.usb;

 import java.util.List;

 import javax.usb.UsbConfiguration;
import javax.usb.UsbDevice;
import javax.usb.UsbDeviceDescriptor;
import javax.usb.UsbEndpoint;
import javax.usb.UsbHostManager;
import javax.usb.UsbHub;
import javax.usb.UsbInterface;
import javax.usb.UsbPipe; public class UsbUtil {
private static short idVendor = (short)0x8888;
private static short idProduct = (short)0x0006; public static void main(String[] args) {
try {
UsbPipe sendUsbPipe = new UsbUtil().useUsb(); if (sendUsbPipe != null) {
byte[] buff = new byte[64];
for (int i = 0; i < 9; i++) {
buff[i] = (byte)i;
sendMassge(sendUsbPipe, buff);
}
} } catch (Exception e) {
e.printStackTrace();
}
} public UsbPipe useUsb() throws Exception{
UsbInterface iface = linkDevice();
if (iface == null) {
return null;
}
UsbEndpoint receivedUsbEndpoint,sendUsbEndpoint; sendUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(0);
if (!sendUsbEndpoint.getUsbEndpointDescriptor().toString().contains("OUT")) {
receivedUsbEndpoint = sendUsbEndpoint;
sendUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(1);
} else {
receivedUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(1);
} //发送:
UsbPipe sendUsbPipe = sendUsbEndpoint.getUsbPipe();
sendUsbPipe.open(); //接收
final UsbPipe receivedUsbPipe = receivedUsbEndpoint.getUsbPipe();
receivedUsbPipe.open(); new Thread(new Runnable() {
public void run() {
try {
receivedMassge(receivedUsbPipe);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
return sendUsbPipe;
} public UsbInterface linkDevice() throws Exception{ UsbDevice device = null;
if (device == null) {
device = findDevice(UsbHostManager.getUsbServices()
.getRootUsbHub());
}
if (device == null) {
System.out.println("设备未找到!");
return null;
}
UsbConfiguration configuration = device.getActiveUsbConfiguration();
UsbInterface iface = null;
if (configuration.getUsbInterfaces().size() > 0) {
iface = (UsbInterface)configuration.getUsbInterfaces().get(0);
} else {
return null;
}
iface.claim();
return iface;
} public void receivedMassge(UsbPipe usbPipe) throws Exception{
byte[] b = new byte[64];
int length = 0;
while (true) {
length = usbPipe.syncSubmit(b);//阻塞
System.out.println("接收长度:" + length);
for (int i = 0; i < length; i++) {
System.out.print(Byte.toUnsignedInt(b[i])+" ");
}
}
} public static void sendMassge(UsbPipe usbPipe,byte[] buff) throws Exception{
usbPipe.syncSubmit(buff);//阻塞
//usbPipe.asyncSubmit(buff);//非阻塞
} public UsbDevice findDevice(UsbHub hub)
{
UsbDevice device = null;
List list = (List) hub.getAttachedUsbDevices();
for (int i = 0;i<list.size();i++)
{
device = (UsbDevice)list.get(i);
UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
if (desc.idVendor() == idVendor && desc.idProduct() == idProduct) {return device;}
if (device.isUsbHub())
{
device = findDevice((UsbHub) device);
if (device != null) return device;
}
}
return null;
}
}

 注意点:发送和接收数据长度要与设备匹配

基于usb4java的usb通讯的更多相关文章

  1. 基于Linux的USB 主/从设备之间通讯的三种方式

    转载:http://archive.eet-china.com/www.eet-china.com/ART_8800323770_617693_TA_eda530e7.HTM 随着简单易用的USB接口 ...

  2. .Net开发笔记(十五) 基于“泵”的TCP通讯(接上篇)

    上一篇博客中说了基于“泵”的UDP通讯,附上了一个Demo,模拟飞鸽传书的功能,功能不太完善,主要是为了说明“泵”在编程中的应用.本篇文章我再附上一个关于TCP通讯的两个Demo,也都采用了“泵”模式 ...

  3. C#:基于WMI查询USB设备信息 及 Android设备厂商VID列表

    /* ---------------------------------------------------------- 文件名称:WMIUsbQuery.cs 作者:秦建辉 MSN:splashc ...

  4. Android基于XMPP的即时通讯3-表情发送

    这篇博文主要讲表情发送的一些东西. 参考:Android基于XMPP的即时通讯1-基本对话 1.准备好资源文件 采用的是emoji的表情,我打包好了,下载地址:http://files.cnblogs ...

  5. Android基于XMPP的即时通讯2-文件传输

    本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输 1.初始化文件传输管理类 public static FileTransferManager get ...

  6. C#:基于WMI查询USB设备

    来源:http://blog.csdn.net/jhqin/article/details/6734673 /* ------------------------------------------- ...

  7. USB通讯协议 && 数据传输

    USB2.0通讯协议(spalish)   1.包(packet) 包是USB系统中信息传输的基本单元,所有数据都是经过打包后在总线上传输的.USB包由五部分组成,同步字段(sync).包标识符(PI ...

  8. 基于“泵”的TCP通讯(接上篇)

    基于“泵”的TCP通讯(接上篇) 上一篇博客中说了基于“泵”的UDP通讯,附上了一个Demo,模拟飞鸽传书的功能,功能不太完善,主要是为了说明“泵”在编程中的应用.本篇文章我再附上一个关于TCP通讯的 ...

  9. 基于libUSB的USB设备固件更新程序(下载数据)(转)

    源:基于libUSB的USB设备固件更新程序(下载数据) 本文紧接上一篇日志:基于libUSB-Win32的USB设备固件更新程序(前言),相关背景以及起因等,此处不再赘述,如感兴趣请移步. libU ...

随机推荐

  1. phpunit assert断言分类整理

    布尔类型 方法名 含义 参数 返回值 assertTrue 断言为真     assertFalse 断言为假     NULL类型 方法名 含义 参数 返回值 assertNull 断言为NULL ...

  2. [转]git commit --amend用法

    适用场景: 比方说,你的代码已经提交到git库,leader审核的时候发现有个Java文件代码有点问题,于是让你修改,通常有2种方法: 方法1:leader 将你提交的所有代码 abandon掉,然后 ...

  3. linux 查找并kill进程

    以php以关键字查找进程 $ ps aux | grep php root             32957   0.0  0.1  2470904   8908 s002  S+    4:53下 ...

  4. data.frame类型数据如何将第一列值替换为行号

    data.frame类型数据如何将第一列值替换为行号 row.names(data) <- data[, 1]data <- data[, -1]

  5. js this 引起的祸

    注意:这里的 this 指向已经改变了. 所以不能再次使用 this 需在上面自定义变量. 那些年踩过的坑,记录一下.

  6. mysql与mycat搭建实现集群与读写分离

    数据库性能优化普遍采用集群方式,oracle集群软硬件投入昂贵,今天花了一天时间搭建基于mysql的集群环境. 主要思路 简单说,实现mysql主备复制-->利用mycat实现负载均衡. 比较了 ...

  7. Tcp Udp发送包的大小限制问题

    以太网(Ethernet)数据帧的长度必须在46-1500字节之间,这是由以太网的物理特性决定的.    这个1500字节被称为链路层的MTU(最大传输单元).    但这并不是指链路层的长度被限制在 ...

  8. [原]win10下编译lua5.3.4

    1.下载lua源码http://www.lua.org/ftp/ 2.打开vs2012工具命令提示 3.cd 到lua源码的src目录 4.依次执行以下代码 cl /MD /O2 /c /DLUA_B ...

  9. Centos 下 JProfiler 9.1.1 安装 部署 及 使用

    JProfiler[1] 是一个商业授权的Java剖析工具,由EJ技术有限公司,针对的Java EE和Java SE应用程序开发的.它把CPU.执行绪和内存的剖析组合在一个强大的应用中.JProfil ...

  10. HTTPS原理和CA证书申请(转)

    原文地址:http://blog.51cto.com/11883699/2160032 众所周知,WEB服务存在http和https两种通信方式,http默认采用80作为通讯端口,对于传输采用不加密的 ...