【问题标题】:Linux and Python: auto-detect Arduino serial port [duplicate]Linux和Python:自动检测Arduino串口[重复]
【发布时间】:2012-07-06 15:09:31
【问题描述】:

我在使用 Mac/Linux 在 Python 中自动检测我的 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 脚本调用以返回正确的字符串?我尝试使用several commands to call shell scripts,但没有一个成功。

【问题讨论】:

    标签: python macos serial-port arduino


    【解决方案1】:

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

    接下来,您甚至无需调用 shell 命令即可在 Python 中使用 glob!

    考虑以下代码:

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

    【讨论】:

    • 嗯,我一定是做错了什么...我尝试了代码并返回了一个空列表[]
    • @mr_schlomo:你确定你有这些tty.usbmodem 文件吗?
    • 哎呀——我的 Arduino 已断开连接!感谢您的帮助!
    • 是否可以在 Windows 上找到类似的 arduino?
    【解决方案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
    

    【讨论】:

      【解决方案3】:

      我已使用此代码自动检测 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()
      

      【讨论】:

        【解决方案4】:

        如果您想要一个纯 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 中使用或发送到文件以供以后阅读。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多