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.

111 lines
3.3KB

  1. #!/usr/bin/env python
  2. #
  3. # Serial port enumeration. Console tool and backend selection.
  4. #
  5. # This file is part of pySerial. https://github.com/pyserial/pyserial
  6. # (C) 2011-2015 Chris Liechti <cliechti@gmx.net>
  7. #
  8. # SPDX-License-Identifier: BSD-3-Clause
  9. """\
  10. This module will provide a function called comports that returns an
  11. iterable (generator or list) that will enumerate available com ports. Note that
  12. on some systems non-existent ports may be listed.
  13. Additionally a grep function is supplied that can be used to search for ports
  14. based on their descriptions or hardware ID.
  15. """
  16. from __future__ import absolute_import
  17. import sys
  18. import os
  19. import re
  20. # chose an implementation, depending on os
  21. #~ if sys.platform == 'cli':
  22. #~ else:
  23. if os.name == 'nt': # sys.platform == 'win32':
  24. from serial.tools.list_ports_windows import comports
  25. elif os.name == 'posix':
  26. from serial.tools.list_ports_posix import comports
  27. #~ elif os.name == 'java':
  28. else:
  29. raise ImportError("Sorry: no implementation for your platform ('{}') available".format(os.name))
  30. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  31. def grep(regexp, include_links=False):
  32. """\
  33. Search for ports using a regular expression. Port name, description and
  34. hardware ID are searched. The function returns an iterable that returns the
  35. same tuples as comport() would do.
  36. """
  37. r = re.compile(regexp, re.I)
  38. for info in comports(include_links):
  39. port, desc, hwid = info
  40. if r.search(port) or r.search(desc) or r.search(hwid):
  41. yield info
  42. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  43. def main():
  44. import argparse
  45. parser = argparse.ArgumentParser(description='Serial port enumeration')
  46. parser.add_argument(
  47. 'regexp',
  48. nargs='?',
  49. help='only show ports that match this regex')
  50. parser.add_argument(
  51. '-v', '--verbose',
  52. action='store_true',
  53. help='show more messages')
  54. parser.add_argument(
  55. '-q', '--quiet',
  56. action='store_true',
  57. help='suppress all messages')
  58. parser.add_argument(
  59. '-n',
  60. type=int,
  61. help='only output the N-th entry')
  62. parser.add_argument(
  63. '-s', '--include-links',
  64. action='store_true',
  65. help='include entries that are symlinks to real devices')
  66. args = parser.parse_args()
  67. hits = 0
  68. # get iteraror w/ or w/o filter
  69. if args.regexp:
  70. if not args.quiet:
  71. sys.stderr.write("Filtered list with regexp: {!r}\n".format(args.regexp))
  72. iterator = sorted(grep(args.regexp, include_links=args.include_links))
  73. else:
  74. iterator = sorted(comports(include_links=args.include_links))
  75. # list them
  76. for n, (port, desc, hwid) in enumerate(iterator, 1):
  77. if args.n is None or args.n == n:
  78. sys.stdout.write("{:20}\n".format(port))
  79. if args.verbose:
  80. sys.stdout.write(" desc: {}\n".format(desc))
  81. sys.stdout.write(" hwid: {}\n".format(hwid))
  82. hits += 1
  83. if not args.quiet:
  84. if hits:
  85. sys.stderr.write("{} ports found\n".format(hits))
  86. else:
  87. sys.stderr.write("no ports found\n")
  88. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  89. # test
  90. if __name__ == '__main__':
  91. main()