下载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. ProtoBuf3 C++使用篇

    protobuf 是用于结构化数据串行化的灵活.高效.自动化的解决方案.又如 XML,不过它更小.更快.也更简单.你只需要按照你想要的数据存储格式编写一个.proto,然后使用生成器生成的代码来读写这 ...

  2. 【资料下载区】【iCore1S相关代码、资料下载地址】更新日期2017/10/09

    [iCore1S相关文档][更新中...] iCore1S原理图(PDF)下载iCore1S引脚注释(PDF)下载 [iCore1S相关例程代码][ARM][更新中...] DEMO1.0测试程序发布 ...

  3. 【iCore1S 双核心板_ARM】例程十七:FSMC实验——读写FPGA

    实验现象: 先烧写FPGA程序,再烧写ARM程序,ARM程序烧写完毕后即开始读写RAM测试,测试成功,绿色ARM·LED亮,测试失败,红色ARM·LED闪烁. 核心代码: int main(void) ...

  4. Centos7下安装Oracle11g r2图形化界面数据库

    我的centos7是在VMware下安装的,安装Oracle安装了好久好久,最开始的时候在网上找的两个文章,按照步骤装,有一篇写着装的时候有灰色的竖线,直接按space键或者鼠标右键closed关闭掉 ...

  5. STM32串口DMA超时接收方法,可大大节约CPU时间

    //超时时间定义#define        UART1_TimeoutComp 2  //20ms#define        UART2_TimeoutComp 10  //100ms#defin ...

  6. DTO转DOMAIN动态转换类。

    package dtotransfer.util; import dtotransfer.annotation.DomainField; import java.lang.annotation.Ann ...

  7. SAP Brazil J1BTAX 为税收例外创建税收组(翻译)

    很多人对如何维持巴西的税收仍有疑问.前段时间,一家有几个税务问题的公司联系我帮助他们,我发现多年来,由于他们的税务计算系统缺少配置,他们正在进行手动调整. 维持税收的第一条规则是: TAXBRJ = ...

  8. 并发编程基础之volatile关键字的用法

    一:概念 volatile关键字是一个轻量级的线程同步,它可以保证线程之间对于共享变量的同步,假设有两个线程a和b, 它们都可以访问一个成员变量,当a修改成员变量的值的时候,要保证b也能够取得成员变量 ...

  9. Redis介绍和安装

    一. Redis的介绍 Redis 是一个Key-Value存储的系统:它支持存储的value类型有string(字符串),list(列表),set(集合),zset(有序集合):为了保证效率:数据都 ...

  10. 阿里云负载均衡SLB 七层https协议 nginx 获取真实IP

    https://www.cnblogs.com/baylorqu/p/8565667.html https://help.aliyun.com/document_detail/54007.html