Discussion:
[libhid-discuss] python works but not C
Will Woods
2009-02-18 19:51:26 UTC
Permalink
I am successfully talking to my device via the python bindings, but my C
version fails with error 21 "could not claim interface 0: Device or
resource busy" after successfully opening the device. The two pieces of
code look the same to me, but I must be missing something?

Any advice would be much appreciated...


Python code:

import sys
from struct import unpack
from hid import *


ret = hid_init()
if ret != HID_RET_SUCCESS:
sys.stderr.write("hid_init failed with return code %d.n" % ret)

hid = hid_new_HIDInterface()
if hid == 0:
sys.stderr.write("hid_new_HIDInterface failed.")

matcher = HIDInterfaceMatcher()
matcher.vendor_id = 0x0488
matcher.product_id = 0x0020

ret = hid_force_open(hid, 0, matcher, 3)
if ret != HID_RET_SUCCESS:
sys.stderr.write("hid_force_open failed with return code %d.n" % ret)

while True:
ret, bytes = hid_interrupt_read(hid, 0x81, 8, 100)
if ret == 0:
a = unpack('Q',bytes)[0]
print a






and the C version:


#include <stdio.h>
#include <hid.h>

#define VENDOR_ID 0x0488
#define PRODUCT_ID 0x0020


int main(int argc, char *argv[])
{
unsigned char bytes[8];

HIDInterface* hid;
int iface_num = 0;
hid_return ret;

HIDInterfaceMatcher matcher = { VENDOR_ID, PRODUCT_ID, NULL, NULL, 0 };

// see include/debug.h for possible values
hid_set_debug(HID_DEBUG_ALL);
hid_set_debug_stream(stderr);
// passed directly to libusb
hid_set_usb_debug(0);

ret = hid_init();
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_init failed with return code %d\n", ret);
return 1;
}

hid = hid_new_HIDInterface();
if (hid == 0) {
fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
return 1;
}

ret = hid_force_open(hid, iface_num, &matcher, 3);
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
return 1;
}

while( 1)
{
ret = hid_interrupt_read( hid, 0x81, bytes, 8, 100);

printf( "ret=%d\n", ret);
if( ret == HID_RET_SUCCESS )
{
// do something
}

}

ret = hid_close(hid);
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_close failed with return code %d\n", ret);
return 1;
}

hid_delete_HIDInterface(&hid);

ret = hid_cleanup();
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
return 1;
}

return 0;
}
Charles Lepple
2009-02-19 03:44:14 UTC
Permalink
Post by Will Woods
I am successfully talking to my device via the python bindings, but my C
version fails with error 21 "could not claim interface 0: Device or
resource busy" after successfully opening the device. The two
pieces of
code look the same to me, but I must be missing something?
Not sure - they do look equivalent to me.

What version of libhid are you using (and what distribution did it
come from, if any)?

You might be able to find something out via "ltrace" - it intercepts
calls to shared libraries, but I don't have any specific tips on how
you might go about that. (If you do, please compress any logs before
sending them to the list.)
Will Woods
2009-02-19 08:52:14 UTC
Permalink
I was using 0.2.15 versions of libhid and python bindings that come from
ubuntu 8.02 (Hardy). I downloaded the latest version 0.2.16 and it works
fine...

Will
Post by Will Woods
I am successfully talking to my device via the python bindings, but my C
version fails with error 21 "could not claim interface 0: Device or
resource busy" after successfully opening the device. The two pieces of
code look the same to me, but I must be missing something?
Any advice would be much appreciated...
import sys
from struct import unpack
from hid import *
ret = hid_init()
sys.stderr.write("hid_init failed with return code %d.n" % ret)
hid = hid_new_HIDInterface()
sys.stderr.write("hid_new_HIDInterface failed.")
matcher = HIDInterfaceMatcher()
matcher.vendor_id = 0x0488
matcher.product_id = 0x0020
ret = hid_force_open(hid, 0, matcher, 3)
sys.stderr.write("hid_force_open failed with return code %d.n" % ret)
ret, bytes = hid_interrupt_read(hid, 0x81, 8, 100)
a = unpack('Q',bytes)[0]
print a
#include <stdio.h>
#include <hid.h>
#define VENDOR_ID 0x0488
#define PRODUCT_ID 0x0020
int main(int argc, char *argv[])
{
unsigned char bytes[8];
HIDInterface* hid;
int iface_num = 0;
hid_return ret;
HIDInterfaceMatcher matcher = { VENDOR_ID, PRODUCT_ID, NULL, NULL, 0 };
// see include/debug.h for possible values
hid_set_debug(HID_DEBUG_ALL);
hid_set_debug_stream(stderr);
// passed directly to libusb
hid_set_usb_debug(0);
ret = hid_init();
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_init failed with return code %d\n", ret);
return 1;
}
hid = hid_new_HIDInterface();
if (hid == 0) {
fprintf(stderr, "hid_new_HIDInterface() failed, out of
memory?\n");
return 1;
}
ret = hid_force_open(hid, iface_num, &matcher, 3);
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
return 1;
}
while( 1)
{
ret = hid_interrupt_read( hid, 0x81, bytes, 8, 100);
printf( "ret=%d\n", ret);
if( ret == HID_RET_SUCCESS )
{
// do something
}
}
ret = hid_close(hid);
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_close failed with return code %d\n", ret);
return 1;
}
hid_delete_HIDInterface(&hid);
ret = hid_cleanup();
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
return 1;
}
return 0;
}
_______________________________________________
libhid-discuss mailing list
libhid-discuss at lists.alioth.debian.org
http://lists.alioth.debian.org/mailman/listinfo/libhid-discuss
http://libhid.alioth.debian.org/
Loading...