Forum: PC-Programmierung Libusb liefert immer error code -12


von Mario X. (grinderfx)


Lesenswert?

Hallo.
Ich habe ein Gerät was per USB an mein Rechner angeschlossen ist.
Es gibt ein Programm in Java, welches die Java Libusb Version benutzt 
und auch funktioniert.
Ich benötige leider eine Version in c/c++, jedoch bekomme ich die nicht 
zum laufen.
An sich ja nicht das große Problem aber sämtliche Versionen scheitern am 
Verbinden.
Das Gerät wird erkannt nur liefert z.B. libusb_open -12 als Rückgabewert 
und libusb_open_device_with_vid_pid NULL. Ich nutze natürlich nicht 
beide gleichzeitig. Hab mehrere Versionen.
Woran kann das liegen?
Ich kann das Java Programm nicht 1:1 in c übersetzen, da die Javaversion
usb_find_busses(); usb_find_devices(); und usb_get_busses(); nutzt, was 
es bei Libusb 1.x nicht gibt.

Mein code sieht so aus:
1
libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
2
  libusb_device_handle *dev_handle; //a device handle
3
  libusb_context *ctx = NULL; //a libusb session
4
  int r; //for return values
5
  ssize_t cnt; //holding number of devices in list
6
  r = libusb_init(&ctx); //initialize the library for the session we just declared
7
  if(r < 0) {
8
    
9
    printf("Init Error %i\n", r);
10
    return 1;
11
  }
12
  //libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
13
14
  cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
15
  if(cnt < 0) {
16
    
17
    printf("Get Device Error\n");
18
    return 1;
19
  }
20
  print_devs(devs);
21
  //cout<<cnt<<" Devices in list."<<endl;
22
  printf("%i Devices in list.\n", cnt);
23
24
  dev_handle = libusb_open_device_with_vid_pid(ctx, 0x221a, 0x0100); //these are vendorID and productID I found for my usb device
25
  
26
  if(dev_handle == NULL)
27
    
28
    printf("Cannot open device\n", r);
29
  else
30
    printf("Device Opened\n", r);
31
    
32
  libusb_free_device_list(devs, 1); //free the list, unref the devices in it
33
34
  libusb_close(dev_handle); //close the device we opened
35
  libusb_exit(ctx); //needs to be called to end the

oder so:
1
struct libusb_device **devs;
2
      struct libusb_device *found = NULL;
3
      struct libusb_device *dev;
4
      struct libusb_device_handle *handle = NULL;
5
    libusb_context *ctx = NULL; //a libusb session
6
      size_t i = 0;
7
      int r;
8
9
    r = libusb_init(&ctx); //initialize the library for the session we just declared
10
    if(r < 0) {
11
    //cout<<"Init Error "<<r<<endl; //there was an error
12
    printf("Init Error %i\n", r);
13
    return 1;
14
    }
15
16
      if (libusb_get_device_list(ctx, &devs) < 0)
17
            return NULL;
18
19
    print_devs(devs);
20
21
      while ((dev = devs[i++]) != NULL) {
22
            struct libusb_device_descriptor desc;
23
            r = libusb_get_device_descriptor(dev, &desc);
24
            if (r < 0)
25
                  goto out;
26
            if (desc.idVendor == 0x221a && desc.idProduct == 0x0100) {      
27
                  found = dev;
28
                  break;
29
            }
30
      }
31
    
32
33
      if (found) {
34
            r = libusb_open(found, &handle);
35
            if (r < 0)
36
                  handle = NULL;
37
      }
38
39
    out:
40
      libusb_free_device_list(devs, 1);

von Jim M. (turboj)


Lesenswert?

Was sagt denn libusb_error_name() zu dem Fehlercode? Ohne Angabe des OS 
ist der IMO nicht eindeutig.

von Sinuswechselanwalt (Gast)


Lesenswert?

LIBUSB_ERROR_NOT_SUPPORTED? WinUSB-Treiber nicht installiert?

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.