首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-07-06 15:16:35

首先,如果使用的是shell,则可以使用glob (*),因此命令将变为ls /dev/tty.usbmodem*

接下来,您甚至不需要调用shell命令就可以在Python中使用glob!

考虑以下代码:

代码语言:javascript
运行
复制
import glob

print(glob.glob("/dev/tty.usbmodem*"))
票数 8
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

Stack Overflow用户

发布于 2012-07-31 20:31:54

我使用这段代码自动检测linux上的串行端口。它基于我在MAVLINK项目中找到的一些代码。

代码语言:javascript
运行
复制
import fnmatch
import serial

def auto_detect_serial_unix(preferred_list=['*']):
    '''try to auto-detect serial ports on win32'''
    import glob
    glist = glob.glob('/dev/ttyUSB*') + glob.glob('/dev/ttyACM*')
    ret = []

    # try preferred ones first
    for d in glist:
        for preferred in preferred_list:
            if fnmatch.fnmatch(d, preferred):
                ret.append(d)
    if len(ret) > 0:
        return ret
    # now the rest
    for d in glist:
        ret.append(d)
    return ret

def main():
    available_ports = auto_detect_serial_unix()
    port = serial.Serial(available_ports[0], 115200,timeout=1)
    return 0

if __name__ == '__main__':
    main()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11364879

复制
相关文章

相似问题

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