-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test and correct receiving more than one packet (#1965)
- Loading branch information
1 parent
2a6433a
commit 9b67640
Showing
4 changed files
with
92 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,47 @@ | ||
"""ModbusProtocol network stub.""" | ||
from __future__ import annotations | ||
|
||
from pymodbus.transport.transport import ModbusProtocol | ||
from typing import Callable | ||
|
||
from pymodbus.transport.transport import CommParams, ModbusProtocol | ||
|
||
|
||
class ModbusProtocolStub(ModbusProtocol): | ||
"""Protocol layer including transport.""" | ||
|
||
def __init__( | ||
self, | ||
params: CommParams, | ||
is_server: bool, | ||
handler: Callable[[bytes], bytes] | None = None, | ||
) -> None: | ||
"""Initialize a stub instance.""" | ||
self.stub_handle_data = handler if handler else self.dummy_handler | ||
super().__init__(params, is_server) | ||
|
||
|
||
async def start_run(self): | ||
"""Call need functions to start server/client.""" | ||
if self.is_server: | ||
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.""" | ||
new_stub = ModbusProtocolStub(self.comm_params, False) | ||
new_stub.stub_handle_data = self.stub_handle_data | ||
return new_stub | ||
|
||
# ---------------- # | ||
# external methods # | ||
# ---------------- # | ||
def stub_handle_data(self, data: bytes) -> bytes | None: | ||
def dummy_handler(self, data: bytes) -> bytes | None: | ||
"""Handle received data.""" | ||
if len(data) > 5: | ||
return data | ||
return None | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters