summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2010-07-21 15:46:39 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2010-07-21 15:46:39 +0000
commit02a829f3a0764015b97c90571b1ca2a8a682d32a (patch)
tree2eacc69eb296ed71ba14a6d14e8590b99938471f
parent27eb48e6fabc870c875d36137ec7e97d4e902f39 (diff)
downloadpyserial-02a829f3a0764015b97c90571b1ca2a8a682d32a.tar.gz
- move [x]readline[s] to FileLike base class (io module provides implementation in the other case)
- CR and LF constants added git-svn-id: http://svn.code.sf.net/p/pyserial/code/trunk/pyserial@371 f19166aa-fa4f-0410-85c2-fa1106f25c8a
-rw-r--r--serial/serialutil.py88
1 files changed, 46 insertions, 42 deletions
diff --git a/serial/serialutil.py b/serial/serialutil.py
index 969467c..0f37218 100644
--- a/serial/serialutil.py
+++ b/serial/serialutil.py
@@ -5,7 +5,7 @@
# (C) 2001-2009 Chris Liechti <cliechti@gmx.net>
# this is distributed under a free software license, see license.txt
-# compatibility for older Python < 2.6
+# compatibility for older Python < 2.6
try:
bytes
bytearray
@@ -44,6 +44,9 @@ def to_bytes(seq):
XON = to_bytes([17])
XOFF = to_bytes([19])
+CR = to_bytes([13])
+LF = to_bytes([10])
+
PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = 'N', 'E', 'O', 'M', 'S'
STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO = (1, 1.5, 2)
@@ -120,6 +123,48 @@ class FileLike(object):
def __iter__(self):
return self
+
+ def readline(self, size=None, eol=LF):
+ """read a line which is terminated with end-of-line (eol) character
+ ('\n' by default) or until timeout."""
+ leneol = len(eol)
+ line = bytearray()
+ while True:
+ c = self.read(1)
+ if c:
+ line += c
+ if line[-leneol:] == eol:
+ break
+ if size is not None and len(line) >= size:
+ break
+ else:
+ break
+ return bytes(line)
+
+ def readlines(self, sizehint=None, eol='\n'):
+ """read a list of lines, until timeout.
+ sizehint is ignored."""
+ if self.timeout is None:
+ raise ValueError("Serial port MUST have enabled timeout for this function!")
+ lines = []
+ while True:
+ line = self.readline(eol=eol)
+ if line:
+ lines.append(line)
+ if line[-1] != eol: # was the line received with a timeout?
+ break
+ else:
+ break
+ return lines
+
+ def xreadlines(self, sizehint=None):
+ """Read lines, implemented as generator. It will raise StopIteration on
+ timeout (empty read). sizehint is ignored."""
+ while True:
+ line = self.readline()
+ if not line: break
+ yield line
+
# other functions of file-likes - not used by pySerial
#~ readinto(b)
@@ -436,47 +481,6 @@ class SerialBase(object):
self.dsrdtr,
)
- # - - - - - - - - - - - - - - - - - - - - - - - -
-
- def readline(self, size=None, eol='\n'):
- """read a line which is terminated with end-of-line (eol) character
- ('\n' by default) or until timeout."""
- line = bytearray()
- while 1:
- c = self.read(1)
- if c:
- line += c
- if c == eol:
- break
- if size is not None and len(line) >= size:
- break
- else:
- break
- return bytes(line)
-
- def readlines(self, sizehint=None, eol='\n'):
- """read a list of lines, until timeout.
- sizehint is ignored."""
- if self.timeout is None:
- raise ValueError("Serial port MUST have enabled timeout for this function!")
- lines = []
- while 1:
- line = self.readline(eol=eol)
- if line:
- lines.append(line)
- if line[-1] != eol: # was the line received with a timeout?
- break
- else:
- break
- return lines
-
- def xreadlines(self, sizehint=None):
- """Read lines, implemented as generator. It will raise StopIteration on
- timeout (empty read). sizehint is ignored."""
- while True:
- line = self.readline()
- if not line: break
- yield line
# - - - - - - - - - - - - - - - - - - - - - - - -
# compatibility with io library