Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve transport types #2090

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pymodbus/transport/serialtransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ def abort(self) -> None:

# ------------------------------------------------

def intern_read_ready(self):
def intern_read_ready(self) -> None:
"""Test if there are data waiting."""
try:
if data := self.sync_serial.read(1024):
self.intern_protocol.data_received(data)
self.intern_protocol.data_received(data) # type: ignore[attr-defined]
except serial.SerialException as exc:
self.close(exc=exc)

def intern_write_ready(self):
def intern_write_ready(self) -> None:
"""Asynchronously write buffered data."""
data = b"".join(self.intern_write_buffer)
try:
if (nlen := self.sync_serial.write(data)) < len(data):
if (nlen := self.sync_serial.write(data)) and nlen < len(data):
self.intern_write_buffer = [data[nlen:]]
if not self.poll_task:
self.async_loop.add_writer(
Expand All @@ -154,8 +154,8 @@ async def polling_task(self):
self.intern_write_ready()
if self.sync_serial.in_waiting:
self.intern_read_ready()
except asyncio.CancelledError as exc:
self.close(exc)
except asyncio.CancelledError:
self.close(None)


async def create_serial_connection(
Expand Down