还有在QT下使用libusb库的时候一定要在(项目名吵租态.pro)文件加上:
[cpp] view plain copy
INCLUDEPATH += /usr/include/libusb-1.0
LIBS += -L"/usr/lib/x86_64-linux-gnu" -lusb-1.0
否则程序无法找到该库的路径。
[cpp] view plain copy
libusb_device **devs
libusb_device_handle *dev_handle
libusb_context *ctx=NULL
struct libusb_device_descriptor desc
int r
ssize_t cnt
r=libusb_init(&ctx)
if(r<0)
{
//printf("init error")
QMessageBox::about(this,"haha","initialerror")
return
}
libusb_set_debug(ctx,3)
//cnt=libusb_get_device_list(ctx,&devs)
dev_handle=libusb_open_device_with_vid_pid(ctx,VID,PID)
if(dev_handle==NULL)
QMessageBox::about(this,"haha","open my dev error")
else
QMessageBox::about(this,"haha","open my dev success")
//libusb_free_device_list(devs,1)
if(libusb_kernel_driver_active(dev_handle,0)==1)
{
QMessageBox::about(this,"haha","kernel driver active")
if(libusb_detach_kernel_driver(dev_handle,0)==0)
QMessageBox::about(this,"haha","kernel driver detached")
}
r=libusb_claim_interface(dev_handle,0)
if(r<0)
{
QMessageBox::about(this,"haha","cannot claim interface")
return
}
QMessageBox::about(this,"haha","claimed interface")
// start use this usb
unsigned char dataOut[0x30]={0}
dataOut[0]=0x00
dataOut[1]=0x02
dataOut[2]=0x08
dataOut[3]=0x00
dataOut[4]=0x02
dataOut[5]=0x00
dataOut[6]=0xb2
dataOut[7]=0x30
dataOut[8]=dataOut[2]^dataOut[3]^dataOut[4]^dataOut[5]^dataOut[6]^dataOut[7]
//r=libusb_interrupt_transfer(dev_handle,LIBUSB_ENDPOINT_OUT, dataOut,9, &actual_length, 0)
//r=libusb_bulk_transfer(dev_handle,LIBUSB_ENDPOINT_OUT,dataOut,9,&actual_length,5000)
//此设备必须用控制传输
r=libusb_control_transfer(dev_handle, 0x21, 0x09, 0x0300, 0x00, dataOut+1, 0x20, 1000)
unsigned char dataRead[0x30]={0}
r=libusb_control_transfer(dev_handle, 0xa1, 0x01, 0x0300, 0x00, dataRead, 0x20, 1000)
if(r<0)
{
QMessageBox::about(this,"haha","transfer data error")
}else
{
QMessageBox::about(this,"haha","transfer data success")
}
//release everything
r=libusb_release_interface(dev_handle,0)
libusb_attach_kernel_driver(dev_handle,0)
libusb_close(dev_handle)
libusb_exit(ctx)
首先需要了解Linux输入子系统,因为鼠标是输入设备,一种input_device。然后需伏高要了解USB设备驱动。使用了USB接橡段口,urb来获得鼠标的数据,然后把数据往上层应用提交。可以参看内核已经有的USB鼠标驱动,位置是\linux-3.0.86\drivers\hid\usbhid\usbmouse.c。linux-3.0.86是Linux内核的根目录。并不难。至于哪些书,可以看‘Linux设备驱动程序’这缺如尺本书,里头主要知识都有讲到。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)