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