简体   繁体   English

在C / C ++ / Java中查找USB设备的信息

[英]Find the Information of USB Devices In C/C++/Java

I need to list existing USB hub and the devices connected in the hub using my C++ program. 我需要列出使用我的C ++程序的现有USB集线器和在集线器中连接的设备。

I can able to print the USB Hub and devices connected in hub from terminal using the commands 我可以使用以下命令从终端打印USB集线器和在集线器中连接的设备

lsusb lsusb -v lsusb lsusb -v

I want to use that feature in my C++ program. 我想在我的C ++程序中使用该功能。

How I can do this programmatically. 我如何以编程方式做到这一点。 Is there any C++ classes available to use in my Qt application or in c or java.this is one help taken from https://unix.stackexchange.com/questions/61484/find-the-information-of-usb-devices-in-c 我的Qt应用程序或c或java是否有可用的C ++类。这是从https://unix.stackexchange.com/questions/61484/find-the-information-of-usb-devices-在-C

#include <stdio.h>
#include <usb.h>
main(){
struct usb_bus *bus;
struct usb_device *dev;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next)
    for (dev = bus->devices; dev; dev = dev->next){
        printf("Trying device %s/%s\n", bus->dirname, dev->filename);
        printf("\tID_VENDOR = 0x%04x\n", dev->descriptor.idVendor);
        printf("\tID_PRODUCT = 0x%04x\n", dev->descriptor.idProduct);
    }

} does anyone know how to compile this??? }有人知道如何编译吗??? im facing problem 我面临的问题

You need to install libusb-dev and libusb 您需要安装libusb-dev和libusb

sudo apt-get update
sudo apt-get install libusb
sudo apt-get install libusb-dev

After that compile your code with -lusb 之后,用-lusb编译代码

gcc usbtest.c -o usbtest -lusb

Check this: How to execute a command and get output of command within C++ using POSIX? 请检查以下内容: 如何使用POSIX在C ++中执行命令并获取命令输出?

You could use system() if you do not need the stdout / stderr . 如果不需要stdout / stderr则可以使用system()

You could use popen() if you want to capture the stdout . 如果要捕获stdout可以使用popen()

You could use execlp() to run the command you desire. 您可以使用execlp()运行所需的命令。 The function call for your question would be something like execlp("ls","Isusb","Isusb","-v") 您问题的函数调用将类似于execlp("ls","Isusb","Isusb","-v")
Read this for more information: http://linux.about.com/library/cmd/blcmdl3_execlp.htm 阅读此以获得更多信息: http : //linux.about.com/library/cmd/blcmdl3_execlp.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM