在Python中枚举串行端口(包括虚拟端口)的跨平台方法是什么? [英] What is the cross-platform method of enumerating serial ports in Python (including virtual ports)?

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

问题描述

注意:我正在使用Python 2.7和pySerial进行串行通信。

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

我发现本文列出了两种方法:<一个href = http://www.zaber.com/wiki/Software/Python#Displaying_a_list_of_available_serial_ports rel = nofollow noreferrer> 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: programmatically finding all serial ports
  • MacOS: what's the difference between /dev/tty.* and /dev/cu.*?
  • How to find all serial devices (ttyS, ttyUSB, ..) on Linux without opening them?

推荐答案

这就是我一直在使用的东西。这是我上面发布的方法的汇总。不过,我仍然希望看到更好的解决方案。

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中枚举串行端口(包括虚拟端口)的跨平台方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆