我从上上个礼拜开始接触,公司需要,所以开始在网上搜索相关资料,但是都没有找到合适的范例,但万幸终于测试出合适的代码。
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.usb.*;
public class UsbTest {
//下边两个参数为系统中usb设备的VID和PID 需要自行配置
private static short idVendor = ...;
private static short idProduct = ...;
public static void main(String[] args) {
try {
UsbPipe sendUsbPipe = new UsbTest().useUsb();
String string = "..." ;//传输内容
byte[] buff =string.getBytes();
sendMassge(sendUsbPipe, buff);
} catch (Exception e1) {
e1.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) {
//各位如果在此处获取不到设备请自行进行debug看map中存储的设备key到底是多少
iface = configuration.getUsbInterface((byte) 0);
} else {
return null;
}
iface.claim(new UsbInterfacePolicy()
{
@Override
public boolean forceClaim(UsbInterface usbInterface)
{
return true;
}
});
return iface;
}
public void receivedMassge(UsbPipe usbPipe) throws Exception{
StringBuffer all = new StringBuffer();
byte[] b = new byte[64];
int length;
while (true) {
length = usbPipe.syncSubmit(b);//阻塞
System.out.println("接收长度:" + length);
for (int i = 0; i < length; i++) {
//此处会打印所有的返回值 注意返回值全部也都是16进制的
//比如读取卡号或者身份证号时需要自行转换回10进制
//并进行补0操作,比如01转换为10进制会变成1需要补0 变成01
//不然得到的10进制返回值会有问题
System.out.print(Byte.toUnsignedInt(b[i])+" ");
all.append(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) throws UnsupportedEncodingException, UsbException {
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)
{
System.out.println(i+"___"+desc.idVendor()+"___"+desc.idProduct());
System.out.println("设备描述符:"+desc.toString());
System.out.println("厂商描述:"+device.getString(desc.iManufacturer())+"产品描述:"+device.getString(desc.iProduct()));
return device;
}
if (device.isUsbHub()){
device = findDevice((UsbHub) device);
if (device != null) return device;
}
}
return null;
}
}
这篇文章的数据接收部分并没有进行测试,原因是没有拿到对应的协议,后续会更新相关信息。
注意事项:
1:创建javax.usb.properties文件,将其置于resource目录下。
文件文件内容:
javax.usb.services = org.usb4java.javax.Services
2:pom文件引用的usb4java文件为:
<dependency>
<groupId>org.usb4java</groupId>
<artifactId>usb4java</artifactId>
<version>1.3.0</version>
</dependency>
<!-- For using usb4java with javax-usb -->
<dependency>
<groupId>org.usb4java</groupId>
<artifactId>usb4java-javax</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>javax.usb</groupId>
<artifactId>usb-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>javax.usb</groupId>
<artifactId>usb-api</artifactId>
<version>1.0.2</version>
<classifier>sources</classifier>
</dependency>
3:在使用IO时会有一个usb error,此处我查询大量相关资料显示,还需要安装UsbDk。以此对usb的反问进行重定向。
解释:UsbDk开发套件是由一套软件工具和模块组成,它把USB设备从PNP管理器和设备驱动分离,并提供了一系列API供用户层程序直接独占的访问USB设备,进行USB设备相关操作。
软件下载地址
4.在下载安装完毕后,在javax.usb.properties文件中添加一句,
org.usb4java.javax.useUSBDK = true
本文介绍了如何在Java中获取USB设备信息,重点提及了需要创建javax.usb.properties文件并放置于resource目录下,引用usb4java库,并且在遇到USB错误时,需要安装UsbDk进行设备访问。UsbDk是一个允许用户层程序直接访问USB设备的开发套件,提供了API供USB设备操作。
8325

被折叠的 条评论
为什么被折叠?



