Libusb - ubuntu - Psoc5. libusb_open_device_with_vid_pid return 0

1.8k views Asked by At

I am working on a power engineering project on 4th semester, and programming isn't my strong side. I've been working on using libusb for communication between a PSoC 5 and a Linux terminal program written in C++. The terminal code is:

The problem is that libusb_open_device_with_vid_pid(NULL, 0x1111, 0x2222) returns 0 every time, even though the device is recognized by the Linux OS. OS is Ubuntu if that is relevant.

#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();
                }
            }
    }
1

There are 1 answers

0
Armali On

libusb_open_device_with_vid_pid combines finding and opening and doesn't return an error code. If you are sure the device is there, have you checked you have the rights to read/write to it ? You can also increase the verbosity of error messages. – Leiaz

Thanks! that did it!. I forgot to use sudo.. rookie mistake – Çağrı Esen