summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-08-02 00:00:11 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-08-02 00:00:11 +0000
commit109486b0eb2d3a2a6fcdf43043eb22eaecc8a132 (patch)
tree214342b8c598f7e6095b011453ede56e4dadef6a
parent8099bedac9dc4e882d89ebb4def3f419a350a7d3 (diff)
downloadpyserial-git-109486b0eb2d3a2a6fcdf43043eb22eaecc8a132.tar.gz
add helper function to automatically select native or RFC2217 implementation of Serial object
-rw-r--r--pyserial/serial/__init__.py14
-rw-r--r--pyserial/serial/serialposix.py4
2 files changed, 16 insertions, 2 deletions
diff --git a/pyserial/serial/__init__.py b/pyserial/serial/__init__.py
index 6e160e0..acda005 100644
--- a/pyserial/serial/__init__.py
+++ b/pyserial/serial/__init__.py
@@ -24,3 +24,17 @@ else:
else:
raise Exception("Sorry: no implementation for your platform ('%s') available" % os.name)
+
+def serial_class_for_url(url, do_not_open=False, *args, **kwargs):
+ """Get a native or a RFC2217 implementation of the Serial class, depending
+ on port/url. The port is not opened when do_not_open is true, by default it is."""
+ if url.lower().startswith('rfc2217://'):
+ import rfc2217 # late import, so that users that don't use it don't have to load it
+ klass = rfc2217.Serial # RFC2217 implementation
+ else:
+ klass = Serial # 'native' implementation
+ instance = klass(None, *args, **kwargs)
+ instance.port = url
+ if not do_not_open:
+ instance.open()
+ return instance
diff --git a/pyserial/serial/serialposix.py b/pyserial/serial/serialposix.py
index bbf7fb9..d9eec35 100644
--- a/pyserial/serial/serialposix.py
+++ b/pyserial/serial/serialposix.py
@@ -606,7 +606,7 @@ if __name__ == '__main__':
s.flushInput()
s.flushOutput()
s.write('hello')
- sys.stdio.write('%r\n' % s.read(5))
- sys.stdio.write('%s\n' % s.inWaiting())
+ sys.stdout.write('%r\n' % s.read(5))
+ sys.stdout.write('%s\n' % s.inWaiting())
del s