• <i id='sFqhh'><tr id='sFqhh'><dt id='sFqhh'><q id='sFqhh'><span id='sFqhh'><b id='sFqhh'><form id='sFqhh'><ins id='sFqhh'></ins><ul id='sFqhh'></ul><sub id='sFqhh'></sub></form><legend id='sFqhh'></legend><bdo id='sFqhh'><pre id='sFqhh'><center id='sFqhh'></center></pre></bdo></b><th id='sFqhh'></th></span></q></dt></tr></i><div id='sFqhh'><tfoot id='sFqhh'></tfoot><dl id='sFqhh'><fieldset id='sFqhh'></fieldset></dl></div>

      <bdo id='sFqhh'></bdo><ul id='sFqhh'></ul>
    <tfoot id='sFqhh'></tfoot>
  • <small id='sFqhh'></small><noframes id='sFqhh'>

        <legend id='sFqhh'><style id='sFqhh'><dir id='sFqhh'><q id='sFqhh'></q></dir></style></legend>

      1. Python中枚举串口(包括虚拟端口)的跨平台方法是什么?

        时间:2023-07-23

          • <tfoot id='ctA9y'></tfoot>

            1. <legend id='ctA9y'><style id='ctA9y'><dir id='ctA9y'><q id='ctA9y'></q></dir></style></legend>

                  <tbody id='ctA9y'></tbody>

                <small id='ctA9y'></small><noframes id='ctA9y'>

                • <bdo id='ctA9y'></bdo><ul id='ctA9y'></ul>
                  <i id='ctA9y'><tr id='ctA9y'><dt id='ctA9y'><q id='ctA9y'><span id='ctA9y'><b id='ctA9y'><form id='ctA9y'><ins id='ctA9y'></ins><ul id='ctA9y'></ul><sub id='ctA9y'></sub></form><legend id='ctA9y'></legend><bdo id='ctA9y'><pre id='ctA9y'><center id='ctA9y'></center></pre></bdo></b><th id='ctA9y'></th></span></q></dt></tr></i><div id='ctA9y'><tfoot id='ctA9y'></tfoot><dl id='ctA9y'><fieldset id='ctA9y'></fieldset></dl></div>

                  本文介绍了Python中枚举串口(包括虚拟端口)的跨平台方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  注意:我使用 Python 2.7 和 pySerial 进行串行通信.

                  Note: I'm using Python 2.7, and pySerial for serial communications.

                  我发现这篇文章列出了两种方法:http://www.zaber.com/wiki/Software/Python#Displaying_a_list_of_available_serial_ports

                  I found this article which lists two ways: http://www.zaber.com/wiki/Software/Python#Displaying_a_list_of_available_serial_ports

                  此方法适用于 Windows 和 Linux,但有时会错过 Linux 上的虚拟端口:

                  This method works on Windows and Linux, but sometimes misses virtual ports on Linux:

                  import serial
                  
                  def scan():
                     # scan for available ports. return a list of tuples (num, name)
                     available = []
                     for i in range(256):
                         try:
                             s = serial.Serial(i)
                             available.append( (i, s.portstr))
                             s.close()
                         except serial.SerialException:
                             pass
                     return available
                  
                  print "Found ports:"
                  for n,s in scan(): print "(%d) %s" % (n,s)
                  

                  而这个仅适用于 Linux,但包含虚拟端口:

                  And this one that only works on Linux, but includes virtual ports:

                  import serial, glob
                  
                  def scan():
                     # scan for available ports. return a list of device names.
                     return glob.glob('/dev/ttyS*') + glob.glob('/dev/ttyUSB*')
                  
                  print "Found ports:"
                  for name in scan(): print name
                  

                  我想我可以在 Linux 上运行时使用第二种方法(包括虚拟端口的方法)进行平台检测,在运行 Windows 时使用第一种方法,但是 Mac 呢?

                  I suppose I could do platform detection to use the second method (the one that includes virtual ports) when running on Linux, and the first method when running Windows, but what about Mac?

                  无论平台如何,我应该如何枚举串行端口(也是虚拟的)?

                  How should I enumerate serial ports (virtual too) regardless of platform?

                  编辑

                  我发现了一些相关的问题:

                  I found a few pertinent questions:

                  • MacPython:以编程方式查找所有串行端口
                  • MacOS:/dev/有什么区别tty.* 和/dev/cu.*?
                  • 如何查找Linux 上的所有串行设备(ttyS、ttyUSB、..)都无需打开?

                  推荐答案

                  这是我一直在使用的.这是我上面发布的方法的混搭.不过,我仍然希望看到更好的解决方案.

                  This is what I've been using. It's a mashup of the methods I posted above. I'd still like to see better solutions, though.

                  # A function that tries to list serial ports on most common platforms
                  def list_serial_ports():
                      system_name = platform.system()
                      if system_name == "Windows":
                          # Scan for available ports.
                          available = []
                          for i in range(256):
                              try:
                                  s = serial.Serial(i)
                                  available.append(i)
                                  s.close()
                              except serial.SerialException:
                                  pass
                          return available
                      elif system_name == "Darwin":
                          # Mac
                          return glob.glob('/dev/tty*') + glob.glob('/dev/cu*')
                      else:
                          # Assume Linux or something else
                          return glob.glob('/dev/ttyS*') + glob.glob('/dev/ttyUSB*')
                  

                  这篇关于Python中枚举串口(包括虚拟端口)的跨平台方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Arduino 的 Pyserial 问题 - 适用于 Python shell 但不适用于程序 下一篇:使用 Python 和 pyserial 访问 USB 串口

                  相关文章

                    • <bdo id='BjnzW'></bdo><ul id='BjnzW'></ul>

                  1. <i id='BjnzW'><tr id='BjnzW'><dt id='BjnzW'><q id='BjnzW'><span id='BjnzW'><b id='BjnzW'><form id='BjnzW'><ins id='BjnzW'></ins><ul id='BjnzW'></ul><sub id='BjnzW'></sub></form><legend id='BjnzW'></legend><bdo id='BjnzW'><pre id='BjnzW'><center id='BjnzW'></center></pre></bdo></b><th id='BjnzW'></th></span></q></dt></tr></i><div id='BjnzW'><tfoot id='BjnzW'></tfoot><dl id='BjnzW'><fieldset id='BjnzW'></fieldset></dl></div>

                  2. <small id='BjnzW'></small><noframes id='BjnzW'>

                    <tfoot id='BjnzW'></tfoot>
                      <legend id='BjnzW'><style id='BjnzW'><dir id='BjnzW'><q id='BjnzW'></q></dir></style></legend>