2014-12-16 15 views
0

저는 4 학기에 전력 공학 프로젝트를 진행하고 있습니다. 프로그래밍은 제 강점이 아닙니다. 나는 PSoC 5과 C++로 작성된 리눅스 터미널 프로그램 간의 통신을 위해 libusb을 사용하고있다. 터미널 코드는 다음과 같습니다.Libusb - 우분투 - Psoc5. libusb_open_device_with_vid_pid return 0

libusb_open_device_with_vid_pid(NULL, 0x1111, 0x2222)은 장치가 Linux OS에서 인식 되더라도 항상 0을 반환합니다. 운영 체제는 관련성이있는 경우 Ubuntu입니다.

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

'libusb_open_device_with_vid_pid'는 찾기와 열기를 결합하고 오류 코드를 반환하지 않습니다. 장치가 있다고 확신하는 경우, 장치에 대한 읽기/쓰기 권한이 있는지 확인 했습니까? 또한 [오류 메시지의 자세한 표시를 높일 수 있습니다] (http://libusb.sourceforge.net/api-1.0/group__lib.html#ga5f8376b7a863a5a8d5b8824feb8a427a). – Leiaz

+0

감사합니다. 그게 그랬어!. sudo를 사용하는 것을 잊었습니다. 신인의 실수 –

답변

0

libusb_open_device_with_vid_pid는 찾아 열고 '은 t 오류 코드를 반환 아무튼 결합합니다. 장치가 있다고 확신하는 경우, 장치에 대한 읽기/쓰기 권한이 있는지 확인 했습니까? increase the verbosity of error messages도 가능합니다. –   leiaz

고마워요! 그게 그랬어!. sudo를 사용하는 것을 잊어 버렸습니다 .. 신참의 실수 - Çağrı Esen