Linux和Python:自动检测Arduino串口
发布时间:2020-12-14 01:25:46 所属栏目:Linux 来源:网络整理
导读:参见英文答案 Python to automatically select serial ports (for Arduino)????????????????????????????????????4个 我有一个问题是使用Mac / Linux在Python中自动检测我的Arduino串口. 我知道一个有效的shell命令来查找端口;因为Arduino串口几乎总是以tty.
参见英文答案 >
Python to automatically select serial ports (for Arduino)????????????????????????????????????4个
我有一个问题是使用Mac / Linux在Python中自动检测我的Arduino串口. 我知道一个有效的shell命令来查找端口;因为Arduino串口几乎总是以tty.usbmodem开头,你可以找到ls / dev的串口grep tty.usbmodem应返回类似tty.usbmodem262141的内容. 但是,我对如何从我的Python代码调用此shell命令感到困惑.我试过这个: p = "/dev/" + str(subprocess.Popen('ls /dev | grep tty.usbmodem',shell=True).stdout) 哪个应该使p成为/dev/tty.usbmodem262141. 但是,目前我得到/ dev / None. 如何修改我的shell脚本调用以返回正确的字符串?我试过使用several commands to call shell scripts,但都没有用. 解决方法
首先,如果你使用的是shell,你可以使用glob(*),这样你的命令就会变成ls /dev/tty.usbmodem*.
接下来,您甚至不必调用shell命令在Python中使用glob! 请考虑以下代码: import glob print(glob.glob("/dev/tty.usbmodem*")) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |