永发信息网

java中有没有通过USB传输数据的库或者API之类的,要求一个终端发送一定格式的usb数据另一个终端能收到

答案:1  悬赏:60  手机版
解决时间 2021-11-29 11:02
  • 提问者网友:ミ烙印ゝ
  • 2021-11-28 16:06
java中有没有通过USB传输数据的库或者API之类的,要求一个终端发送一定格式的usb数据另一个终端能收到
最佳答案
  • 五星知识达人网友:雪起风沙痕
  • 2021-11-28 17:38
High-level (javax-usb) API
import javax.usb.*;
import java.util.List;

public class TraverseUSB
{
 public static void main(String argv[])
 {
  try
  {
   // Access the system USB services, and access to the root 
   // hub. Then traverse through the root hub.
   UsbServices services = UsbHostManager.getUsbServices();
   UsbHub rootHub = services.getRootUsbHub();
   traverse(rootHub);
  } catch (Exception e) {}
 }

 public static void traverse(UsbDevice device)
 {
  if (device.isUsbHub())
  { 
   // This is a USB Hub, traverse through the hub.
   List attachedDevices = ((UsbHub) device).getAttachedUsbDevices();
   for (int i=0; i
   {
    traverse((UsbDevice) attachedDevices.get(i));
   }
  }
  else
  {
   // This is a USB function, not a hub.
   // Do something.
  }
 }
 public static void testIO(UsbDevice device)
{
 try
 {
  // Access to the active configuration of the USB device, obtain 
  // all the interfaces available in that configuration.
  UsbConfiguration config = device.getActiveUsbConfiguration();
  List totalInterfaces = config.getUsbInterfaces();

  // Traverse through all the interfaces, and access the endpoints 
  // available to that interface for I/O.
  for (int i=0; i
  {
   UsbInterface interf = (UsbInterface) totalInterfaces.get(i);
   interf.claim();
   List totalEndpoints = interf.getUsbEndpoints();
   for (int j=0; j
   {
    // Access the particular endpoint, determine the direction
    // of its data flow, and type of data transfer, and open the 
    // data pipe for I/O.
    UsbEndpoint ep = (UsbEndpoint) totalEndpoints.get(i);
    int direction = ep.getDirection();
    int type = ep.getType();
    UsbPipe pipe = ep.getUsbPipe();
    pipe.open();
    // Perform I/O through the USB pipe here.
    pipe.close();
   }
   interf.release();
  }
 } catch (Exception e) {} 
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯