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 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 3 | # This is a wrapper module for different platform implementations |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 4 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 5 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | 91dc087 | 2016-01-11 23:48:41 +0100 | [diff] [blame] | 6 | # (C) 2001-2016 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 7 | # |
| 8 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 9 | |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 10 | import importlib |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 11 | import sys |
Robert Smallshire | 05f6e0e | 2016-06-14 08:54:18 +0200 | [diff] [blame] | 12 | from pkgutil import extend_path |
| 13 | |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 14 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 15 | from serial.serialutil import * |
| 16 | #~ SerialBase, SerialException, to_bytes, iterbytes |
| 17 | |
Chris Liechti | f1dec6a | 2016-06-12 22:47:09 +0200 | [diff] [blame] | 18 | __version__ = '3.1.1' |
Robert Smallshire | fa3abd6 | 2016-03-22 21:39:42 +0100 | [diff] [blame] | 19 | |
| 20 | VERSION = __version__ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 21 | |
Robert Smallshire | 05f6e0e | 2016-06-14 08:54:18 +0200 | [diff] [blame] | 22 | # serial is a namespace package |
| 23 | __path__ = extend_path(__path__, __name__) |
| 24 | |
Chris Liechti | 409e10b | 2016-02-10 22:40:34 +0100 | [diff] [blame] | 25 | # pylint: disable=wrong-import-position |
cliechti | 4ff9724 | 2008-06-21 18:14:46 +0000 | [diff] [blame] | 26 | if sys.platform == 'cli': |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 27 | from serial.serialcli import Serial |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 28 | else: |
cliechti | 4ff9724 | 2008-06-21 18:14:46 +0000 | [diff] [blame] | 29 | import os |
cliechti | fab0987 | 2009-02-07 00:25:44 +0000 | [diff] [blame] | 30 | # chose an implementation, depending on os |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 31 | if os.name == 'nt': # sys.platform == 'win32': |
| 32 | from serial.serialwin32 import Serial |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 33 | elif os.name == 'posix': |
Chris Liechti | cb6ce1b | 2016-02-02 01:53:56 +0100 | [diff] [blame] | 34 | from serial.serialposix import Serial, PosixPollSerial, VTIMESerial # noqa |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 35 | elif os.name == 'java': |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 36 | from serial.serialjava import Serial |
cliechti | 0bfe525 | 2008-06-21 01:36:52 +0000 | [diff] [blame] | 37 | else: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 38 | raise ImportError("Sorry: no implementation for your platform ('{}') available".format(os.name)) |
cliechti | 330185e | 2011-08-18 22:45:17 +0000 | [diff] [blame] | 39 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 40 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 41 | protocol_handler_packages = [ |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 42 | 'serial.urlhandler', |
| 43 | ] |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 44 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 45 | |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 46 | def serial_for_url(url, *args, **kwargs): |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 47 | """\ |
cliechti | 330185e | 2011-08-18 22:45:17 +0000 | [diff] [blame] | 48 | Get an instance of the Serial class, depending on port/url. The port is not |
| 49 | opened when the keyword parameter 'do_not_open' is true, by default it |
| 50 | is. All other parameters are directly passed to the __init__ method when |
| 51 | the port is instantiated. |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 52 | |
| 53 | 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] | 54 | ``protocol_handler_packages``. |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 55 | |
| 56 | e.g. we want to support a URL ``foobar://``. A module |
| 57 | ``my_handlers.protocol_foobar`` is provided by the user. Then |
| 58 | ``protocol_handler_packages.append("my_handlers")`` would extend the search |
| 59 | path so that ``serial_for_url("foobar://"))`` would work. |
| 60 | """ |
Chris Liechti | 4afb6c9 | 2015-10-16 01:11:28 +0200 | [diff] [blame] | 61 | # check and remove extra parameter to not confuse the Serial class |
| 62 | do_open = not kwargs.pop('do_not_open', False) |
| 63 | # the default is to use the native implementation |
| 64 | klass = Serial |
cliechti | aaf778a | 2009-08-05 17:59:15 +0000 | [diff] [blame] | 65 | try: |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 66 | url_lowercase = url.lower() |
cliechti | aaf778a | 2009-08-05 17:59:15 +0000 | [diff] [blame] | 67 | except AttributeError: |
cliechti | 2a6d533 | 2011-03-04 02:08:32 +0000 | [diff] [blame] | 68 | # it's not a string, use default |
cliechti | aaf778a | 2009-08-05 17:59:15 +0000 | [diff] [blame] | 69 | pass |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 70 | else: |
Chris Liechti | 4afb6c9 | 2015-10-16 01:11:28 +0200 | [diff] [blame] | 71 | # if it is an URL, try to import the handler module from the list of possible packages |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 72 | if '://' in url_lowercase: |
| 73 | protocol = url_lowercase.split('://', 1)[0] |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 74 | module_name = '.protocol_{}'.format(protocol) |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 75 | for package_name in protocol_handler_packages: |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 76 | try: |
Chris Liechti | 6c63da6 | 2016-01-25 21:39:32 +0100 | [diff] [blame] | 77 | importlib.import_module(package_name) |
Chris Liechti | baec2a3 | 2015-08-07 14:42:15 +0200 | [diff] [blame] | 78 | handler_module = importlib.import_module(module_name, package_name) |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 79 | except ImportError: |
Chris Liechti | 4afb6c9 | 2015-10-16 01:11:28 +0200 | [diff] [blame] | 80 | continue |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 81 | else: |
Chris Liechti | 8ce75fa | 2015-10-17 23:36:15 +0200 | [diff] [blame] | 82 | if hasattr(handler_module, 'serial_class_for_url'): |
| 83 | url, klass = handler_module.serial_class_for_url(url) |
| 84 | else: |
| 85 | klass = handler_module.Serial |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 86 | break |
cliechti | 2a6d533 | 2011-03-04 02:08:32 +0000 | [diff] [blame] | 87 | else: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 88 | raise ValueError('invalid URL, protocol {!r} not known'.format(protocol)) |
cliechti | 9b1d3ad | 2009-08-02 23:47:04 +0000 | [diff] [blame] | 89 | # instantiate and open when desired |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 90 | instance = klass(None, *args, **kwargs) |
| 91 | instance.port = url |
cliechti | 9b1d3ad | 2009-08-02 23:47:04 +0000 | [diff] [blame] | 92 | if do_open: |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 93 | instance.open() |
| 94 | return instance |