我有一个 USB 打印机设备。我想从 Linux 向 USB 打印机发送文件数据。我正在为我的代码使用 libUsb。我总是在发送时超时(libusb 返回值 -7)。但我可以在 Windows 中为同一台打印机发送数据。什么地方出了错 ?似乎 ehci 或 uhci 没有向打印机发送数据。请帮忙 。
操作系统:Ubuntu 12.04(32 位)
下面是我的代码片段。
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 return value of r is -7::::0
The return value of r is -7::::0