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

    pom

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.usb4java</groupId>
    4. <artifactId>usb4java-javax</artifactId>
    5. <version>1.3.0</version>
    6. </dependency>
    7. </dependencies>