gpt4 book ai didi

c++ - 八进制常量错误(libusb)

转载 作者:行者123 更新时间:2023-11-28 00:48:53 25 4
gpt4 key购买 nike

我正在运行 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”)但是当我这样做时,文件被成功编译,但是当我 rnu 编译版本时,这会抛出以下错误

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

非常感谢您的宝贵时间

最佳答案

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

0x951

而不是你的 0951

和:

0x1689

而不是你的 1689

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

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

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

关于c++ - 八进制常量错误(libusb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15087753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com