From ad7c3e97aa366de6fe88159585acca4d455ef8ad Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sun, 23 Jul 2023 21:02:59 +0200 Subject: [PATCH] no cover. --- examples/client_calls.py | 6 +++--- examples/client_custom_msg.py | 33 +++++++++++++++--------------- test/sub_examples/test_examples.py | 8 ++++---- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/examples/client_calls.py b/examples/client_calls.py index a86258e2a1..90dcb63580 100755 --- a/examples/client_calls.py +++ b/examples/client_calls.py @@ -158,7 +158,7 @@ def handle_input_registers(client): assert len(rr.registers) == 8 -def execute_information_requests(client): +def execute_information_requests(client): # pragma no cover """Execute extended information requests.""" _logger.info("### Running information requests.") rr = client.execute(req_mei.ReadDeviceInformationRequest(slave=SLAVE)) @@ -184,7 +184,7 @@ def execute_information_requests(client): # assert not (rr.event_count + rr.message_count + len(rr.events)) -def execute_diagnostic_requests(client): +def execute_diagnostic_requests(client): # pragma no cover """Execute extended diagnostic requests.""" _logger.info("### Running diagnostic requests.") message = b"OK" @@ -233,4 +233,4 @@ def main(cmdline=None): if __name__ == "__main__": - main() # pragma: no cove + main() # pragma: no cover diff --git a/examples/client_custom_msg.py b/examples/client_custom_msg.py index b6480a87c2..e99f4b9607 100755 --- a/examples/client_custom_msg.py +++ b/examples/client_custom_msg.py @@ -9,16 +9,18 @@ result = client.read_coils(1,10) print result """ +import asyncio import logging import struct from pymodbus.bit_read_message import ReadCoilsRequest -from pymodbus.client import ModbusTcpClient as ModbusClient +from pymodbus.client import AsyncModbusTcpClient as ModbusClient # --------------------------------------------------------------------------- # # import the various server implementations # --------------------------------------------------------------------------- # from pymodbus.pdu import ModbusExceptions, ModbusRequest, ModbusResponse +from pymodbus.transaction import ModbusSocketFramer # --------------------------------------------------------------------------- # @@ -39,7 +41,7 @@ # --------------------------------------------------------------------------- # -class CustomModbusResponse(ModbusResponse): +class CustomModbusResponse(ModbusResponse): # pragma no cover """Custom modbus response.""" function_code = 55 @@ -87,11 +89,11 @@ def encode(self): """Encode.""" return struct.pack(">HH", self.address, self.count) - def decode(self, data): + def decode(self, data): # pragma no cover """Decode.""" self.address, self.count = struct.unpack(">HH", data) - def execute(self, context): + def execute(self, context): # pragma no cover """Execute.""" if not 1 <= self.count <= 0x7D0: return self.doException(ModbusExceptions.IllegalValue) @@ -125,25 +127,22 @@ def __init__(self, address, **kwargs): # --------------------------------------------------------------------------- # -def run_custom_client(host, port): +async def main(host="localhost", port=5020): """Run versions of read coil.""" - with ModbusClient(host=host, port=port) as client: - # Standard call - request = ReadCoilsRequest(32, 1, slave=1) - result = client.execute(request) - print(result) - - # inherited request - request = Read16CoilsRequest(32, slave=1) - result = client.execute(request) - print(result) + with ModbusClient(host=host, port=port, framer=ModbusSocketFramer) as client: + await client.connect() # new modbus function code. client.register(CustomModbusResponse) request = CustomModbusRequest(32, slave=1) - result = client.execute(request) + result = await client.execute(request) + print(result) + + # inherited request + request = Read16CoilsRequest(32, slave=1) + result = await client.execute(request) print(result) if __name__ == "__main__": - run_custom_client("localhost", "5020") # pragma: no cover + asyncio.run(main()) # pragma: no cover diff --git a/test/sub_examples/test_examples.py b/test/sub_examples/test_examples.py index 25d07f5ac5..4a2afbd869 100755 --- a/test/sub_examples/test_examples.py +++ b/test/sub_examples/test_examples.py @@ -15,7 +15,7 @@ from examples.client_async_calls import main as main_client_async_calls from examples.client_calls import main as main_client_calls from examples.client_calls import template_call -from examples.client_custom_msg import run_custom_client +from examples.client_custom_msg import main as main_custom_client from examples.client_payload import run_payload_calls from examples.datastore_simulator import run_server_simulator, setup_simulator from examples.message_generator import generate_messages @@ -141,10 +141,10 @@ def run_template_call(client): ("tcp", "socket", BASE_PORT + 41), ], ) - def xtest_custom_msg(self, use_port, mock_server): + async def test_custom_msg(self, mock_server, use_port, use_host): """Test client with custom message.""" - _cmdline = mock_server - run_custom_client("localhost", use_port) + assert mock_server + await main_custom_client(port=use_port, host=use_host) @pytest.mark.parametrize( ("use_comm", "use_framer", "use_port"),