linux usb-hid :将 libhid 库添加到 eclipse(C++) 或 netbeans IDE 或本机 input.h 或 hiddev.h?

发布于 2024-08-02 05:57:56 字数 2527 浏览 1 评论 0原文

我对 libhid 有问题。

我发现有 2 种方式 4 访问 linux 中的 usb-hid

1)linux 默认库,如 input.hhiddev.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("!!!Hello World!!!"); /* prints !!!Hello 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;
}

i have problem with libhid .

i found that there 2 way 4 accessing the usb-hid in linux

1)linux default libraries like input.h and hiddev.h and ...

2)using libhid

i found libhid some confusing and try to use input.h but i have problem with that 2.

i dont know how to get information about my device from ubuntu

i use open() to open the device

str="/dev/inpt/eventX" \\where X=0,1,...,7(I'm not sure about this)
open(str,O_RDWR)

then get info with ioctl

ioctl(fd,EVIOCGVERSION,&version);

but it give me wrong vendor and product IDs

then
i try to use libhid but had know idea how to use libhid (or any other library) in eclipse or netbeans

can you tell me how you compiled your codes any IDE like eclipse or netbeans or just using terminal and gcc?
or
how to work with ioctl() and open() ?

my whole example code:

#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("!!!Hello World!!!"); /* prints !!!Hello 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2024-08-09 05:57:56

中编译代码的方法,

我找到了一种在 eclipse 1

解决了在终端中使用 GCC 编译代码的问题,您应该通过在命令中添加“-lhid”来为 GCC 定义 libhid :
gcc test_libhid.c -lhid

如果您使用 eclipse 并且想要使用 libhid,则应将“-lhid”添加到 gcc 链接器,以便 gcc 在编译代码时可以使用 libhid
按照步骤操作:

1) 在项目资源管理器面板上,右键单击项目选择属性(最后一个选项)
或者选择您的项目并按 Alt+Enter

2) 在左侧面板中展开“c/c++ build”并选择“设置”

3) 在右侧选择“工具”设置”选项卡

4)您应该在其中看到 GCC C 编译器、GCC C 链接器和 GCC 汇编器。
展开GCC C链接器并选择Libraries

5)在右侧选择后,您应该看到2个框:Libraries(-l)和Library search path(-L)
在 Libraries(-l) 中添加“hid”

注意:执行此步骤时 eclipse 使用 GCC 编译代码 eclipse 添加“-lhid”参数gcc 使其能够识别 libhid。

I find a way to compile my code in eclipse

1 problem solved

to compile your code with GCC in terminal you should define libhid for GCC by adding "-lhid" to your command :
gcc test_libhid.c -lhid

if your using eclipse and you want to use libhid with it you should add "-lhid" to gcc linker in order to gcc could use libhid when its compiling your code
follow the steps:

1)on the project Explorer panel , R-click on your project select properties (last option)
or select your project and press Alt+Enter

2)in the left panel expand "c/c++ build" and select "setting"

3)in the right side select "tool setting" tab

4)you should see GCC C compiler and GCC C linker and GCC assembler in there .
expand GCC C linker and select Libraries

5)after selecting in the right side you should see 2 boxes: Libraries(-l) and Library search path(-L)
in the Libraries(-l) add "hid"

note:eclipse use GCC to compile your codes when you do this steps eclipse add "-lhid" parameter to gcc to able it recognizing the libhid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文