2013-02-25 99 views
0

我運行Ubuntu 12.04使用C++,我有以下代碼編譯器錯誤的libusb

#include <iostream> 
#include <libusb-1.0/libusb.h> 

using namespace std; 

int main(){ 
    //pointer to pointer of device used to retrieve a list of devices 
    libusb_device **devs; 
    libusb_device_handle *dev_handle; //a device handle 
    libusb_context *ctx = NULL; //A LIBUSB session 
    int r;// for return values 
    ssize_t cnt; //holding number of devices in list 
    r = libusb_init(&ctx); // initialize the library for the session we just declared 

    if(r < 0){ 
     cout <<"init error "<<r<< endl; 
     return 1; 
    } 

    libusb_set_debug(ctx, 3); // set verbosity level to 3, as suggested in the documentation 
    cnt = libusb_get_device_list(ctx, &devs); //get the list of devices 

    if (cnt < 0) { 
     cout <<"Get Device Error "<< endl; // there was an error 
     return 1; 
    } 

    cout << cnt <<" Device in list " << endl; 
    dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689); // these are vendor id and product id 

    if (dev_handle == null){ 
     cout <<"Cannot open device "<< endl; 
    }else{ 
     cout << "Device opened" << endl; 
    } 

    libusb_free_device_list(devs, 1);// free the list unref the devices in it 

    unsigned char *data = new unsigned char[4];//data to write 
    data[0] = 'a'; data[1] = 'b'; data[2] = 'c'; data[3] = 'd';//some dummy values 

    int actual; //used to find how many bytes were written 

    if (libusb_kernal_driver_active(dev_handle, 0) == 1){// findout if kernal driver attached 
     cout << "Kernal Driver Active" << endl; 
     if (libus_detach_kernal_driver(dev_handle, 0) == 0){ //detach it 
      cout<< "Kernal Driver Detached" << endl; 
     } 
    } 

    r = libusb_claim_interface(dev_handle, 0);// claim interface 0 (the first) of devices 

    if(r < 0){ 
     cout <<"Cannot claim interface "<<endl; 
     return 1; 
    } 

    cout <<"Claimed interface "<<endl; 

    cout<<"data->"<<data<<"<-"<<endl; // just to see the data we want to write : abcd 
    cout<<"Writing data..."<<endl; 

    r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);//my device's out endpoint was 2, found withe trial - the device had two endpoints: 2 and 129 

    if(r == 0 && actual == 4){ // we wrote 4 bytes successfully 
     cout<<"Writing successfull"<<endl; 
    }else{ 
     cout<<"write error"<<endl; 
    } 

    r = libusb_release_interface(dev_handle, 0); // release the claimed interface 

    if (r != 0){ 
     cout << "Cannot release interface" << endl; 
     retrun 1; 
    } 
    cout<<"Released interface"<<endl; 
    libusb_close(dev_handle); // close the device we opened 
    libusb_exit(ctx); // need to be called to end the 

    delete[] data;// delete the allocated memory for data 
    return 0; 
    //printf("Hello libusb!"); 
    //return 0; 
} 

我嘗試以下三個命令編譯上面的代碼

g++ transfer_data_libusb.cpp $(pkg-config --libs libusb-1.0) -o transfer_data_libusb 
g++ transfer_data_libusb.cpp -o transfer_data_libusb 
g++ transfer_data_libusb.cpp 

,但我得到以下錯誤

transfer_data_libusb.cpp: In function ‘int main()’: 
transfer_data_libusb.cpp:32:20: error: ‘null’ was not declared in this scope 
transfer_data_libusb.cpp:45:47: error: ‘libusb_kernal_driver_active’ was not declared in this scope 
transfer_data_libusb.cpp:47:47: error: ‘libus_detach_kernal_driver’ was not declared in this scope 

我有理解這些錯誤困難的時候,想一些建議。如果有可能的解決方案,

感謝

回答

2

您收到錯誤,因爲該變量和函數(符號)並沒有宣佈,也沒有在自己的主程序也不在您使用的庫(頭文件)中。你可能只是拼錯了他們的名字。

  • 你應該有,而不是 「空」
  • 你應該有 'libusb_kernel_driver_active' 而不是 'libusb_kernal_driver_active' 「NULL」
  • 你應該有,而不是 'libus_detach_kernal_driver'
'libus_detach_kernel_driver'

只要解決這個問題,一切都應該沒問題。

+0

非常感謝你,我做了你的建議的變化和程序現在編譯完美。當實際供應商ID和設備ID替換代碼中的虛擬版本時,我似乎遇到了新問題。它不斷給另一個錯誤是:(transfer_data_libusb.cpp:30:55:錯誤:在八進制常量無效的數字「9」),你可以幫我這個問題,我使用者均基於USB設備一個16GB的金士頓設備 – Amjad 2013-02-25 17:57:33

+0

你可能有一些問題存在: dev_handle = libusb_open_device_with_vid_pid(CTX,0951,1689); – 2013-02-25 18:02:45

+1

@UsmanSharifAmjadKhan當你在它被解釋爲八進制數值常數前加上一個0。 9不是八進制數字。 – daramarak 2013-02-25 18:04:21