gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:55:50 25 4
gpt4 key购买 nike

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

第一个方法返回目标设备。这是一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com