Linux和Python的:自动检测Arduino的串口

编程入门 行业动态 更新时间:2024-10-25 18:28:02
本文介绍了Linux和Python的:自动检测Arduino的串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个问题,自动将检测在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的串口

本文发布于:2023-11-05 04:11:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:串口   自动检测   Linux   Python   Arduino

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!