如何在Linux下利用Libusb与HID USB设备通信
1个回答
展开全部
由于公司的产品,导致我在Linux下写usb通信的代码出现了许多的问题,最后发现这些设备只能通过控制传输来操作设备,所以后面的问题就迎刃而解了。现在贴上当时在QT下写的代码,以防日后再碰到此类问题的时候翻查。
还有在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);
还有在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);
光派通信
2024-09-03 广告
2024-09-03 广告
对光传输设备进行调试和维护,需要掌握以下关键点:1. **熟悉设备性能**:深入了解OTN/DWDM等光传输产品的性能特点,确保能够熟练使用设备。2. **按规范操作**:依据产品说明书和维护手册进行调试和维护,确保操作步骤正确无误。3. ...
点击进入详情页
本回答由光派通信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询