如何使用 Python 识别 LINUX 上的 USB 调制解调器 /dev
如果我有一个使用 Python pyserial 模块访问的 USB 调制解调器,则需要将设备识别为“/dev/ttyACM0”。如果调制解调器连接到 USB 集线器,它不再出现在 /dev/tty 中...
如何从我的 Python 代码中以编程方式识别它,以便无论它是否已更改,或者计算机是否重新启动,我都可以找到调制解调器?
笔记: 我总是可以使用 lsusb 查看该设备,但如果它连接到 USB 集线器,它不会显示为 /dev/tty... 设备
If I have a USB modem that I am accessing using Python pyserial module, it requires the device to be identified '/dev/ttyACM0 for example. If the modem is attached to a USB hub it no longer appears in /dev/tty...
How do identify it programmatically from my Python code so regardless of whether it has been changed or not, or the machine rebooted I can locate the modem?
Note:
I can always see the device using lsusb, but if it is attached to a USB hub it does not appear as /dev/tty... device
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来像是 Linux 内核中的一个错误。如果可以,请尝试更新的版本。
如果失败,请检查 dmesg 输出的最后几行或文件
/var/log/messages
(后者取决于您的发行版;如果该文件没有不存在或不包含您要查找的内容,然后检查/var/log
中的其他文件;使用ls -rt
按时间排序会有所帮助)。识别设备后,您可能会看到一种模式。
另一种方法是主要数字和次要数字。如果运行
ls -l /dev
,您将看到如下输出:c
表示“字符设备”,4, 0
表示表示它是控制台设备单元 0。4
是标识设备类型的主编号。有关主设备号和相应设备驱动程序的列表,请参阅/proc/devices
。如果直接插入型号,请记下主编号。将其插入集线器后,尝试查找具有相同编号的设备。
This sounds like a bug in the linux kernel. If you can, try a more recent version.
If that fails, check the last few lines of the output of
dmesg
or in the file/var/log/messages
(the latter depends on your distribution; if that file doesn't exist or doesn't contain what you're looking for, then check the other files in/var/log
; sorting by time withls -rt
helps).After identifying the device, you might see a pattern.
Another approach is the major and minor number. If you run
ls -l /dev
, you'll see output like this:The
c
means "character device" and the4, 0
means it's the console device unit 0.The
4
is the major number which identifies the type of device. See/proc/devices
for a list of major numbers and the respective device drivers.If you plug in the model directly, note the major number. After plugging it into a hub, try to find devices with the same number.
不要在 Python 中做一些巫术,而是尝试编写一个 udev 规则,它为您的设备提供了一个更有用的名称,例如
/dev/my-serial-thingy
。从 Python 中使用它要容易得多。Instead of doing some voodoo in Python, try writing a udev rule which gives your device a much more useful name like
/dev/my-serial-thingy
. Using that from Python is way easier.