如何在不打开 Linux 的情况下找到所有串行设备(ttyS、ttyUSB、..)? [英] How to find all serial devices (ttyS, ttyUSB, ..) on Linux without opening them?

查看:33
本文介绍了如何在不打开 Linux 的情况下找到所有串行设备(ttyS、ttyUSB、..)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取 Linux 系统上所有可用串行端口/设备列表的正确方法是什么?

What is the proper way to get a list of all available serial ports/devices on a Linux system?

也就是说,当我遍历/dev/中的所有设备时,我如何以经典的方式判断哪些是串口,即那些通常支持波特率和RTS/CTS 流量控制?

In other words, when I iterate over all devices in /dev/, how do I tell which ones are serial ports in the classic way, that is, those usually supporting baud rates and RTS/CTS flow control?

解决方案将用 C 编码.

The solution would be coded in C.

我问是因为我使用的第三方库明显错误:它似乎只迭代 /dev/ttyS*.问题在于,例如,USB 上的串行端口(由 USB-RS232 适配器提供),这些端口列在/dev/ttyUSB* 下.阅读 Serial-HOWTO at Linux.org,我得到了随着时间的推移,还会有其他名称空间的想法.

I ask because I am using a third-party library that does this clearly wrong: It appears to only iterate over /dev/ttyS*. The problem is that there are, for instance, serial ports over USB (provided by USB-RS232 adapters), and those are listed under /dev/ttyUSB*. And reading the Serial-HOWTO at Linux.org, I get the idea that there'll be other name spaces as well, as time comes.

所以我需要找到检测串口设备的官方方法.问题是似乎没有记录,或者我找不到.

So I need to find the official way to detect serial devices. The problem is that none appears to be documented, or I can't find it.

我想一种方法是从 /dev/tty* 打开所有文件并在它们上调用一个特定的 ioctl() ,该文件仅在串行设备上可用.不过,这会是一个好的解决方案吗?

I imagine one way would be to open all files from /dev/tty* and call a specific ioctl() on them that is only available on serial devices. Would that be a good solution, though?

hrickards 建议查看setserial"的来源.它的代码完全符合我的想法:

hrickards suggested to look at the source for "setserial". Its code does exactly what I had in mind:

首先,它打开一个设备:

First, it opens a device with:

fd = open (path, O_RDWR | O_NONBLOCK)

然后它调用:

ioctl (fd, TIOCGSERIAL, &serinfo)

如果该调用没有返回错误,那么它显然是一个串行设备.

If that call returns no error, then it's a serial device, apparently.

我在 Serial Programming/termios 中发现了类似的代码em>,建议同时添加 O_NOCTTY 选项.

不过,这种方法存在一个问题:

当我在 BSD Unix(即 Mac OS X)上测试这段代码时,它运行良好.但是,通过蓝牙提供的串行设备会导致系统(驱动程序)尝试连接到蓝牙设备,这需要一段时间才会超时返回错误.这是由于刚刚打开设备造成的.我可以想象类似的事情也会发生在 Linux 上——理想情况下,我不需要打开设备来确定它的类型.我想知道是否还有一种方法可以在不打开的情况下调用 ioctl 函数,或者以一种不会导致建立连接的方式打开设备?

When I tested this code on BSD Unix (that is, Mac OS X), it worked as well. However, serial devices that are provided through Bluetooth cause the system (driver) to try to connect to the Bluetooth device, which takes a while before it'll return with a timeout error. This is caused by just opening the device. And I can imagine that similar things can happen on Linux as well - ideally, I should not need to open the device to figure out its type. I wonder if there's also a way to invoke ioctl functions without an open, or open a device in a way that it does not cause connections to be made?

我该怎么办?

推荐答案

/sys 文件系统应该包含大量信息用于您的任务.我的系统(2.6.32-40-generic #87-Ubuntu)建议:

The /sys filesystem should contain plenty information for your quest. My system (2.6.32-40-generic #87-Ubuntu) suggests:

/sys/class/tty

它为您提供系统已知的所有 TTY 设备的描述.一个精简的例子:

Which gives you descriptions of all TTY devices known to the system. A trimmed down example:

# ll /sys/class/tty/ttyUSB*
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 /sys/class/tty/ttyUSB0 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.0/ttyUSB0/tty/ttyUSB0/
lrwxrwxrwx 1 root root 0 2012-03-28 20:44 /sys/class/tty/ttyUSB1 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/ttyUSB1/tty/ttyUSB1/

点击以下链接之一:

# ll /sys/class/tty/ttyUSB0/
insgesamt 0
drwxr-xr-x 3 root root    0 2012-03-28 20:43 ./
drwxr-xr-x 3 root root    0 2012-03-28 20:43 ../
-r--r--r-- 1 root root 4096 2012-03-28 20:49 dev
lrwxrwxrwx 1 root root    0 2012-03-28 20:43 device -> ../../../ttyUSB0/
drwxr-xr-x 2 root root    0 2012-03-28 20:49 power/
lrwxrwxrwx 1 root root    0 2012-03-28 20:43 subsystem -> ../../../../../../../../../../class/tty/
-rw-r--r-- 1 root root 4096 2012-03-28 20:43 uevent

这里的 dev 文件包含以下信息:

Here the dev file contains this information:

# cat /sys/class/tty/ttyUSB0/dev
188:0

这是主要/次要节点.这些可以在 /dev 目录中搜索以获得用户友好的名称:

This is the major/minor node. These can be searched in the /dev directory to get user-friendly names:

# ll -R /dev |grep "188, *0"
crw-rw----   1 root dialout 188,   0 2012-03-28 20:44 ttyUSB0

/sys/class/tty 目录包含所有 TTY 设备,但您可能希望排除那些讨厌的虚拟终端和伪终端.我建议你只检查那些有 device/driver 条目的:

The /sys/class/tty dir contains all TTY devices but you might want to exclude those pesky virtual terminals and pseudo terminals. I suggest you examine only those which have a device/driver entry:

# ll /sys/class/tty/*/device/driver
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS0/device/driver -> ../../../bus/pnp/drivers/serial/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS1/device/driver -> ../../../bus/pnp/drivers/serial/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS2/device/driver -> ../../../bus/platform/drivers/serial8250/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS3/device/driver -> ../../../bus/platform/drivers/serial8250/
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 /sys/class/tty/ttyUSB0/device/driver -> ../../../../../../../../bus/usb-serial/drivers/ftdi_sio/
lrwxrwxrwx 1 root root 0 2012-03-28 21:15 /sys/class/tty/ttyUSB1/device/driver -> ../../../../../../../../bus/usb-serial/drivers/ftdi_sio/

这篇关于如何在不打开 Linux 的情况下找到所有串行设备(ttyS、ttyUSB、..)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆