基于usb4java的usb通讯
下载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通讯的更多相关文章
- 基于Linux的USB 主/从设备之间通讯的三种方式
转载:http://archive.eet-china.com/www.eet-china.com/ART_8800323770_617693_TA_eda530e7.HTM 随着简单易用的USB接口 ...
- .Net开发笔记(十五) 基于“泵”的TCP通讯(接上篇)
上一篇博客中说了基于“泵”的UDP通讯,附上了一个Demo,模拟飞鸽传书的功能,功能不太完善,主要是为了说明“泵”在编程中的应用.本篇文章我再附上一个关于TCP通讯的两个Demo,也都采用了“泵”模式 ...
- C#:基于WMI查询USB设备信息 及 Android设备厂商VID列表
/* ---------------------------------------------------------- 文件名称:WMIUsbQuery.cs 作者:秦建辉 MSN:splashc ...
- Android基于XMPP的即时通讯3-表情发送
这篇博文主要讲表情发送的一些东西. 参考:Android基于XMPP的即时通讯1-基本对话 1.准备好资源文件 采用的是emoji的表情,我打包好了,下载地址:http://files.cnblogs ...
- Android基于XMPP的即时通讯2-文件传输
本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输 1.初始化文件传输管理类 public static FileTransferManager get ...
- C#:基于WMI查询USB设备
来源:http://blog.csdn.net/jhqin/article/details/6734673 /* ------------------------------------------- ...
- USB通讯协议 && 数据传输
USB2.0通讯协议(spalish) 1.包(packet) 包是USB系统中信息传输的基本单元,所有数据都是经过打包后在总线上传输的.USB包由五部分组成,同步字段(sync).包标识符(PI ...
- 基于“泵”的TCP通讯(接上篇)
基于“泵”的TCP通讯(接上篇) 上一篇博客中说了基于“泵”的UDP通讯,附上了一个Demo,模拟飞鸽传书的功能,功能不太完善,主要是为了说明“泵”在编程中的应用.本篇文章我再附上一个关于TCP通讯的 ...
- 基于libUSB的USB设备固件更新程序(下载数据)(转)
源:基于libUSB的USB设备固件更新程序(下载数据) 本文紧接上一篇日志:基于libUSB-Win32的USB设备固件更新程序(前言),相关背景以及起因等,此处不再赘述,如感兴趣请移步. libU ...
随机推荐
- Linux下C语言执行shell命令
有时候在代码中需要使用到shell命令的情况,下面就介绍一下怎么在C语言中调用shell命令: 这里使用popen来实现,关于popen的介绍,查看 http://man7.org/linux/man ...
- 【Linux】解决"no member named 'max_align_t'
编译遇到错误: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.1/../../../../include/c++/5.4.1/cstddef:51:11: erro ...
- centos图形界面,vncserver
yum -y groupinstall "Server with GUI" RHEL7 安装图形界面1. 以root角色运行以下命令来安装TigerVNC server yum i ...
- HTTP 07 追加协议与 Ajax
Ajax 解决方法 是一种有效的利用 JavaScript 和 DOM 的操作, 以达到局部Web 页面替换加载异步的通信手段.以达到局部web页面替换加载异步通信手段.和以前的同步通信相比, 由于它 ...
- 基数计数——HyperLogLog
所谓的基数计数就是统计一组元素中不重复的元素的个数.如统计某个网站的UV,或者用户搜索网站的关键词数量:再如对一个网站分别统计了三天的UV,现在需要知道这三天的UV总量是多少,怎么融合多个统计值. 1 ...
- linux下python3调用c代码或者python3调用c++代码
前几篇的blog都是为了这个实验做基础,先说 原因是python调用数据库150w条数据22s,然后处理数据,其实就2个简单的for循环,65s 需求: 1. python调用c++函数 2. c++ ...
- Javascript中Promise对象的实现
http://segmentfault.com/a/1190000000684654 http://www.infoq.com/cn/news/2011/09/js-promise/
- THINKPHP5近期暴露的漏洞
这个THINKPHP5的漏洞涉及好几个版本,我测试中5.0.21和5.0.22都有,据说是5.0 ~ 5.0.23之间的版本都存在,这个漏洞可以执行写文件的操作. 当然了,赶紧升级框架到安全版本是比较 ...
- HttpServletResponse实现文件下载
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- 如何查看github排行热度
github热门趋势 https://github.com/trending github star排行榜 github输入:star:>数字,来查看star数的仓库: 输入:location: ...