我一直在使用 usb4java 库,并且了解了很多 USB 设备的通信方式。
我在我的 Web 应用程序中使用 usb4java 低级 API (LibUsb 1.0) 库来读取 Raspberry Pi 中的条形码扫描仪输入。Raspberry Pi 通过Raspbian发行版更新到最新的软件包。
我有以下示例说明如何通过以下资源在 Java 中实现此过程:
通过 USB 从 Adafruit Trinket 读取数据
问题:
我可以成功地做到:
- 列出所有设备
- 根据供应商 ID 和产品 ID 选择我的设备
- 打开设备
- 分离内核驱动程序
- 认领设备
当读取扫描仪发送的设备数据时,我总是收到超时错误(-7)。
Opening device...
Claiming device...
Found Device, Data below
Failure reading data -7
Failure reading data -7
Failure reading data -7
Failure reading data -7
Failure reading data -7
Failure reading data -7
Failure reading data -7
Failure reading data -7
Failure reading data -7
Failure reading data -7
Failure reading data -7
Releasing device interface...
Closing...
Exiting...
下面的示例,调用该方法 10 次以获得响应,并有 5 秒的超时
for(int i=10; i >= 0; --i)
{
writeOutput(read(DeviceHandle, this.bufferSize, this.timeout, this.transfer));
}
主类:缺少定义的变量,只是为了说明过程
// Create the libusb context
Context context = new Context();
// Initialize the libusb context
int result = LibUsb.init(context);
if ((result != LibUsb.SUCCESS) || (result < 0))
{
writeOutput("Unable to initialize libusb " + result);
}
else
{
// Try to open the device.
DeviceHandle DeviceHandle = new DeviceHandle();
// Get device
Device device = findDevice((short) 0x0c2e, (short) 0x0200);
if (device == null)
{
writeConsole("Unable to find device");
}
else
{
writeConsole("Device found");
}
// Open I/O with the device handle
result = LibUsb.open(device, DeviceHandle);
if (result < 0)
{
writeOutput("Unable to open device");
DeviceHandle = null;
}
else
{
writeOutput("Opening device");
try
{
boolean detach = LibUsb.hasCapability(LibUsb.CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
int detachActive = LibUsb.kernelDriverActive(DeviceHandle, this.interFace);
// Detach the kernel driver
if ((detach) || (detachActive == 1))
{
result = LibUsb.detachKernelDriver(DeviceHandle, this.interFace);
if (result != LibUsb.SUCCESS)
{
writeOutput("Cannot detach kernel from this interface (" + this.interFace + ") error " + result);
}
else
{
writeOutput("Detaching kernel driver (" + this.interFace + ")");
}
}
//set configuration
result = LibUsb.setConfiguration(DeviceHandle, this.configuration);
if (result != LibUsb.SUCCESS)
{
writeOutput("Cannot set device configuration (" + this.configuration + ") error " + result);
}
else
{
writeOutput("Setting device configuration (" + this.configuration + ")");
// Claim the device
result = LibUsb.claimInterface(DeviceHandle, this.interFace);
if (result != LibUsb.SUCCESS)
{
writeOutput("Cannot open device interface (" + this.interFace + ") error " + result);
}
else
{
try
{
writeOutput("Claiming device on interface (" + this.interFace + ")");
switch(this.transfer)
{
case 0:
writeOutput("Set to Interrupt Transfer type");
break;
case 1:
writeOutput("Set to Bulk Transfer type");
break;
}
for (int i=10; i >= 0; --i)
{
writeOutput(read(DeviceHandle, this.bufferSize, this.timeout, this.transfer));
}
}
finally
{
result = LibUsb.releaseInterface(DeviceHandle, this.interFace);
if (result != LibUsb.SUCCESS)
{
writeOutput("Failure releasing device interface (" + this.interFace + ") error " + result);
}
else
{
writeOutput("Releasing device interface (" + this.interFace + ")");
}
if ((detach) || (detachActive == 1))
{
result = LibUsb.attachKernelDriver(DeviceHandle, this.interFace);
if (result != LibUsb.SUCCESS)
{
writeOutput("Cannot attach kernel driver back to the device (" + this.interFace + ") error " + result);
}
else
{
writeOutput("Re attaching kernel driver back (" + this.interFace + ")");
}
}
}
}
}
}
finally
{
LibUsb.close(DeviceHandle);
writeOutput("Closing... ");
}
}
LibUsb.exit(context);
writeOutput("Exiting... ");
}
**我获得同步读取的方法**
// Read data from device after claiming it
public static String read(DeviceHandle handle, int size, long timeout, int readType) {
byte endpointId = (byte) 0x81;
StringBuilder stringBuilder = new StringBuilder();
boolean started = false;
while(true)
{
ByteBuffer buffer = BufferUtils.allocateByteBuffer(size).order(ByteOrder.LITTLE_ENDIAN);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result;
switch(readType)
{
case 0:
result = LibUsb.interruptTransfer(handle, endpointId, buffer, transferred, timeout);
break;
case 1:
result = LibUsb.bulkTransfer(handle, endpointId, buffer, transferred, timeout);
break;
default:
result = LibUsb.interruptTransfer(handle, endpointId, buffer, transferred, timeout);
break;
}
if (result != LibUsb.SUCCESS)
{
return "Failure reading data with"
+ " buffer size:"
+ size + " bytes timeout:"
+ timeout + " ms error code:"
+ result;
}
else
{
CharBuffer charBuffer = buffer.asCharBuffer();
while(charBuffer.hasRemaining())
{
char nextChar = charBuffer.get();
if (nextChar == '\n')
started = true; //Start Character
else if(nextChar == '\r' && started)
return stringBuilder.toString(); //End Character
else if(started)
stringBuilder.append(nextChar);
}
}
}
}
这是 USB 设备的描述符
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 1.10
bDeviceClass 0 Per Interface
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x0c2e
idProduct 0x0200
bcdDevice 53.32
iManufacturer 1 Metrologic
iProduct 2 Metrologic Scanner
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 34
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 3
bmAttributes 0x80
(Bus Powered)
bMaxPower 300mA
extralen 0
extra:
Interface:
numAltsetting 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 3 HID
bInterfaceSubClass 1
bInterfaceProtocol 1
iInterface 0
extralen 9
extra:
09 21 11 01 00 01 22 3f 00
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 8
bInterval 10
extralen 0
extra:
--- Device END ---
我使用的接口为 0,IN 端点为 0x81,供应商 ID 为 0x0c2e,产品 ID 为 0x0200。此外,我还将LibUsb.bulkTransfer()更改为LibUsb.interruptTransfer()因为设备只接受中断传输。正如wMaxPacketSize所说,缓冲区设置为 8 个字节 。
我已经尝试并消除了以下测试:
- 将缓冲区传输大小从 1 ~ 64 Bytes 更改
- 使用 LibUsb.bulkTransfer()
- 将超时时间从 100 ~ 20000 毫秒
- 添加了 LibUsb.clearhalt();
我该如何克服这个问题?我错过了什么?