diff options
author | David Patterson <dpatter877@gmail.com> | 2018-06-16 21:13:15 -0400 |
---|---|---|
committer | David Patterson <dpatter877@gmail.com> | 2018-06-16 21:13:15 -0400 |
commit | daaf33edd1a054a52fa8ec297a5767d06f96a942 (patch) | |
tree | 540331248beb30c2d63740f0080d64b1bb5834f2 | |
parent | d7ae8f668f0d55abe2808144a1ee6c8e1254f13b (diff) | |
download | pyserial-git-daaf33edd1a054a52fa8ec297a5767d06f96a942.tar.gz |
Documented read_until and updated parameter name to reflect functionality beyond just line terminators.
-rw-r--r-- | documentation/pyserial_api.rst | 16 | ||||
-rw-r--r-- | serial/serialutil.py | 8 |
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 |