我一直在使用usb4java库,并且了解了很多USB设备的通信方式。
我在我的网络应用程序中使用usb4java低级API(LibUsb 1.0)库来读取Raspberry Pi中的条形码扫描程序输入。 Raspberry Pi通过Raspbian发行版更新为最新的包。
我有以下关于如何使用以下资源在Java中实现此过程的示例:
Read data from an Adafruit Trinket over USB
问题:
我可以成功地做到:
当需要读取扫描仪发送的设备数据时,我总是会收到超时错误(-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(),因为设备仅接受中断传输。缓冲区设置为8字节,如 wMaxPacketSize 所示。
我已经尝试并取消了以下测试:
我如何克服这个问题?我错过了什么吗?