Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想通过 Python 脚本发现我的 Windows PC 上的所有串行设备。我怎样才能正确地做到这一点?
丑陋的方法是尝试打开 COM1、COM2、...、COM9999(限制是什么?),当引发异常时,得出没有设备的结论。
我找到了一个使用 pyserial 模块的答案,尽管它没有记录:
import serial.tools.list_ports_windows devices = serial.tools.list_ports_windows.comports()
在 Linux 上(虽然在原始问题中没有解决):
import serial.tools.list_ports_posix devices = serial.tools.list_ports_posix.comports()