Linux和Python:自动检测Arduino串口

4
我有一个问题,希望能够使用Python在Mac/Linux上自动检测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脚本调用以返回正确的字符串?我尝试过使用多个命令来调用Shell脚本,但都没有成功。

4个回答

8

首先,如果你正在使用一个shell,你可以使用通配符(*),所以你的命令将变成ls /dev/tty.usbmodem*

接下来,在Python中甚至不需要调用shell命令来使用通配符!

考虑以下代码:

import glob

print(glob.glob("/dev/tty.usbmodem*"))

哎呀,我一定做错了什么... 我尝试运行这段代码,但返回了一个空列表 [] - hao_maike
2
@mr_schlomo:你确定你有这些 tty.usbmodem 文件吗? - cha0site
哎呀——我的Arduino失去连接了!感谢您的帮助! - hao_maike
在Windows上是否也可以类似地找到Arduino呢? - Tim

2

我写了这个代码来找出 arduino 在 OSX 10.7.x 上插入了哪个开发设备:请享用。

#!/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

0

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

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
另一种解决方案是如果您想要纯粹的 shell 解决方案,则可以使用“ls”和awk的组合,我发现它对各种小众应用非常有用。
首先插入您的Arduino,并确保在执行时它能够显示出来。
ls /dev/tty*

crw-rw---- 1 root dialout 166, 0 2012-10-16 18:37 /dev/ttyACM0

我的Arduino Uno显示为ttyACM*,因此我修改了命令以使其更具选择性,然后将输出管道传递到awk,这样它就可以非常容易地打印空格分隔字段。

ls /dev/ttyACM* | awk {'print $9'}

/dev/ttyACM0

你仍然需要对最终输出进行一些处理,例如将其导出以在Shell中使用或发送到文件以供以后阅读。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接