由于没有厂家的驱动,需要自己对usb口进行控制,主要有两种方式:
1.手写驱动,加载设备。2.采用libusb库进行无驱动配置
由于第一种较为麻烦,我采用了第二种。
主要步骤:
1.libusb配置:
官网下载,安装,发现config有问题,先安装依赖项。
主要参考:https://www.cnblogs.com/ettie999/p/8142973.html
2.测试example里面的listudev.c,可运行代表安装成功
3.Qt开发:
1.首先需要在.pro文件中添加路径,用来访问libusb的库:
INCLUDEPATH += /usr/local/include?libusb-1.0
LIBS += -L"/usr/local/lib" -lusb-1.0
2.编写代码:
libusb_context *context = NULL;libusb_device_handle *dev_handle = NULL;libusb_device **devs;libusb_device *dev=NULL;int rc = 0;ssize_t count;//*****************Initial *********rc = libusb_init(&context);// cout << "rc : " << rc << endl;assert(rc==0);//Enable debug
#ifndef NDEBUGlibusb_set_debug(context,LIBUSB_LOG_LEVEL_WARNING);
#endif//Get device listcount = libusb_get_device_list(context,&devs);cout << "count : " << count << endl;assert(count>0);for(ssize_t idx=0;idx<count;idx++){libusb_device *device = devs[idx];libusb_device_descriptor desc = {0};rc = libusb_get_device_descriptor(device,&desc);assert(rc ==0);if((desc.idVendor == 0x0483) && (desc.idProduct == 0x5743)){cout << "get the device " << endl;dev = device;}// printf("Vendor:Device = %04x:%04x\n",desc.idVendor,desc.idProduct);cout << "Vendor:Device = " << hex << desc.idVendor << "___" << desc.idProduct << endl;}//open usb devicerc = libusb_open(dev,&dev_handle);//dev_handle = libusb_open_device_with_vid_pid(context,0x0483,0x5743);//dev_handle = libusb_open_device_with_vid_pid(context,0x17ef,0x602d);if(dev_handle == NULL){cout << "Cann't Open device" << endl;return;}elsecout << "Open Success " << endl;// assert(dev_handle != NULL);//set privilige//Write dataunsigned char *data = new unsigned char[0x30];int actual;data[0] = 0x0a;data[1] = 0x1b;data[2] = 0x21;data[3] = 0x00;data[4] = 0x55;data[6] = 0x66;data[7] = 0x23;data[8] = 0x45;data[9] = 0x0d;data[10] = 0x0a;if(libusb_kernel_driver_active(dev_handle,0) == 1){cout << "Kernel_driver_active" << endl;rc = libusb_detach_kernel_driver(dev_handle,0);cout << "detach_kernel_driver __rc: " << rc << endl;assert(rc==0);}rc = libusb_claim_interface(dev_handle,0);cout << "libusb_claim_interface __rc: " << rc << endl;// assert(rc<0);// transfer *******libusb_config_descriptor* config_descriptor_in = NULL;libusb_get_config_descriptor(dev,0,&config_descriptor_in);// rc = libusb_control_transfer(dev_handle,0x21,0x09,0x0300,0x00,data+1,0x20,10000);// rc = libusb_bulk_transfer(dev_handle,(64|LIBUSB_ENDPOINT_OUT),data,4,&actual,0);rc = libusb_bulk_transfer(dev_handle,0x02,data,12,&actual,0);//rc = libusb_bulk_transfer(dev_handle,config_descriptor_in->interface->altsetting->endpoint->bEndpointAddress,data,4,&actual,10000);cout << "libusb_bulk_transfer __rc: " << rc << endl;cout << "actual num : " << actual << endl;assert(rc!=0 || actual !=5);rc = libusb_release_interface(dev_handle,0);cout << "libusb_release_interface __rc: " << rc << endl;//assert(rc!=0);cout << "close devices " << endl;//Free device listlibusb_free_device_list(devs,1);libusb_attach_kernel_driver(dev_handle,0);libusb_close(dev_handle);libusb_exit(context);
用lsusb可查到usb设备的VID和PID,然后通过官网可以查询对应函数的用法,从而实现控制。
http://libusb.sourceforge.net/api-1.0/libusb_api.html