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