使用 LibUsb 在 Java 中通过 USB 发送数据时出错(找不到实体)
Error sending data via USB in Java using LibUsb (Entity not found)
我尝试使用 LibUsb4Java 通过 USB 发送数据,但在 bulkTransfer 中出现错误。我找不到错误并请求您的帮助。
第一种方法returns目标设备。这是一个frdm板。
尝试在 bulkTransfer 方法中发送数据时出现错误 "USB error 5: Control transfer failed: Entity not found"。
可能是什么问题?
我正在 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 我认为您错过了获取界面的机会。在 "Synchronous 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);
}
我尝试使用 LibUsb4Java 通过 USB 发送数据,但在 bulkTransfer 中出现错误。我找不到错误并请求您的帮助。
第一种方法returns目标设备。这是一个frdm板。
尝试在 bulkTransfer 方法中发送数据时出现错误 "USB error 5: Control transfer failed: Entity not found"。
可能是什么问题?
我正在 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 我认为您错过了获取界面的机会。在 "Synchronous 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);
}