Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
list_ports.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # portable serial port access with python
4 # this is a wrapper module for different platform implementations of the
5 # port enumeration feature
6 #
7 # (C) 2011 Chris Liechti <cliechti@gmx.net>
8 # this is distributed under a free software license, see license.txt
9 
10 """\
11 This module will provide a function called comports that returns an
12 iterable (generator or list) that will enumerate available com ports. Note that
13 on some systems non-existent ports may be listed.
14 
15 Additionally a grep function is supplied that can be used to search for ports
16 based on their descriptions or hardware ID.
17 """
18 
19 import sys, os, re
20 
21 # chose an implementation, depending on os
22 #~ if sys.platform == 'cli':
23 #~ else:
24 import os
25 # chose an implementation, depending on os
26 if os.name == 'nt': #sys.platform == 'win32':
28 elif os.name == 'posix':
29  from serial.tools.list_ports_posix import *
30 #~ elif os.name == 'java':
31 else:
32  raise ImportError("Sorry: no implementation for your platform ('%s') available" % (os.name,))
33 
34 
35 def grep(regexp):
36  """\
37  Search for ports using a regular expression. Port name, description and
38  hardware ID are searched. The function returns an iterable that returns the
39  same tuples as comport() would do.
40  """
41  for port, desc, hwid in comports():
42  if re.search(regexp, port, re.I) or re.search(regexp, desc) or re.search(regexp, hwid):
43  yield port, desc, hwid
44 
45 
46 def main():
47  import optparse
48 
49  parser = optparse.OptionParser(
50  usage = "%prog [options] [<regexp>]",
51  description = "Miniterm - A simple terminal program for the serial port."
52  )
53 
54  parser.add_option("--debug",
55  help="print debug messages and tracebacks (development mode)",
56  dest="debug",
57  default=False,
58  action='store_true')
59 
60  parser.add_option("-v", "--verbose",
61  help="show more messages (can be given multiple times)",
62  dest="verbose",
63  default=1,
64  action='count')
65 
66  parser.add_option("-q", "--quiet",
67  help="suppress all messages",
68  dest="verbose",
69  action='store_const',
70  const=0)
71 
72  (options, args) = parser.parse_args()
73 
74 
75  hits = 0
76  # get iteraror w/ or w/o filter
77  if args:
78  if len(args) > 1:
79  parser.error('more than one regexp not supported')
80  print "Filtered list with regexp: %r" % (args[0],)
81  iterator = sorted(grep(args[0]))
82  else:
83  iterator = sorted(comports())
84  # list them
85  for port, desc, hwid in iterator:
86  print "%-20s" % (port,)
87  if options.verbose > 1:
88  print " desc: %s" % (desc,)
89  print " hwid: %s" % (hwid,)
90  hits += 1
91  if options.verbose:
92  if hits:
93  print "%d ports found" % (hits,)
94  else:
95  print "no ports found"
96 
97 # test
98 if __name__ == '__main__':
99  main()