2013-02-26 18 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 //simon's usb(duracell)1516:1213 
    dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689); //these are vendorID and productID I found for my usb device 

    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_kernel_driver_active(dev_handle, 0) == 1){// findout if kernal driver attached 
     cout << "Kernal Driver Active" << endl; 
     if (libusb_detach_kernel_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; 
     return 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; 
} 

但是當我使用下面的命令行編譯上面的代碼(這些是產品ID和廠商我的USB的ID)

dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689); 

編譯器會引發以下錯誤

transfer_data_libusb.cpp:30:55: error: invalid digit "9" in octal constant 

Someo NE曾經勸我刪除前導零,即(「951」,而不是「0951」) 但是當我做到這一點,該文件被成功編譯,但是當我RNU編譯版本,這將引發以下錯誤

7 Device in list 
Cannot open device 
Segmentation fault (core dumped) 

不知道該怎麼辦,你能幫幫我BTW使用下面的命令來編譯上面的代碼

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

非常感謝你爲你的時間

+0

似乎你正試圖訪問一個未初始化的內存位置。也許一個空引用的地方?你有沒有調試過你的代碼?因爲你在用戶空間工作,你可以調試它。 – 2013-02-26 11:23:10

回答

3

您的號碼可能是十六進制的。使用:

0x951 

而不是您的0951

和:

0x1689 

,而不是你1689

如果您看到其他數字(我想這是你的產品):

http://usbspeed.nirsoft.net/?g=32gb

他們af字符,所以這意味着格式應爲十六進制。

+0

相同的1689它應該是0x1689 – ismail 2013-02-26 11:23:53

+0

@cartman謝謝,添加到我的答案。 – ouah 2013-02-26 11:26:32

+0

感謝工作人員編譯的代碼,但它現在拋出一些其他錯誤(libusb無法打開打開的USB設備權限被拒絕)你能幫我到這個底部請我是新手在c C++和libusb – Amjad 2013-02-26 11:48:43

0

當你調用失敗

dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689); 

dev_handle將是NULL。

if (dev_handle == NULL){ 
    cout <<"Cannot open device "<< endl; 
    //treat error here and quit 

然後您需要停止運行dev_handle

+0

感謝哥們但該設備是有當我改變了上面的代碼(0x951和0x1689),我得到了這是許可被拒絕準確地說它是 錯誤其他錯誤(7列表中的設備 libusb:錯誤[op_open] libusb無法打開USB設備/ dev/bus/usb/002/003:權限被拒絕 libusb:錯誤[op_open] libusb需要寫入USB設備節點 無法打開設備 分段錯誤(核心轉儲)) – Amjad 2013-02-26 12:09:34

+0

你是否以root身份使用libusb? – 2013-02-26 12:22:50

+0

不,但當我登錄 – Amjad 2013-02-26 12:33:37