Steven Michalske | 2a9486d | 2015-08-06 22:35:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame^] | 2 | # |
cliechti | fab0987 | 2009-02-07 00:25:44 +0000 | [diff] [blame] | 3 | # portable serial port access with python |
| 4 | # this is a wrapper module for different platform implementations |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 5 | # |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame^] | 6 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
| 7 | # |
| 8 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 9 | |
Steven Michalske | 2a9486d | 2015-08-06 22:35:21 -0700 | [diff] [blame] | 10 | VERSION = '3.0a' |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 11 | |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 12 | import importlib |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 13 | import sys |
| 14 | |
cliechti | 4ff9724 | 2008-06-21 18:14:46 +0000 | [diff] [blame] | 15 | if sys.platform == 'cli': |
cliechti | 39cfb7b | 2011-08-22 00:30:07 +0000 | [diff] [blame] | 16 | from serial.serialcli import * |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 17 | else: |
cliechti | 4ff9724 | 2008-06-21 18:14:46 +0000 | [diff] [blame] | 18 | import os |
cliechti | fab0987 | 2009-02-07 00:25:44 +0000 | [diff] [blame] | 19 | # chose an implementation, depending on os |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 20 | if os.name == 'nt': #sys.platform == 'win32': |
cliechti | 39cfb7b | 2011-08-22 00:30:07 +0000 | [diff] [blame] | 21 | from serial.serialwin32 import * |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 22 | elif os.name == 'posix': |
cliechti | 39cfb7b | 2011-08-22 00:30:07 +0000 | [diff] [blame] | 23 | from serial.serialposix import * |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 24 | elif os.name == 'java': |
cliechti | 39cfb7b | 2011-08-22 00:30:07 +0000 | [diff] [blame] | 25 | from serial.serialjava import * |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 26 | else: |
cliechti | 330185e | 2011-08-18 22:45:17 +0000 | [diff] [blame] | 27 | raise ImportError("Sorry: no implementation for your platform ('%s') available" % (os.name,)) |
| 28 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 29 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 30 | protocol_handler_packages = [ |
| 31 | 'serial.urlhandler', |
| 32 | ] |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 33 | |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 34 | def serial_for_url(url, *args, **kwargs): |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 35 | """\ |
cliechti | 330185e | 2011-08-18 22:45:17 +0000 | [diff] [blame] | 36 | Get an instance of the Serial class, depending on port/url. The port is not |
| 37 | opened when the keyword parameter 'do_not_open' is true, by default it |
| 38 | is. All other parameters are directly passed to the __init__ method when |
| 39 | the port is instantiated. |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 40 | |
| 41 | The list of package names that is searched for protocol handlers is kept in |
cliechti | 330185e | 2011-08-18 22:45:17 +0000 | [diff] [blame] | 42 | ``protocol_handler_packages``. |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 43 | |
| 44 | e.g. we want to support a URL ``foobar://``. A module |
| 45 | ``my_handlers.protocol_foobar`` is provided by the user. Then |
| 46 | ``protocol_handler_packages.append("my_handlers")`` would extend the search |
| 47 | path so that ``serial_for_url("foobar://"))`` would work. |
| 48 | """ |
cliechti | 9b1d3ad | 2009-08-02 23:47:04 +0000 | [diff] [blame] | 49 | # check remove extra parameter to not confuse the Serial class |
| 50 | do_open = 'do_not_open' not in kwargs or not kwargs['do_not_open'] |
| 51 | if 'do_not_open' in kwargs: del kwargs['do_not_open'] |
cliechti | aaf778a | 2009-08-05 17:59:15 +0000 | [diff] [blame] | 52 | # the default is to use the native version |
| 53 | klass = Serial # 'native' implementation |
cliechti | 9b1d3ad | 2009-08-02 23:47:04 +0000 | [diff] [blame] | 54 | # check port type and get class |
cliechti | aaf778a | 2009-08-05 17:59:15 +0000 | [diff] [blame] | 55 | try: |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 56 | url_lowercase = url.lower() |
cliechti | aaf778a | 2009-08-05 17:59:15 +0000 | [diff] [blame] | 57 | except AttributeError: |
cliechti | 2a6d533 | 2011-03-04 02:08:32 +0000 | [diff] [blame] | 58 | # it's not a string, use default |
cliechti | aaf778a | 2009-08-05 17:59:15 +0000 | [diff] [blame] | 59 | pass |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 60 | else: |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 61 | if '://' in url_lowercase: |
| 62 | protocol = url_lowercase.split('://', 1)[0] |
| 63 | module_name = '.protocol_%s' % (protocol,) |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 64 | for package_name in protocol_handler_packages: |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 65 | package = importlib.import_module(package_name) |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 66 | try: |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 67 | handler_module = importlib.import_module(module_name, package_name) |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 68 | except ImportError: |
| 69 | pass |
| 70 | else: |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 71 | klass = handler_module.Serial |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 72 | break |
cliechti | 2a6d533 | 2011-03-04 02:08:32 +0000 | [diff] [blame] | 73 | else: |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 74 | raise ValueError('invalid URL, protocol %r not known' % (protocol,)) |
cliechti | aaf778a | 2009-08-05 17:59:15 +0000 | [diff] [blame] | 75 | else: |
| 76 | klass = Serial # 'native' implementation |
cliechti | 9b1d3ad | 2009-08-02 23:47:04 +0000 | [diff] [blame] | 77 | # instantiate and open when desired |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 78 | instance = klass(None, *args, **kwargs) |
| 79 | instance.port = url |
cliechti | 9b1d3ad | 2009-08-02 23:47:04 +0000 | [diff] [blame] | 80 | if do_open: |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 81 | instance.open() |
| 82 | return instance |