[libhid-discuss] python works but not C
Will Woods
will.woods at ynic.york.ac.uk
Wed Feb 18 19:51:26 UTC 2009
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;
}
More information about the libhid-discuss
mailing list