summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzsquareplusc <cliechti@gmx.net>2018-06-19 01:24:33 +0200
committerGitHub <noreply@github.com>2018-06-19 01:24:33 +0200
commit826533f25a8e327cb9f3885dacce0e091da0a055 (patch)
tree540331248beb30c2d63740f0080d64b1bb5834f2
parentd7ae8f668f0d55abe2808144a1ee6c8e1254f13b (diff)
parentdaaf33edd1a054a52fa8ec297a5767d06f96a942 (diff)
downloadpyserial-git-826533f25a8e327cb9f3885dacce0e091da0a055.tar.gz
Merge pull request #356 from jdpatt/master
Documented read_until function, rename parameter for read_until
-rw-r--r--documentation/pyserial_api.rst16
-rw-r--r--serial/serialutil.py8
2 files changed, 20 insertions, 4 deletions
diff --git a/documentation/pyserial_api.rst b/documentation/pyserial_api.rst
index 2045244..c821773 100644
--- a/documentation/pyserial_api.rst
+++ b/documentation/pyserial_api.rst
@@ -157,6 +157,22 @@ Native ports
Returns an instance of :class:`bytes` when available (Python 2.6
and newer) and :class:`str` otherwise.
+ .. method:: read_until(expected=LF, size=None)
+
+ :param expected: The byte string to search for.
+ :param size: Number of bytes to read.
+ :return: Bytes read from the port.
+ :rtype: bytes
+
+ Read until an expected sequence is found ('\n' by default), the size
+ is exceeded or until timeout occurs. If a timeout is set it may
+ return less characters as requested. With no timeout it will block
+ until the requested number of bytes is read.
+
+ .. versionchanged:: 2.5
+ Returns an instance of :class:`bytes` when available (Python 2.6
+ and newer) and :class:`str` otherwise.
+
.. method:: write(data)
:param data: Data to send.
diff --git a/serial/serialutil.py b/serial/serialutil.py
index e847a6a..2cce816 100644
--- a/serial/serialutil.py
+++ b/serial/serialutil.py
@@ -649,19 +649,19 @@ class SerialBase(io.RawIOBase):
"""
return self.read(self.in_waiting)
- def read_until(self, terminator=LF, size=None):
+ def read_until(self, expected=LF, size=None):
"""\
- Read until a termination sequence is found ('\n' by default), the size
+ Read until an expected sequence is found ('\n' by default), the size
is exceeded or until timeout occurs.
"""
- lenterm = len(terminator)
+ lenterm = len(expected)
line = bytearray()
timeout = Timeout(self._timeout)
while True:
c = self.read(1)
if c:
line += c
- if line[-lenterm:] == terminator:
+ if line[-lenterm:] == expected:
break
if size is not None and len(line) >= size:
break