Libusb - ubuntu - Psoc5. libusb_open_device_with_vid_pid ret

Libusb - ubuntu - Psoc5. libusb_open_device_with_vid_pid return 0

本文关键字:with vid pid ret device open ubuntu Psoc5 libusb Libusb      更新时间:2023-10-16

我在第四学期从事一个电力工程项目,编程不是我的强项。我一直在使用libusb进行PSoC 5和用C++编写的Linux终端程序之间的通信。终端代码为:

问题是libusb_open_device_with_vid_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_open_device_with_vid_pid结合了查找和打开,并且不返回错误代码。如果您确定设备在那里,您是否检查过您是否有权读取/写入它?您还可以增加错误消息的详细程度。 - 莱亚兹

谢谢!做到了!.我忘了使用sudo..菜鸟错误 – Çağrı Esen