java - 使用 LibUsb 通过 Java 中的 USB 发送数据时出错(未找到实体)

标签 java usb communication libusb

我尝试使用 LibUsb4Java 通过 USB 发送数据,但在批量传输中遇到错误。我找不到错误,请求您的帮助。

第一个方法返回目标设备。这是一个frdm板。

尝试在bulkTransfer方法中发送数据时出现错误“USB错误5:控制传输失败:找不到实体”。

可能是什么问题?

我正在 OS X v10.10 (Yosemite) 上进行开发:

// Create the libusb context
Context context = new Context();

// Initialize the libusb context
int result = LibUsb.init(context);
if (result < 0)
{
    throw new LibUsbException("Unable to initialize libusb", result);
}

// Read the USB device list
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(context, list);
if (result < 0)
{
    throw new LibUsbException("Unable to get device list", result);
}

try
{
    // Iterate over all devices and list them
    for (Device device: list)
    {
        DeviceDescriptor descriptor = new DeviceDescriptor();
        result = LibUsb.getDeviceDescriptor(device, descriptor);
        if (result < 0)
        {
            throw new LibUsbException(
                "Unable to read device descriptor", result);
        }
        if(descriptor.idVendor() == 0x1357 && descriptor.idProduct() == 0x0089)
        {
           frdmBoard = device;
        }
    }
   DeviceHandle handle = new DeviceHandle();
   int resultOpen = LibUsb.open(frdmBoard, handle);
   if (resultOpen != LibUsb.SUCCESS)
      throw new LibUsbException("Unable to open USB device", resultOpen);
   try
   {
       ByteBuffer buffer = ByteBuffer.allocateDirect(8);
       buffer.put(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
       IntBuffer transfered = IntBuffer.allocate(1);
       byte endpoint = (byte)LibUsb.getDeviceAddress(frdmBoard);
       int intResult = LibUsb.bulkTransfer(handle, endpoint, buffer, transfered, 1L);
       try{
          if (intResult != LibUsb.SUCCESS)
             throw new LibUsbException("Control transfer failed", intResult);
       }
       catch(Exception e)
       {
          e.printStackTrace();
       }
   }
   finally
   {
      LibUsb.close(handle);
   }
}
finally
{
   LibUsb.freeDeviceList(list, true);
}

最佳答案

按照快速入门指南 http://usb4java.org/quickstart/libusb.html 进行操作我认为您错过了领取该界面的机会。在“同步 I/O”部分中,指南说

This examples sends 8 bytes to a claimed interface using a control transfer:

声称该设备应该可以解决您的问题:

result = LibUsb.claimInterface(handle, interfaceNumber);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to claim interface", result);

如果设备已被内核驱动程序注册,请记住分离该设备。快速入门指南中的这个改编解决方案在 Ubuntu 14.04 上对我有用:

 boolean detach = (LibUsb.kernelDriverActive(handle, 0) == 1);
 if (detach) {
     result = LibUsb.detachKernelDriver(handle,  interfaceNumber);
     if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to detach kernel driver", result);
 }

关于java - 使用 LibUsb 通过 Java 中的 USB 发送数据时出错(未找到实体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28875044/

相关文章:

C#获取连接到PC的可移植设备

Linux Bash 输入

iphone - 两个 iOS 设备之间的通信

c# - C# async 和 Java ExecutorService 的区别

java - Android 项目 NullPointerException 尝试调用位图图像上的 Files.readAllBytes(Paths.get ("Image Path")))

linux - 一个 USB 2.0 集线器上的两个网络摄像头 - 在 Windows 中工作但在 Linux 中不工作

windows - 在 Windows 中不使用盘符访问 USB 设备

android - 将数据从线程传递到 Activity

java - 停止通过IP地址访问网站

java - 如何创建 get() 调用返回可选包装值的自定义映射?