Linux和Python的:自动检测Arduino的串口 [英] Linux and Python: auto-detect Arduino serial port
问题描述
我有一个问题,自动将检测在Python我的Arduino的串行端口,使用Mac / Linux操作系统。
I have a problem automagically detecting my Arduino's serial port in Python, using Mac/Linux.
我知道一个工作shell命令来查找端口;因为Arduino的串口几乎总是与 tty.usbmodem
开始,你可以找到 ls / dev命令串口| grep的tty.usbmodem
这应该返回类似 tty.usbmodem262141
。
I know a working shell command to find the port; because Arduino serial ports almost always begin with tty.usbmodem
, you can find the serial port with ls /dev | grep tty.usbmodem
which should return something like tty.usbmodem262141
.
不过,我如何从我的Python code调用这个shell命令困惑。我试过这样:
However, I'm confused on how to call this shell command from my Python code. I've tried this:
p = "/dev/" + str(subprocess.Popen('ls /dev | grep tty.usbmodem', shell=True).stdout)
这将使 P
成为 /dev/tty.usbmodem262141
。
然而,此刻我得到的/ dev /无
。
如何修改我的shell脚本调用返回正确的字符串?我试着使用几个命令调用shell脚本,但没有工作过。
How can I modify my shell script call to return the right string? I've tried to use several commands to call shell scripts, but none have worked.
推荐答案
首先,如果你使用一个shell,您可以使用水珠( *
) ,让您的命令将成为 LS /dev/tty.usbmodem *
。
First of all, if you're using a shell, you can use a glob (*
), so your command would become ls /dev/tty.usbmodem*
.
接下来,你甚至不必调用shell命令在Python中使用的glob!
Next, you don't even have to call a shell command to use a glob in Python!
考虑以下code:
import glob
print(glob.glob("/dev/tty.usbmodem*"))
这篇关于Linux和Python的:自动检测Arduino的串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!