libusb_bulk_transfer timeout while write

2.4k Views Asked by At

I have a USB printer device. I want to send file data to the USB printer from Linux. I am using libUsb for my code. I am getting timeout (libusb return value -7) always while sending. But I can able to send data in Windows for the same printer. What went wrong ? It seems ehci or uhci is not sending data to the printer. Please Help .

OS : Ubuntu 12.04 (32 Bit)

The below is my code snippet.

    dev_handle = libusb_open_device_with_vid_pid(ctx, PRINTER_VID, PRINTER_PID); 

if (dev_handle == NULL)
{
    cout << "Cannot open device" << endl;
    libusb_free_device_list(devs, 1); //free the list, unref the devices in it
    return;
}
else
{
    cout << "Device Opened" << endl;
}

if (libusb_kernel_driver_active(dev_handle, 0) == 1) //find out if kernel driver is attached
{ 
    cout << "Kernel Driver Active" << endl;
    if (libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
        cout << "Kernel Driver Detached!" << endl;
}
r = libusb_claim_interface(dev_handle, 0); 
if (r < 0) 
{
    cout << "Cannot Claim Interface" << endl;
    return 1;
}
cout << "Claimed Interface" << endl;
cout << "Writing Data..." << endl;
memset(data_buffer,0,64);

while(fgets((char *)data_buffer,64,fp))
{
    errno = 0;
    r = libusb_bulk_transfer(dev_handle,0x081 | LIBUSB_ENDPOINT_OUT, data_buffer, 64,&actual, 10); 
    cout<<"The return value of r is "<<r<< "::::" << actual << endl ;
    memset(data_buffer,0,64);
}

The output is

The return value of r is -7::::0
The return value of r is -7::::0
1

There are 1 best solutions below

0
On

Try to execute following comamnd from superuser and then reconnect device.

echo 0 > /sys/bus/usb/drivers_autoprobe

It helped me with some devices.