简体   繁体   English

需要在Linux上找到并打开USB串行设备

[英]Need to find and open a USB serial device on linux

I'm writing a small C application to run on my (Linux) QNAP NAS that will talk to an Arduino (I have no difficulty with any of the USB code for the arduino). 我正在编写一个小型C应用程序,以便在将与Arduino对话的(Linux)QNAP NAS上运行(我对arduino的任何USB代码都没有困难)。 (The arduino has a trusted application on it that accepts text commands via USB serial.) (arduino上有一个受信任的应用程序,可以通过USB串行接受文本命令。)

My wish was to find it using the USB vendor/product IDs (only half implemented at the moment). 我希望使用USB供应商/产品ID(目前只有一半实现)找到它。 What I have so far (see below) works quite nicely in that it does find the device. 我到目前为止(见下文)的工作原理很好,因为它确实找到了设备。

// runs on NAS
#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 %s/%s\n", bus->dirname, dev->filename);
      printf("\tVendor = 0x%04x\n", dev->descriptor.idVendor);
      printf("\tBus = 0x%03x\n", bus->location);
      printf("\tFile = %s\n", dev->filename);
      if (dev->descriptor.idVendor==0x403) {
        printf("\t  HEY, THIS IS MINE!\n");
        usb_dev_handle *handle = usb_open(dev);
        printf("\t   HANDLE 0x%08x\n", (int) handle);
        //printf(handle, "1,5,62,75\n");
        usb_close(handle);
      }
    }
  }
}

The trouble is that now I want to send/receive a little bit of text with the device and I don't know how to do that. 麻烦的是,现在我想使用该设备发送/接收一些文本,但是我不知道该怎么做。

I have expected I should be generating a device name from something in the usb_device struct and then open it like a file (like one would do on Windows). 我期望我应该从usb_device结构中的某些内容生成设备名称,然后像打开文件一样打开它(就像在Windows上那样)。

If that's correct, I need to know the correct way to find out the device name... 如果正确,我需要知道找出设备名称的正确方法...

I believe I'm using libusb. 我相信我正在使用libusb。

I happen to know that -- as currently configured/connected -- it's ttyUSB0 but I'd like to know that using code. 我碰巧知道-如当前配置/连接的-它是ttyUSB0,但我想使用代码知道这一点。

thank you! 谢谢!

I suggest using libusbp . 我建议使用libusbp It is a C library with a C++ wrapper, and there is example code showing how to get the name of a serial port based on the vendor ID and product ID of the USB device: 这是一个带有C ++包装程序的C库,下面的示例代码显示了如何根据USB设备的供应商ID和产品ID获取串行端口的名称:

https://github.com/pololu/libusbp/blob/master/examples/port_name/port_name.cpp https://github.com/pololu/libusbp/blob/master/examples/port_name/port_name.cpp

libusb is a library used for low level communication with USB devices. libusb是用于与USB设备进行低级通信的库。 But when your communication is limited just to reading device's USB descriptor - libusb is not the best tool. 但是,当您的通信仅限于读取设备的USB描述符时, libusb并不是最好的工具。

Linux systems use udev subsystem to manage hot-plug devices. Linux系统使用udev子系统来管理热插拔设备。 udev reads descriptors of all plugged USB devices and stores them in its database. udev读取所有插入的USB设备的描述符,并将其存储在其数据库中。 libudev is the library that you should use to get such info like device names of enumerated devices. libudev是用于获取诸如枚举设备的设备名称之类的信息的库。 In your case you need to remember that usb_device and usb_interface are separate things - only the latter would have the tty device file assigned to it. 在您的情况下,您需要记住usb_deviceusb_interface是分开的东西-只有后者会分配tty设备文件。

Alternatively you could just use udev config to assign a constant device name to your specific device. 或者,您可以只使用udev config为您的特定设备分配一个恒定的设备名称。 So you would not have to be looking for it. 因此,您不必寻找它。

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

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