首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Linux和Python:自动检测Arduino串口

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

Stack Overflow用户
提问于 2012-07-06 15:09:31
回答 4查看 9.5K关注 0票数 4

在使用Mac/Linux自动检测Python中的Arduino串口时,我遇到了问题。

我知道一个查找端口的shell命令;因为Arduino串行端口几乎总是以tty.usbmodem开头,所以您可以找到带有ls /dev | grep tty.usbmodem的串行端口,它应该返回类似于tty.usbmodem262141的内容。

但是,我对如何从Python代码中调用这个shell命令感到困惑。我试过这个:

代码语言:javascript
运行
复制
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,但都没有用。

EN

Stack Overflow用户

发布于 2012-07-25 04:14:35

我写这篇文章是为了找出arduino在OSX10.7.x上插入的是什么:享受。

代码语言:javascript
运行
复制
#!/usr/bin/env bash

# script name: findtty.sh
# author: Jerry Davis
#
# this little script determines what usb tty was just plugged in
# on osx especially, there is no utility that just displays what the usb
# ports are connected to each device.
#
# I named this script findtty.sh
# if run interactively, then it prompts you to connect the cable and either press enter or   it will timeout after 10 secs.
# if you set up an alias to have it run non-interactively, then it will just sleep for 10 secs.
# either way, this script gives you 10 seconds to plug in your device

# if run non interactively, a variable named MCPUTTY will be exported, this would be an advantage.
# it WAS an advantage to me, otherwise this would have been a 4 line script. :)
#
# to set up an alias to run non-interactively, do this:
#   osx: $ alias findtty='source findtty.sh',
#   or linux: $ alias findtty='. findtty.sh' (although source might still work)

\ls -1 /dev/tty* > before.tty.list

if [ -z "$PS1" ]; then
    read -s -n1 -t 10 -p "Connect cable, press Enter: " keypress
    echo
else
    sleep 10
fi

\ls -1 /dev/tty* > after.tty.list

ftty=$(diff before.tty.list after.tty.list 2> /dev/null | grep '>' | sed 's/> //')
echo $ftty
rm -f before.tty.list after.tty.list
export MCPUTTY=$ftty                     # this will have no effect if running interactively
票数 2
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11364879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档