八进制常数误差(libusb)
octal constant error (libusb)
我正在运行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
有人建议我去掉前导零ie(用"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
非常感谢您的宝贵时间
您的数字可能是十六进制的。用途:
0x951
代替你的0951
.
:
0x1689
代替你的1689
。
如果你看到其他数字(我假设它是你的产品):
http://usbspeed.nirsoft.net/?g=32gb有a
到f
个字符,所以表示格式应为十六进制。
呼叫失败
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
的操作
相关文章:
- 为什么在浮点中从大到小会引入更多的误差
- USB传输的LibUSB C++格式不同
- 基于相邻元素 c++ 的分段误差范围的循环
- QImage::p ixel 和 QImage::setPixel 坐标超出范围误差
- 将误差线添加到 VTK 二维散点图
- 动态矩阵特征分解过程中的误差
- 使用libusb.h时出现编译器错误
- 转换为非标量误差是什么意思?我该如何解决?
- 如何使用libusb获取驱动器名称?
- 特征误差:INVALID_MATRIX_PRODUCT乘向量和矩阵
- 在 c++ libusb 中将数据写入得利捷 PM9500 扫描仪的正确格式
- 使用不同参数匹配器的同一方法ON_CALL AND EXPECT_CALL会产生饱和和活动误差
- boost odeint 中的受控误差步进器是否支持复杂的数据类型?
- 此外,在计算大斐波那契数时存在精度误差
- Android USB 附件模式无法使用 libusb 与主机 PC 一起读/写
- 如何使用 libusb 为 USB 设备设置备用设置
- 将 libusb 设备存储在易于访问的容器中,无需所有权
- C++17中不定参数函数的误差
- 多态性中独立类的基本误差.C++
- 八进制常数误差(libusb)