cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 1 | #! python |
| 2 | # |
| 3 | # Python Serial Port Extension for Win32, Linux, BSD, Jython |
| 4 | # see __init__.py |
| 5 | # |
| 6 | # This module implements a special URL handler that uses the port listing to |
| 7 | # find ports by searching the string descriptions. |
| 8 | # |
Chris Liechti | 5fe3bdd | 2015-08-07 00:02:44 +0200 | [diff] [blame] | 9 | # (C) 2011-2015 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 10 | # |
| 11 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 12 | # |
| 13 | # URL format: hwgrep://regexp |
| 14 | |
| 15 | import serial |
| 16 | import serial.tools.list_ports |
| 17 | |
Chris Liechti | 5fe3bdd | 2015-08-07 00:02:44 +0200 | [diff] [blame] | 18 | try: |
| 19 | basestring |
| 20 | except NameError: |
| 21 | basestring = str # python 3 |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 22 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame^] | 23 | |
Chris Liechti | 5fe3bdd | 2015-08-07 00:02:44 +0200 | [diff] [blame] | 24 | class Serial(serial.Serial): |
| 25 | """Just inherit the native Serial port implementation and patch the port property.""" |
| 26 | |
| 27 | @serial.Serial.port.setter |
| 28 | def port(self, value): |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 29 | """translate port name before storing it""" |
cliechti | 9a3809e | 2011-08-19 23:43:10 +0000 | [diff] [blame] | 30 | if isinstance(value, basestring) and value.startswith('hwgrep://'): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 31 | serial.Serial.port.__set__(self, self.from_url(value)) |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 32 | else: |
Chris Liechti | 5fe3bdd | 2015-08-07 00:02:44 +0200 | [diff] [blame] | 33 | serial.Serial.port.__set__(self, value) |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 34 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 35 | def from_url(self, url): |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 36 | """extract host and port from an URL string""" |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 37 | if url.lower().startswith("hwgrep://"): |
| 38 | url = url[9:] |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 39 | # use a for loop to get the 1st element from the generator |
| 40 | for port, desc, hwid in serial.tools.list_ports.grep(url): |
| 41 | return port |
| 42 | else: |
cliechti | 9a3809e | 2011-08-19 23:43:10 +0000 | [diff] [blame] | 43 | raise serial.SerialException('no ports found matching regexp %r' % (url,)) |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 44 | |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 45 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 46 | if __name__ == '__main__': |
cliechti | 6fa1911 | 2011-08-19 01:50:49 +0000 | [diff] [blame] | 47 | s = Serial(None) |
| 48 | s.port = 'hwgrep://ttyS0' |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 49 | print(s) |