cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 1 | #! python |
| 2 | # |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 3 | # This module implements a special URL handler that uses the port listing to |
| 4 | # find ports by searching the string descriptions. |
| 5 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 6 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | 5fe3bdd | 2015-08-07 00:02:44 +0200 | [diff] [blame] | 7 | # (C) 2011-2015 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 8 | # |
| 9 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 10 | # |
Chris Liechti | 757d3ed | 2015-12-22 22:30:10 +0100 | [diff] [blame] | 11 | # URL format: hwgrep://<regexp>&<option> |
| 12 | # |
| 13 | # where <regexp> is a Python regexp according to the re module |
| 14 | # |
| 15 | # violating the normal definition for URLs, the charachter `&` is used to |
| 16 | # separate parameters from the arguments (instead of `?`, but the question mark |
| 17 | # is heavily used in regexp'es) |
| 18 | # |
| 19 | # options: |
| 20 | # n=<N> pick the N'th entry instead of the first one (numbering starts at 1) |
Chris Liechti | 9df194c | 2015-12-23 20:12:17 +0100 | [diff] [blame] | 21 | # skip_busy tries to open port to check if it is busy, fails on posix as ports are not locked! |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 22 | |
| 23 | import serial |
| 24 | import serial.tools.list_ports |
| 25 | |
Chris Liechti | 5fe3bdd | 2015-08-07 00:02:44 +0200 | [diff] [blame] | 26 | try: |
| 27 | basestring |
| 28 | except NameError: |
| 29 | basestring = str # python 3 |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 30 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 31 | |
Chris Liechti | 5fe3bdd | 2015-08-07 00:02:44 +0200 | [diff] [blame] | 32 | class Serial(serial.Serial): |
| 33 | """Just inherit the native Serial port implementation and patch the port property.""" |
| 34 | |
| 35 | @serial.Serial.port.setter |
| 36 | def port(self, value): |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 37 | """translate port name before storing it""" |
cliechti | 9a3809e | 2011-08-19 23:43:10 +0000 | [diff] [blame] | 38 | if isinstance(value, basestring) and value.startswith('hwgrep://'): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 39 | serial.Serial.port.__set__(self, self.from_url(value)) |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 40 | else: |
Chris Liechti | 5fe3bdd | 2015-08-07 00:02:44 +0200 | [diff] [blame] | 41 | serial.Serial.port.__set__(self, value) |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 42 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 43 | def from_url(self, url): |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 44 | """extract host and port from an URL string""" |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 45 | if url.lower().startswith("hwgrep://"): |
| 46 | url = url[9:] |
Chris Liechti | 757d3ed | 2015-12-22 22:30:10 +0100 | [diff] [blame] | 47 | n = 0 |
Chris Liechti | 9df194c | 2015-12-23 20:12:17 +0100 | [diff] [blame] | 48 | test_open = False |
Chris Liechti | 757d3ed | 2015-12-22 22:30:10 +0100 | [diff] [blame] | 49 | args = url.split('&') |
| 50 | regexp = args.pop(0) |
| 51 | for arg in args: |
| 52 | if '=' in arg: |
| 53 | option, value = arg.split('=', 1) |
| 54 | else: |
| 55 | option = arg |
| 56 | value = None |
| 57 | if option == 'n': |
| 58 | # pick n'th element |
| 59 | n = int(value) - 1 |
| 60 | if n < 1: |
| 61 | raise ValueError('option "n" expects a positive integer larger than 1: %r' % (value,)) |
Chris Liechti | 9df194c | 2015-12-23 20:12:17 +0100 | [diff] [blame] | 62 | elif option == 'skip_busy': |
| 63 | # open to test if port is available. not the nicest way.. |
| 64 | test_open = True |
Chris Liechti | 757d3ed | 2015-12-22 22:30:10 +0100 | [diff] [blame] | 65 | else: |
| 66 | raise ValueError('unknown option: %r' % (option,)) |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 67 | # use a for loop to get the 1st element from the generator |
Chris Liechti | 757d3ed | 2015-12-22 22:30:10 +0100 | [diff] [blame] | 68 | for port, desc, hwid in sorted(serial.tools.list_ports.grep(regexp)): |
Chris Liechti | 9df194c | 2015-12-23 20:12:17 +0100 | [diff] [blame] | 69 | if test_open: |
| 70 | try: |
| 71 | s = serial.Serial(port) |
| 72 | except serial.SerialException: |
| 73 | # it has some error, skip this one |
| 74 | continue |
| 75 | else: |
| 76 | s.close() |
Chris Liechti | 757d3ed | 2015-12-22 22:30:10 +0100 | [diff] [blame] | 77 | if n: |
| 78 | n -= 1 |
| 79 | continue |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 80 | return port |
| 81 | else: |
cliechti | 9a3809e | 2011-08-19 23:43:10 +0000 | [diff] [blame] | 82 | raise serial.SerialException('no ports found matching regexp %r' % (url,)) |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 83 | |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 84 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 85 | if __name__ == '__main__': |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 86 | s = Serial(None) |
| 87 | s.port = 'hwgrep://ttyS0' |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 88 | print(s) |