基于usb4java的usb通讯

转:https://www.cnblogs.com/wbjgogogo/p/8583622.html

下载java API及lib库地址:http://usb4java.org/index.html

1、导入所需要的库:

2、添加配置文件:文件名:javax.usb.properties;内容:javax.usb.services = org.usb4java.javax.Services  

目录结构如下:

3、具体代码实现如下:

复制代码
  1 package com.test.usb;
  2 
  3 import java.util.List;
  4 
  5 import javax.usb.UsbConfiguration;
  6 import javax.usb.UsbDevice;
  7 import javax.usb.UsbDeviceDescriptor;
  8 import javax.usb.UsbEndpoint;
  9 import javax.usb.UsbHostManager;
 10 import javax.usb.UsbHub;
 11 import javax.usb.UsbInterface;
 12 import javax.usb.UsbPipe;
 13 
 14 public class UsbUtil {
 15     private static short idVendor = (short)0x8888;
 16     private static short idProduct = (short)0x0006;
 17     
 18     public static void main(String[] args) {
 19         try {
 20             UsbPipe sendUsbPipe = new UsbUtil().useUsb();
 21             
 22             if (sendUsbPipe != null) {
 23                 byte[] buff = new byte[64];
 24                 for (int i = 0; i < 9; i++) {
 25                     buff[i] = (byte)i;
 26                     sendMassge(sendUsbPipe, buff);
 27                 }
 28             }
 29             
 30         } catch (Exception e) {
 31             e.printStackTrace();
 32         }
 33     }
 34     
 35     public UsbPipe useUsb() throws Exception{
 36         UsbInterface iface = linkDevice();
 37         if (iface == null) {
 38             return null;
 39         }
 40         UsbEndpoint receivedUsbEndpoint,sendUsbEndpoint;
 41         
 42         sendUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(0);
 43         if (!sendUsbEndpoint.getUsbEndpointDescriptor().toString().contains("OUT")) {
 44             receivedUsbEndpoint = sendUsbEndpoint;
 45             sendUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(1);
 46         } else {
 47             receivedUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(1);
 48         }
 49         
 50         //发送:
 51         UsbPipe sendUsbPipe =  sendUsbEndpoint.getUsbPipe();
 52         sendUsbPipe.open();
 53         
 54         //接收
 55         final UsbPipe receivedUsbPipe =  receivedUsbEndpoint.getUsbPipe();
 56         receivedUsbPipe.open();
 57         
 58         new Thread(new Runnable() {
 59             public void run() {
 60                 try {
 61                     receivedMassge(receivedUsbPipe);
 62                 } catch (Exception e) {
 63                     e.printStackTrace();
 64                 }
 65             }
 66         }).start();
 67         return sendUsbPipe;
 68     }
 69     
 70     public UsbInterface linkDevice() throws Exception{
 71 
 72         UsbDevice device = null;
 73         if (device == null) {
 74              device = findDevice(UsbHostManager.getUsbServices()
 75                         .getRootUsbHub());
 76         }
 77         if (device == null) {
 78             System.out.println("设备未找到!");
 79             return null;
 80         }
 81         UsbConfiguration configuration = device.getActiveUsbConfiguration();
 82         UsbInterface iface = null;
 83         if (configuration.getUsbInterfaces().size() > 0) {
 84             iface = (UsbInterface)configuration.getUsbInterfaces().get(0);
 85         } else {
 86             return null;
 87         }
 88         iface.claim();
 89         return iface;
 90     }
 91     
 92     public void receivedMassge(UsbPipe usbPipe) throws Exception{
 93         byte[] b = new byte[64];
 94         int length = 0;
 95         while (true) {
 96             length = usbPipe.syncSubmit(b);//阻塞
 97             System.out.println("接收长度:" + length);
 98             for (int i = 0; i < length; i++) {
 99                 System.out.print(Byte.toUnsignedInt(b[i])+" ");
100             }
101         }
102     }
103     
104     public static void sendMassge(UsbPipe usbPipe,byte[] buff)  throws Exception{
105         usbPipe.syncSubmit(buff);//阻塞
106         //usbPipe.asyncSubmit(buff);//非阻塞
107     }
108     
109     public UsbDevice findDevice(UsbHub hub)
110     {
111         UsbDevice device = null;
112         List list = (List) hub.getAttachedUsbDevices();
113         for (int i = 0;i<list.size();i++)
114         {
115             device = (UsbDevice)list.get(i);
116             UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
117             if (desc.idVendor() == idVendor && desc.idProduct() == idProduct) {return device;}
118             if (device.isUsbHub())
119             {
120                 device = findDevice((UsbHub) device);
121                 if (device != null) return device;
122             }
123         }
124         return null;
125     }
126 }
复制代码
View Code

 注意点:发送和接收数据长度要与设备匹配

猜你喜欢

转载自blog.csdn.net/ztx01001/article/details/88973779
usb