libusb.dylib 初体验

来源:互联网 发布:淘宝网秋季男装外套 编辑:程序博客网 时间:2024/06/05 20:11

macpro Darwin kernel,对这个linux内核并不了解,但是自己的实验项目中又要开发usb Driver,窥视了一下Darwin后,还是决定先不触碰kernel(反正早晚要碰头尴尬)绕过kernel,使用个封装库来达到目的吧 大笑

libusb.dylib 是一个用来开发usb驱动(或者其他功能的比较方便的库),macOS的用户可以用port来安装,或者去sourcerage(估计这个拼错了)下载。libusb on source

源码估计看一遍就知道怎么使用了,使用起来还是非常方便简单的,即使不懂硬件也可以轻松使用(有点做广告的嫌疑),但是USB的工作原理还是要明白的。USB的教程网上有很多,这里就不多说了,咱们直接看libusb吧

libusb_device_handle* discover(int vendor , int product){ofstream errorLog;libusb_device** li;libusb_init(NULL);int cnt = libusb_get_device_list(NULL , &li);for(int i = 0;i<cnt;++i){libusb_device_descriptor* desc = new libusb_device_descriptor;memset(desc , 0 , 128);libusb_get_device_descriptor(li[i] , desc);cout<<"the "<<i<<" device:"<<endl;cout<<"device vendor id: "<<desc->idVendor<<"device product id: "<<desc->idProduct<<endl;delete desc;}libusb_device_handle* han;han = libusb_open_device_with_vid_pid(NULL , product , vendor);if(!han){cout<<"warning:open device wrong"<<endl;libusb_free_device_list(li , 1);libusb_exit(NULL);string err = "error in line:245 , open device error\n";errorLog.open("/Developer/program_for_vim/testusb/errorlog.txt");errorLog.write(err.c_str() , strlen(err.c_str()));errorLog.close();return NULL;}libusb_free_device_list(li , 1);libusb_exit(NULL);return han;}void transData(libusb_device_handle* han){if(!han){string err = "there is no device handle";cout<<err.c_str()<<endl;return ;}const int inFlag = LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE;const int outFlag = LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE;const int getReport = 0x09;const int setReport = 0x19;const int reportTypeIn = 0x01;const int reportTypeOut = 0x02;const int reportTypeFeature = 0x03;const int maxIn = 1;const int maxOut = 1;const int interface = 0;const int timeOut = 5000;ofstream errorLog;int resFlag = -1;int ifSent , ifRevieved;unsigned char dataOut[maxOut];unsigned char dataIn[maxIn];libusb_init(NULL);libusb_detach_kernel_driver(han , interface);resFlag = libusb_claim_interface(han , interface);if(resFlag >= 0){for(int i = 0;i<maxOut;++i){dataOut[i] = 0x01 + i;}ifSent = libusb_control_transfer(han,    //device handleoutFlag, //Control retuest out flagsetReport,(reportTypeFeature<<8)|0x00,0, //indexdataOut,                     //transDatasizeof(dataOut),timeOut);cout<<"sending data:"<<endl;if(ifSent >= 0){cout<<"ifSent"<<endl;}else {string err = "error in line: 308\n";errorLog.open("/Developer/program_for_vim/testusb/errorlog.txt");errorLog.write(err.c_str() , strlen(err.c_str()));errorLog.close();libusb_release_interface(han , interface);libusb_exit(NULL);return;}ifRevieved = libusb_control_transfer(han,inFlag,getReport,(reportTypeFeature<<8)|0x00,interface,dataIn,maxIn,      //when recieving data , make it bigtimeOut);if(ifRevieved > 0){cout<<"ifRevieved"<<endl;}else{string err = "recieving data error";errorLog.open("/Developer/program_for_vim/testusb/errorlog.txt");errorLog.write(err.c_str() , strlen(err.c_str()));errorLog.close();libusb_release_interface(han , interface);libusb_exit(NULL);return;}}else {cout<<"warning:usb interface wrong"<<endl;string err = "error in line 295 , usb interface error\n";errorLog.open("/Developer/program_for_vim/testusb/errorlog.txt");errorLog.write(err.c_str() , strlen(err.c_str()));errorLog.close();libusb_release_interface(han , interface);libusb_exit(NULL);return;}libusb_release_interface(han , interface);libusb_init(NULL);}int main(int argc , const char* argv[]){libusb_init(NULL);int vendor = 0x2303;int product = 0x067b;libusb_device_handle* han;han = discover(vendor , product);transData(han);system("date");return 1;}

上面的code只是实现了由server端到client的通信(因为我开发板上还没有程序,所以板上具体反应还看不到,过几天再贴出来吧大笑),上面的只是小小的测试,估计里面有不少地方欠妥,还请大牛指导。

最后自曝一下自己的开发界面,对于我开发C++和Python来说,macOS确实使用的十分顺手。


原创粉丝点击