C++ Libusb-ubuntu-Psoc5。libusb\u打开\u设备\u,带\u vid\u pid返回0
第四学期我在做一个电力工程项目,编程不是我的强项。我一直在使用一个用C++编写的Linux终端程序之间的通信。终端代码为:C++ Libusb-ubuntu-Psoc5。libusb\u打开\u设备\u,带\u vid\u pid返回0,c++,linux,ubuntu,libusb-1.0,psoc,C++,Linux,Ubuntu,Libusb 1.0,Psoc,第四学期我在做一个电力工程项目,编程不是我的强项。我一直在使用一个用C++编写的Linux终端程序之间的通信。终端代码为: 问题是libusb\u open\u device\u with\u vid\u pid(NULL,0x1111,0x2222)每次都返回0,即使Linux操作系统可以识别该设备。操作系统是相关的 #include <iostream> #include "libusb-1.0/libusb.h" #include "usb.h" #include <ti
问题是
libusb\u open\u device\u with\u vid\u pid(NULL,0x1111,0x2222)
每次都返回0,即使Linux操作系统可以识别该设备。操作系统是相关的
#include <iostream>
#include "libusb-1.0/libusb.h"
#include "usb.h"
#include <time.h>
using namespace std;
union USB_DATA
{
unsigned char USB_ARRAY[1200];
int DirectionOfPower;
int menu;
float Voltage;
float Current;
float Temperature;
float PowerFactor;
float DistortionPowerFactor;
float Amplitude_Of_Harmonics[1001];
float Regulate_To;
};
union USB_DATA USB_;
/*
void error(string s, int err)
{
cout << s " ERROR: " << libusb_error_name(err) << endl;
exit(err);
}
*/
int main()
{
int transfer_size;
int err;
float Reg_To;
// Device Handle
libusb_device_handle* dev;
// Initialize libusb with default context
libusb_init(NULL);
// Open Device VID = 0x1111, PID = 0x2222 with the default libusb context
dev = libusb_open_device_with_vid_pid( NULL, 0x1111, 0x2222 );
// If device is null, we didn't find it
/*
if (dev == NULL)
{
cout << "Device not found, exiting." << endl;
return -1;
}
int k = 0;
while (dev == NULL)
{
cout << "Device not found, trying again." << " " << k << endl;
//sleep(1);
k = k+1;
}
*/
// Claim interface 0 on the device. Here we te the operation system that wewan this device
libusb_claim_interface(dev, 0);
libusb_detach_kernel_driver(dev, 0);
// Set alternate setting 0 on interface 0
libusb_set_interface_alt_setting(dev, 0, 0);
while(true)
{
cout << "Welcome to Spaendingsregulering!" << endl;
cout << endl;
cout << "MENU" << endl;
cout << "Indtast nummer for navigation" << endl;
cout << "1. Indsaet driftsparametre " << endl;
cout << "2. Analyser harmoniske " << endl;
cout << "3. Fremvis data " << endl;
while(true)
{
cin >> USB_.menu;
if(cin.good())
break;
cin.clear();
}
/*
err = libusb_bulk_transfer(dev, 0x02, USB_.USB_ARRAY, sizeof(union USB_), &transfer_size, 1000);
if( err )
error( "Bulk OUT Transfer Failed!", err);
err = libusb_bulk_transfer(dev, 0x81, USB_.USB_ARRAY, sizeof(union USB_), &transfer_size, 1000);
if( err )
error( "Bulk IN Transfer Failed!", err);
*/
if(USB_.menu == 1)
while(true)
{
cout << "Indsaet oensket spaending" << endl;
cout << "Indtast 999 for at vende tilbage til hovedmenuen" << endl;
cin >> Reg_To;
cout << endl;
if(Reg_To == 999)
{
break;
}
USB_.Regulate_To = Reg_To;
cout << "=======================" << endl;
cout << "Saetter oensket spaending til:" << " " << USB_.Regulate_To << "V" << endl;
cout << "=======================" << endl;
cout << endl;
cout << "Vender tilbage til hovedmenu" << endl;
cout << "..." << endl;
cout << endl;
if(cin.good())
break;
cin.clear();
}
}
}
#包括
#包括“libusb-1.0/libusb.h”
#包括“usb.h”
#包括
使用名称空间std;
联合USB_数据
{
无符号字符USB_阵列[1200];
int定向电源;
int菜单;
浮充电压;
浮充电流;
浮子温度;
浮动功率因数;
浮动失真功率因数;
_谐波的浮动幅度_[1001];
浮动调节到;
};
联合USB_数据USB_;
/*
无效错误(字符串s,整数错误)
{
coutlibusb\u open\u device\u with\u vid\u pid
将查找和打开结合在一起,并且不返回错误代码。如果您确定设备在那里,是否已检查您是否有权读取/写入该设备?您也可以。
-莱亚兹
谢谢!成功了!我忘了使用sudo..新手错误-ğrıEsenlibusb_open_device_with_vid_pid
将查找和打开结合在一起,不返回错误代码。如果你确定设备在那里,你检查过你有读/写它的权限吗?你也可以。谢谢!成功了!我忘了使用sudo..ro奥基错误