Skip to content

Commit

Permalink
Adding support for assertion in read_until api.
Browse files Browse the repository at this point in the history
  • Loading branch information
reharish committed Sep 20, 2024
1 parent f7bab66 commit 17339e0
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions SerialLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,29 @@ def write(self, data: str):
raise PySerialError(f"Failed to write to device: {exc}") from exc

@keyword("Read until")
def read_until(self, expected: str) -> str:
def read_until(self, expected: str, quiet=True) -> str:
"""
Reads data from the serial device until a specified string is encountered.
``expected`` - The string to read until.
=== Example ===
| Read until | <expected>
| Read until | string
| Read until | string quiet=False
=== Returns ===
The read data as a string.
If the expected not found in the read data, It throws error.
"""
if not self.device:
raise PySerialError("Device not connected to start read")
expected = expected.encode()
buff = self.device.read_until(expected)
self.buffer.write(buff)
return buff.decode(self.unicode)
data = buff.decode(self.unicode)
if quiet or expected in buff.decode(self.unicode):
return data
raise PySerialError(f"Expected: {expected} not in {data}")

@keyword("Read all")
def read_all(self) -> str:
Expand Down

0 comments on commit 17339e0

Please sign in to comment.