You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.4KB

  1. #!/usr/bin/env python
  2. #
  3. # This is a module that gathers a list of serial ports on POSIXy systems.
  4. # For some specific implementations, see also list_ports_linux, list_ports_osx
  5. #
  6. # This file is part of pySerial. https://github.com/pyserial/pyserial
  7. # (C) 2011-2015 Chris Liechti <cliechti@gmx.net>
  8. #
  9. # SPDX-License-Identifier: BSD-3-Clause
  10. """\
  11. The ``comports`` function is expected to return an iterable that yields tuples
  12. of 3 strings: port name, human readable description and a hardware ID.
  13. As currently no method is known to get the second two strings easily, they are
  14. currently just identical to the port name.
  15. """
  16. from __future__ import absolute_import
  17. import glob
  18. import sys
  19. import os
  20. from serial.tools import list_ports_common
  21. # try to detect the OS so that a device can be selected...
  22. plat = sys.platform.lower()
  23. if plat[:5] == 'linux': # Linux (confirmed) # noqa
  24. from serial.tools.list_ports_linux import comports
  25. elif plat[:6] == 'darwin': # OS X (confirmed)
  26. from serial.tools.list_ports_osx import comports
  27. elif plat == 'cygwin': # cygwin/win32
  28. # cygwin accepts /dev/com* in many contexts
  29. # (such as 'open' call, explicit 'ls'), but 'glob.glob'
  30. # and bare 'ls' do not; so use /dev/ttyS* instead
  31. def comports(include_links=False):
  32. devices = glob.glob('/dev/ttyS*')
  33. if include_links:
  34. devices.extend(list_ports_common.list_links(devices))
  35. return [list_ports_common.ListPortInfo(d) for d in devices]
  36. elif plat[:7] == 'openbsd': # OpenBSD
  37. def comports(include_links=False):
  38. devices = glob.glob('/dev/cua*')
  39. if include_links:
  40. devices.extend(list_ports_common.list_links(devices))
  41. return [list_ports_common.ListPortInfo(d) for d in devices]
  42. elif plat[:3] == 'bsd' or plat[:7] == 'freebsd':
  43. def comports(include_links=False):
  44. devices = glob.glob('/dev/cua*[!.init][!.lock]')
  45. if include_links:
  46. devices.extend(list_ports_common.list_links(devices))
  47. return [list_ports_common.ListPortInfo(d) for d in devices]
  48. elif plat[:6] == 'netbsd': # NetBSD
  49. def comports(include_links=False):
  50. """scan for available ports. return a list of device names."""
  51. devices = glob.glob('/dev/dty*')
  52. if include_links:
  53. devices.extend(list_ports_common.list_links(devices))
  54. return [list_ports_common.ListPortInfo(d) for d in devices]
  55. elif plat[:4] == 'irix': # IRIX
  56. def comports(include_links=False):
  57. """scan for available ports. return a list of device names."""
  58. devices = glob.glob('/dev/ttyf*')
  59. if include_links:
  60. devices.extend(list_ports_common.list_links(devices))
  61. return [list_ports_common.ListPortInfo(d) for d in devices]
  62. elif plat[:2] == 'hp': # HP-UX (not tested)
  63. def comports(include_links=False):
  64. """scan for available ports. return a list of device names."""
  65. devices = glob.glob('/dev/tty*p0')
  66. if include_links:
  67. devices.extend(list_ports_common.list_links(devices))
  68. return [list_ports_common.ListPortInfo(d) for d in devices]
  69. elif plat[:5] == 'sunos': # Solaris/SunOS
  70. def comports(include_links=False):
  71. """scan for available ports. return a list of device names."""
  72. devices = glob.glob('/dev/tty*c')
  73. if include_links:
  74. devices.extend(list_ports_common.list_links(devices))
  75. return [list_ports_common.ListPortInfo(d) for d in devices]
  76. elif plat[:3] == 'aix': # AIX
  77. def comports(include_links=False):
  78. """scan for available ports. return a list of device names."""
  79. devices = glob.glob('/dev/tty*')
  80. if include_links:
  81. devices.extend(list_ports_common.list_links(devices))
  82. return [list_ports_common.ListPortInfo(d) for d in devices]
  83. else:
  84. # platform detection has failed...
  85. import serial
  86. sys.stderr.write("""\
  87. don't know how to enumerate ttys on this system.
  88. ! I you know how the serial ports are named send this information to
  89. ! the author of this module:
  90. sys.platform = {!r}
  91. os.name = {!r}
  92. pySerial version = {}
  93. also add the naming scheme of the serial ports and with a bit luck you can get
  94. this module running...
  95. """.format(sys.platform, os.name, serial.VERSION))
  96. raise ImportError("Sorry: no implementation for your platform ('{}') available".format(os.name))
  97. # test
  98. if __name__ == '__main__':
  99. for port, desc, hwid in sorted(comports()):
  100. print("{}: {} [{}]".format(port, desc, hwid))