Skip to content

Commit

Permalink
Fix writing to serial (rs485) on windows os. (#2191)
Browse files Browse the repository at this point in the history
Co-authored-by: jan iversen <[email protected]>
  • Loading branch information
andrew-harness and janiversen authored May 31, 2024
1 parent 450b96f commit df0c09e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pymodbus/transport/serialtransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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:
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion test/transport/test_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/transport/test_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit df0c09e

Please sign in to comment.