基于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 ...
随机推荐
- 如何关闭windows server2012 80端口
Windows Server禁用本地端口的两种方法 这篇文章主要介绍了Windows Server 2008 禁用本地端口的两种方法,本文讲解了通过Windows防火墙禁用端口.通过IP安全策略禁用端 ...
- [C++]“error C2712: 无法在要求对象展开的函数中使用__try”解决方案
https://blog.csdn.net/shiqw5696/article/details/80664749 前段时间写了一篇关于C++异常捕获及异常处理的文章:c++异常捕获及异常处理try-t ...
- zoj 3430 Detect the Virus(AC自己主动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题
https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...
- windows redis 连接错误Creating Server TCP listening socket 127.0.0.1:637 9: bind: No error
报错信息如下: [10036] 30 Dec 10:23:49.616 # Creating Server TCP listening socket 127.0.0.1:637 9: bind: No ...
- MySQL优化之——备份和恢复
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46766919 备份 逻辑备份方法 使用MYSQLDUMP命令备份 MYSQLDUM ...
- 告别GOPATH,快速使用 go mod(Golang包管理工具)
https://studygolang.com/articles/17508?fr=sidebar 文中的wserver为module名,route为本地的包名,go.mod所在的目录名不一定非要和m ...
- 直播框架ijkplayer
直播框架ijkplayer [如何快速的开发一个完整的iOS直播app](播放篇) ijkplayer ios项目集成(图文详细版) 一步步搭建视频直播系统,基于LFLiveKit+ijkplayer ...
- springmvc 返回 404 解决
Idea Maven springmvc spring 项目搭建中/url 可以访问controller,并且能返回正确的ModelAndView,但是页面总是显示404 项目结构: web.xml ...
- gitlab+jenkins+tomcat CI/CD 部署
整个项目的框架为: gitlab的安装与使用(Centos7) gitlab的安装 新建yum源 vim /etc/yum.repos.d/gitlab-ce.repo [gitlab-ce] nam ...