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

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

问题描述

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

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

换句话说,当我遍历/dev/中的所有设备时,如何以经典方式分辨哪些设备是串行端口,即那些通常支持波特率和

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编码.

我问是因为我正在使用第三方库,但这样做显然是错误的:它似乎仅对/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.

我在 串行编程/术语 ,建议也添加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,通用#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文件包含以下信息:

# 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天全站免登陆