首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Libusb - ubuntu - Psoc5。libusb_open_device_with_vid_pid返回%0

Libusb - ubuntu - Psoc5。libusb_open_device_with_vid_pid返回%0
EN

Stack Overflow用户
提问于 2014-12-16 08:34:19
回答 1查看 1.7K关注 0票数 0

我在第四个学期做一个动力工程项目,编程不是我的强项。我一直在研究如何使用libusbPSoC 5和用C++编写的Linux终端程序之间进行通信。终端代码为:

问题是,即使设备被Linux OS识别,libusb_open_device_with_vid_pid(NULL, 0x1111, 0x2222)每次都会返回0。OS是Ubuntu,如果相关的话。

代码语言:javascript
复制
#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();
                }
            }
    }
EN

回答 1

Stack Overflow用户

发布于 2017-03-13 18:35:04

libusb_open_device_with_vid_pid结合了查找和打开,并且不返回错误代码。如果您确定设备在那里,您是否检查了您有权对其进行读/写操作?您也可以使用increase the verbosity of error messages。- Leiaz

谢谢!就是这样!我忘记用sudo了..菜鸟的错误--ğrıEsen

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27495667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档