|
From: Brice R. <bri...@gm...> - 2006-11-02 05:03:08
|
Hello,
I have a USB barcode scanner that acts like a keyboard: whenever a
barcode is read it sends the code where the keyboard would normally
write. I am writing some code in C to read from the scanner. This is
my first application using libusb, and it is the first time I am
working with a USB device.
I found out how to open the usb device, detach the kernel driver and
claim the interface. I got this information by reading the code of the
phidget library that makes use of libusb. To read from the phidgets
they call usb_interrupt_read. So I am doing the same but I get
nothing, although I am managing to read 8 bytes, always the same: 0 0
30 0 0 0 0 0
What's wrong? I pasted my code below (I removed error checking code).
One more question (not so important):
When detaching the kernel driver, it returns an error if it had been
previously detached. How to check that and make the distinction with
other kind of errors?
Or:
How to reattach the driver at the end?
Thanks,
Brice
int main(){
usb_dev_handle *udev;
struct usb_bus *bus;
struct usb_device *dev;
char barcode[8];
// search for the scanner, open the device and claim interface
usb_init();
usb_find_busses();
usb_find_devices();
for( bus=usb_get_busses(); bus; bus = bus->next )
for( dev = bus->devices; dev; dev = dev->next )
if( dev->descriptor.idVendor==0x5e0 && dev->descriptor.idProduct==0x200 )
udev = usb_open(dev);
usb_detach_kernel_driver_np(udev, 0);
usb_claim_interface(udev, 0);
//prompt scanner until we get 8 bytes.
while( usb_interrupt_read(udev,1,barcode,8,1000)!=8 );
int i;
printf("barcode: ");
for( i=0; i<8; i++ ) printf("%u ",barcode[i]);
printf("\n");
usb_release_interface(udev, 0);
usb_close(udev);
return 1;
}
|