Skip to content

Commit

Permalink
Data transfer ok.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Feb 4, 2024
1 parent 9bb8672 commit 13445d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 7 additions & 7 deletions pymodbus/transport/stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ class ModbusProtocolStub(ModbusProtocol):
async def start_run(self):
"""Call need functions to start server/client."""
if self.is_server:
await self.transport_listen()
else:
await self.transport_connect()
self.transport = self
return await self.transport_listen()
return await self.transport_connect()

def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data."""
if (response := self.stub_handle_data(data)):
self.transport_send(response)
return len(data)

def callback_new_connection(self) -> ModbusProtocol:
"""Call when listener receive new connection request."""
return ModbusProtocolStub(self.comm_params, False)

# ---------------- #
# external methods #
# ---------------- #
def stub_handle_data(self, data: bytes) -> bytes | None:
"""Handle received data."""
if len(data) > 5:
return data
return None
return data
4 changes: 3 additions & 1 deletion test/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ async def test_stub(self, use_port, use_cls):
stub = ModbusProtocolStub(use_cls, True)
assert await stub.start_run()
assert await client.connect()
test_data = b"Data got echoed."
client.transport.write(test_data)
client.transport_close()
stub.transport_close()

Expand All @@ -33,7 +35,7 @@ async def test_double_packet(self, use_port, use_cls):
stub = ModbusProtocolStub(use_cls, True)
await stub.start_run()
assert await client.connect()
await client.read_holding_registers(address=1, count=2)
# await client.read_holding_registers(address=1, count=2)
# await asyncio.gather(*[client.read_holding_registers(address=x, count=2) for x in range(0, 1000, 100)])
client.transport_close()
stub.transport_close()

0 comments on commit 13445d3

Please sign in to comment.