下载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. Python之turtle画同心圆和棋盘

    画饼图 import turtle t = turtle.Pen() for i in range(5): t.penup() t.goto(0, -i*30) t.pendown() t.circl ...

  2. 码云git 使用配置

    码云git 使用配置:www.gitee.com安装:1.Git-2.18.0-64-bit.exe:2.GitExtensions-2.51.04.msi:3.GitExtensionsVSIX.v ...

  3. ffmpeg转码器移植VC的project:ffmpeg for MFC

    本文介绍一个自己做的FFMPEG移植到VC下的开源project:ffmpeg for MFC.本project将ffmpegproject中的ffmpeg转码器(ffmpeg.c)移植到了VC环境下 ...

  4. Python2.x与3.x版本区别

    Python2.x与3.x版本区别 1.print 函数 print语句没有了,取而代之的是print()函数. Python 2.6与Python 2.7部分地支持这种形式的print语法.在Pyt ...

  5. SpringBoot系统列 4 - 常用注解、拦截器、异常处理

    在前面代码基础上进行改造: 1.SpringBoot常用注解 @SpringBootApplication :指定SpringBoot项目启动的入口,是一个复合注解,由@Configuration.@ ...

  6. UITableView 自定义多选

    前言 在上一篇文章中介绍了UITableView的多选操作,有提到将 return UITableViewCellEditingStyleDelete | UITableViewCellEditing ...

  7. linux中,history命令,显示时间戳?操作人?IP地址?

    需求描述: 在linux环境中,有的时候为了审计的需要,要记录谁什么时间从什么IP登录,执行了什么命令,bash的history命令就能够记录这些信息,但是在默认的情况下,是不记录时间的,所以呢,在这 ...

  8. unity3D内嵌android项目

    1.从u3d中导出android工程  工程名为 HS5 2.as(AndroidStudio简写)中新建android工程 MyAndroid 3.HS5 以module方式导入到MyAndroid ...

  9. SpringBoot------集成MyBatis报错

    在spring boot启动main方法所在的类中加入 @MapperScan注入后报错: Invalid default: public abstract java.lang.Class org.m ...

  10. css布局 - 两栏自适应布局的几种实现方法汇总

    这种两列布局的样式是我们在平时工作中非常常见的设计,同时也是面试中要求实现的高频题.很有必要掌握以备不时之需: 整理了几种实现方法,当然,风骚的代码不止这几种能实现,欢迎各位的补充. 方法汇总目录 简 ...