From df0c09e901feaf538fa1ec87fec36625807e2842 Mon Sep 17 00:00:00 2001 From: andrew-harness <75149647+andrew-harness@users.noreply.github.com> Date: Fri, 31 May 2024 13:34:48 -0400 Subject: [PATCH] Fix writing to serial (rs485) on windows os. (#2191) Co-authored-by: jan iversen --- pymodbus/transport/serialtransport.py | 6 +++--- test/transport/test_comm.py | 1 - test/transport/test_serial.py | 1 + 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pymodbus/transport/serialtransport.py b/pymodbus/transport/serialtransport.py index c86494c97..6d632fcae 100644 --- a/pymodbus/transport/serialtransport.py +++ b/pymodbus/transport/serialtransport.py @@ -13,7 +13,7 @@ class SerialTransport(asyncio.Transport): """An asyncio serial transport.""" - force_poll: bool = False + force_poll: bool = os.name == "nt" def __init__(self, loop, protocol, *args, **kwargs) -> None: """Initialize.""" @@ -29,7 +29,7 @@ def __init__(self, loop, protocol, *args, **kwargs) -> None: def setup(self) -> None: """Prepare to read/write.""" - if os.name == "nt" or self.force_poll: + if self.force_poll: self.poll_task = asyncio.create_task(self.polling_task()) self.poll_task.set_name("SerialTransport poll") else: @@ -56,7 +56,7 @@ def close(self, exc: Exception | None = None) -> None: def write(self, data) -> None: """Write some data to the transport.""" self.intern_write_buffer.append(data) - if not self.poll_task: + if not self.force_poll: self.async_loop.add_writer(self.sync_serial.fileno(), self.intern_write_ready) def flush(self) -> None: diff --git a/test/transport/test_comm.py b/test/transport/test_comm.py index 4ddf7b742..8b175bc09 100644 --- a/test/transport/test_comm.py +++ b/test/transport/test_comm.py @@ -179,7 +179,6 @@ async def test_serial_poll(self, client, server, use_port): SerialTransport.force_poll = True assert await client.connect() await asyncio.sleep(0.5) - SerialTransport.force_poll = False assert len(server.active_connections) == 1 server_connected = list(server.active_connections.values())[0] test_data = b"abcd" * 1000 diff --git a/test/transport/test_serial.py b/test/transport/test_serial.py index 62f54e8b4..c39e11e4c 100644 --- a/test/transport/test_serial.py +++ b/test/transport/test_serial.py @@ -100,6 +100,7 @@ async def test_write_force_poll(self): ) await asyncio.sleep(0) transport.write(b"abcd") + await asyncio.sleep(0.5) transport.close() SerialTransport.force_poll = False