我对libhid有意见。
我发现在linux中有2种方式4访问usb-hid。
1)linux默认库,如input.h和hiddev.h和.
2)使用libhid
我发现libhid有些困惑,并试图使用input.h,但我对这个2有问题。
我不知道如何从ubuntu获得有关我的设备的信息。
我使用open()打开设备
str="/dev/inpt/eventX" \\where X=0,1,...,7(I'm not sure about this)
open(str,O_RDWR)然后用ioctl获取信息
ioctl(fd,EVIOCGVERSION,&version);但是它给了我错误的供应商和产品ID。
然后我尝试使用libhid,但我知道如何在eclipse或netbeans中使用libhid (或任何其他库)。
你能告诉我你是如何编译你的代码的吗?像eclipse或netbeans这样的IDE,或者仅仅使用终端和gcc?或者如何使用ioctl()和open()?
我的整个示例代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ftw.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <asm/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
#include <linux/input.h>
#include <strings.h>
struct input_devinfo
{
uint16_t bustype;
uint16_t vendor;
uint16_t product;
uint16_t version;
};
int main(void) {
//puts("!World!"); /* prints !World! */
//usb_device ud;
//int i=0;
//string str;
//str=char[100];
//str="/dev/input/event0\n";
printf("------------- start -----------------\n");
char str[]="" ;
int version=0;
char c[16];
char t;
int i,fd;
//for (i=0 ; i<8 ; i++)
{
//strcpy(c,str);
//t=i-'0';
//printf("salam5\n");
//c[15]=t;
//openning
//open(str,O_RDONLY);//read and write
if ((fd = open(str,O_RDWR)) < 0)
perror("str open\n");
else
printf("%s opened successfully\n",str);
ioctl(fd,EVIOCGVERSION,&version);
printf("version = %d \n",version);
printf("evdev driver version is %d.%d.%d\n",version >> 16, (version >> 8) & 0xff, version & 0xff);
//geting info from device
struct input_devinfo device_info;
ioctl(fd,EVIOCGID,&device_info);
printf("vendor 0x%04hx product 0x%04hx version 0x%04hx is on ?",
device_info.vendor, device_info.product,
device_info.version);
}
return EXIT_SUCCESS;
}发布于 2009-08-29 06:02:13
我找到了一种在eclipse中编译代码的方法。
一个问题解决了
若要在终端中使用GCC编译代码,应在命令中添加" -lhid“,以便为GCC定义libhid : test_libhid.c -lhid。
如果您使用eclipse,并且希望与它一起使用libhid,您应该在gcc链接器中添加"-lhid“,这样gcc在编译代码时可以使用libhid,具体步骤如下:
(1)在项目资源管理器面板上,R-单击项目选择属性(最后选项)或选择项目并按Alt+Enter
(2)在左侧面板中展开"c/c++ build“并选择”设置“
3)在右侧选择“工具设置”选项卡
4)你应该会看到GCC C编译器,GCC C链接器和AC.26汇编程序。扩展GCC C链接器并选择库
(5)在右侧选择后,您应该会看到两个框:库(-l)和图书馆搜索路径(-l),在库(-l)中添加"hid"
note:eclipse使用GCC编译您的代码,当您执行此步骤,eclipse添加了"-lhid“参数的gcc,以使它能够识别libhid。
https://stackoverflow.com/questions/1320618
复制相似问题