From 96722cd932d0e230d2eaeb4521aa8d8da5dfaac4 Mon Sep 17 00:00:00 2001 From: Collin Epstein Date: Thu, 10 Oct 2024 14:23:02 -0400 Subject: [PATCH 1/4] add readline method to asyncserial class --- asyncserial/asyncserial.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/asyncserial/asyncserial.py b/asyncserial/asyncserial.py index 522d006..1d605cd 100644 --- a/asyncserial/asyncserial.py +++ b/asyncserial/asyncserial.py @@ -33,6 +33,12 @@ async def write_exactly(self, data): res = await self.write(data) data = data[res:] + async def readline(self): + newline = b'\n' + data = bytearray() + while newline not in data: + data += await self.read(1) + return data if os.name != "nt": class AsyncSerial(AsyncSerialBase): From eb58196c7b5bd682b4c687f49e8e1fc61ef7ce2c Mon Sep 17 00:00:00 2001 From: Collin Epstein Date: Mon, 21 Oct 2024 15:54:44 -0400 Subject: [PATCH 2/4] implement readline with read_exactly --- asyncserial/asyncserial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncserial/asyncserial.py b/asyncserial/asyncserial.py index 1d605cd..82e2d11 100644 --- a/asyncserial/asyncserial.py +++ b/asyncserial/asyncserial.py @@ -37,7 +37,7 @@ async def readline(self): newline = b'\n' data = bytearray() while newline not in data: - data += await self.read(1) + data += await self.read_exactly(1) return data if os.name != "nt": From 54cc010fe11fc8625fa5bf6fc8dbc6a0f8fdf146 Mon Sep 17 00:00:00 2001 From: Collin Epstein Date: Mon, 21 Oct 2024 16:09:40 -0400 Subject: [PATCH 3/4] add terminator character as an argument for readline --- asyncserial/asyncserial.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/asyncserial/asyncserial.py b/asyncserial/asyncserial.py index 82e2d11..98d8592 100644 --- a/asyncserial/asyncserial.py +++ b/asyncserial/asyncserial.py @@ -33,10 +33,9 @@ async def write_exactly(self, data): res = await self.write(data) data = data[res:] - async def readline(self): - newline = b'\n' + async def readline(self, terminator="\n"): data = bytearray() - while newline not in data: + while terminator not in data: data += await self.read_exactly(1) return data From 9fdf4cf3d2c5c61747345b1c7c34ceb85643dbff Mon Sep 17 00:00:00 2001 From: Collin Epstein Date: Mon, 21 Oct 2024 16:14:14 -0400 Subject: [PATCH 4/4] newline character as default argument of readline is byte type --- asyncserial/asyncserial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncserial/asyncserial.py b/asyncserial/asyncserial.py index 98d8592..fea8df0 100644 --- a/asyncserial/asyncserial.py +++ b/asyncserial/asyncserial.py @@ -33,7 +33,7 @@ async def write_exactly(self, data): res = await self.write(data) data = data[res:] - async def readline(self, terminator="\n"): + async def readline(self, terminator=b"\n"): data = bytearray() while terminator not in data: data += await self.read_exactly(1)