使用libusb,具体如下:
1 打开设备:
2 关闭设备
其它就简单了
写入用usb_interrupt_write
读取用usb_interrupt_read
1 打开设备:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
usb_init();
nret=usb_find_busses();
if
(nret<0)
{
sprintf
(g_buf,
"usb_find_busses nret:[%d] fail:[%s]"
,nret,
strerror
(
errno
));
return
(*env)->NewStringUTF(env,g_buf);
}
nret1=nret;
nret=usb_find_devices();
if
(nret<0)
{
sprintf
(g_buf,
"nret1:[%d] usb_find_devices nret:[%d] fail:[%s]"
,nret1,nret,
strerror
(
errno
));
return
(*env)->NewStringUTF(env,g_buf);
}
#ifdef myhid_debug
sprintf
(g_buf,
"libenkb:usb_find_busses[%d] usb_find_devices[%d]\n"
,nret1,nret);
writelog(g_buf);
#endif
for
(bus = usb_busses; bus; bus = bus->next)
{
for
(dev = bus->devices; dev; dev = dev->next)
{
if
(dev->descriptor.idVendor==VENDOR_ID&& dev->descriptor.idProduct == PRODUCT_ID)
{
findflag=1;
goto
find_ok;
}
}
}
if
(findflag==0)
{
return
(*env)->NewStringUTF(env,
"hid device not find!"
);
}
find_ok:
udev=usb_open(dev);
if
(udev==NULL)
{
return
(*env)->NewStringUTF(env,
"hid open fail!"
);
}
#ifdef myhid_debug
sprintf
(g_buf,
"idVendor:0x%x idProduct:0x%0x iManufacturer:%d bNumEndpoints:0x%0x"
,dev->descriptor.idVendor,dev->descriptor.idProduct,dev->descriptor.iManufacturer,dev->config->interface->altsetting->bNumEndpoints);
writelog(g_buf);
#endif
nret=usb_detach_kernel_driver_np(udev,0);
/*
if(nret<0)
{
sprintf(g_buf,"usb_detach_kernel_driver_np nret:[%d] fail:[%s]",nret,strerror(errno));
return (*env)->NewStringUTF(env,g_buf);
}
*/
nret=usb_claim_interface(udev,0);
|
2 关闭设备
1
2
3
4
5
6
7
8
9
10
11
12
13
|
int
nret=usb_release_interface(udev,0);
if
(nret<0)
{
sprintf
(g_buf,
"usb_release_interface nret:[%d] fail:[%s]"
,nret,
strerror
(
errno
));
return
(*env)->NewStringUTF(env,g_buf);
}
nret=usb_close(udev);
if
(nret<0)
{
sprintf
(g_buf,
"usb_close ret:[%d] fail:[%s]"
,nret,
strerror
(
errno
));
return
(*env)->NewStringUTF(env,g_buf);
}
|
其它就简单了
写入用usb_interrupt_write
读取用usb_interrupt_read