【问题标题】:octal constant error (libusb)八进制常数错误 (libusb)
【发布时间】:2013-02-26 11:20:19
【问题描述】:

我正在运行 Ubuntu 12.04 编译具有以下代码的 c++ 文件

#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;
}

但是当我使用以下命令行编译上述代码时(这些是我的 USB 的产品 ID 和供应商 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

有人建议我删除前导零,即(“951”而不是“0951”) 但是当我这样做时,文件会成功编译,但是当我运行编译后的版本时,会引发以下错误

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

不知道该怎么办,请你帮忙顺便说一句,我使用以下命令编译上面的代码

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

非常感谢您的宝贵时间

【问题讨论】:

  • 您似乎正在尝试访问未初始化的内存位置。也许某个地方的空引用?你调试过你的代码吗?因为你在用户空间工作,你可以调试它。

标签: c++ ubuntu usb libusb


【解决方案1】:

您的号码可能是十六进制的。使用:

0x951

而不是你的0951

还有:

0x1689

而不是你的1689

如果您看到其他数字(我假设它是您的产品):

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

它们有af 字符,所以这意味着格式应该是十六进制。

【讨论】:

  • 对于 1689 应该是 0x1689
  • 感谢编译代码的人,但它现在抛出了一些其他错误(libusb 无法打开打开的 USB 设备权限被拒绝)你能帮我深入了解吗?我是新手在 c c++ 和 libusb 中
  • 当我编译代码 7 列表 libusb 中的设备时,终端上出现以下错误:错误 [op_open] libusb 无法打开 USB 设备 /dev/bus/usb/002/003:权限被拒绝。 libusb:error [op_open] libusb 需要对 USB 设备节点的写入权限。无法打开设备分段错误(核心转储)
【解决方案2】:

当你的电话失败时

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 的操作。

【讨论】:

  • 谢谢哥们,但是当我更改上面的代码(0x951 和 0x1689)时,设备就在那里我得到了另一个错误,它是权限被拒绝准确的错误它是(7 设备在列表 libusb:错误[op_open] libusb 无法打开 USB 设备 /dev/bus/usb/002/003:权限被拒绝。libusb:错误 [op_open] libusb 需要对 USB 设备节点的写访问。无法打开设备分段错误(核心转储))
  • 你是否以 root 身份使用 libusb?
  • 对上面的 cmets 错误感到抱歉,是的,我以 root 用户身份登录,它可以工作,但我无法物理访问我的 USB 设备,但是当我输入 lsusb 时,我可以看到它在那里?
  • 尝试使用libusb_context - 对象,即libusb_open_device_with_vid_pid ( NULL, 0x951, 0x1689 )
  • 不,它不起作用我试过但同样的错误(分段错误(核心转储))...当我以 root 用户身份运行时,只要我执行 (./transfer_data_libusb) 命令 USB消失了...可以再看到这个...即使消息写入成功...我想查看写入的数据(abcd)但看不到...???
猜你喜欢
  • 1970-01-01
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
  • 2012-06-25
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多