MacPython: programmatically finding all serial ports

喜夏-厌秋 提交于 2020-01-04 04:03:44

问题


I am looking for a solution to programmatically return all available serial ports with python.

At the moment I am entering ls /dev/tty.* or ls /dev/cu.* into the terminal to list ports and hardcoding them into the pyserial class.


回答1:


You could do something like this:

import glob
def scan():
    return glob.glob('/dev/tty*') + glob.glob('/dev/cu*')

for port in scan():
   # do something to check this port is open.

Then, take a look at pyserial for some good utility functions to check if a port is open and so forth.




回答2:


What about just doing the os.listdir / glob equivalent of ls to perform the equivalent of that ls? Of course it's not going to be the case that some usable device is connected to each such special file (but, that holds for ls as well;-), but for "finding all serial ports", as you ask in your Q's title, I'm not sure how else you might proceed.



来源:https://stackoverflow.com/questions/1659283/macpython-programmatically-finding-all-serial-ports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!