From ac852003cc6cdcec70dcb125a5aa8918b3cce796 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sat, 14 Oct 2023 21:06:37 +0200 Subject: [PATCH] Clean client API imports. --- examples/client_async.py | 27 ++++++---------- examples/client_async_calls.py | 48 ++++++++++++---------------- examples/client_calls.py | 1 - examples/client_custom_msg.py | 8 ----- examples/client_payload.py | 3 -- examples/client_sync.py | 38 ++++++++++------------ examples/contrib/serial_forwarder.py | 1 - examples/contrib/solar.py | 1 - examples/datastore_simulator.py | 1 - examples/message_generator.py | 1 - examples/message_parser.py | 1 - examples/modbus_forwarder.py | 1 - examples/server_async.py | 1 - examples/simulator.py | 1 - pymodbus/__init__.py | 2 ++ pymodbus/client/__init__.py | 2 -- test/test_framers.py | 2 +- 17 files changed, 48 insertions(+), 91 deletions(-) diff --git a/examples/client_async.py b/examples/client_async.py index d676194fe0..6e5bd04908 100755 --- a/examples/client_async.py +++ b/examples/client_async.py @@ -31,19 +31,10 @@ import helper -# --------------------------------------------------------------------------- # -# import the various client implementations -# --------------------------------------------------------------------------- # -from pymodbus.client import ( - AsyncModbusSerialClient, - AsyncModbusTcpClient, - AsyncModbusTlsClient, - AsyncModbusUdpClient, -) -from pymodbus.exceptions import ModbusIOException - - -logging.basicConfig() +import pymodbus.client as modbusClient +from pymodbus import ModbusException + + _logger = logging.getLogger(__file__) _logger.setLevel("DEBUG") @@ -55,7 +46,7 @@ def setup_async_client(description=None, cmdline=None): ) _logger.info("### Create client object") if args.comm == "tcp": - client = AsyncModbusTcpClient( + client = modbusClient.AsyncModbusTcpClient( args.host, port=args.port, # on which port # Common optional parameters: @@ -69,7 +60,7 @@ def setup_async_client(description=None, cmdline=None): # source_address=("localhost", 0), ) elif args.comm == "udp": - client = AsyncModbusUdpClient( + client = modbusClient.AsyncModbusUdpClient( args.host, port=args.port, # Common optional parameters: @@ -81,7 +72,7 @@ def setup_async_client(description=None, cmdline=None): # source_address=None, ) elif args.comm == "serial": - client = AsyncModbusSerialClient( + client = modbusClient.AsyncModbusSerialClient( args.port, # Common optional parameters: # framer=ModbusRtuFramer, @@ -96,7 +87,7 @@ def setup_async_client(description=None, cmdline=None): # handle_local_echo=False, ) elif args.comm == "tls": - client = AsyncModbusTlsClient( + client = modbusClient.AsyncModbusTlsClient( args.host, port=args.port, # Common optional parameters: @@ -135,7 +126,7 @@ async def run_a_few_calls(client): rr = await client.read_holding_registers(4, 2, slave=1) assert rr.registers[0] == 17 assert rr.registers[1] == 17 - except ModbusIOException: + except ModbusException: pass diff --git a/examples/client_async_calls.py b/examples/client_async_calls.py index 11a754f34f..250cc7dce1 100755 --- a/examples/client_async_calls.py +++ b/examples/client_async_calls.py @@ -34,13 +34,10 @@ import client_async -import pymodbus.diag_message as req_diag import pymodbus.mei_message as req_mei import pymodbus.other_message as req_other -from pymodbus.exceptions import ModbusException -logging.basicConfig() _logger = logging.getLogger(__file__) _logger.setLevel("DEBUG") @@ -56,14 +53,14 @@ async def async_template_call(client): """Show complete modbus call, async version.""" try: rr = await client.read_coils(1, 1, slave=SLAVE) - except ModbusException as exc: + except client_async.ModbusException as exc: txt = f"ERROR: exception in pymodbus {exc}" _logger.error(txt) raise exc if rr.isError(): txt = "ERROR: pymodbus returned an error!" _logger.error(txt) - raise ModbusException(txt) + raise client_async.ModbusException(txt) # Validate data txt = f"### Template coils response: {rr.bits!s}" @@ -187,32 +184,27 @@ async def async_execute_diagnostic_requests(client): """Execute extended diagnostic requests.""" _logger.info("### Running diagnostic requests.") message = b"OK" - rr = await client.execute( - req_diag.ReturnQueryDataRequest(message=message, slave=SLAVE) - ) + rr = await client.diag_query_data(msg=message, slave=SLAVE) assert not rr.isError() # test that call was OK assert rr.message == message - await client.execute(req_diag.RestartCommunicationsOptionRequest(slave=SLAVE)) - await client.execute(req_diag.ReturnDiagnosticRegisterRequest(slave=SLAVE)) - await client.execute(req_diag.ChangeAsciiInputDelimiterRequest(slave=SLAVE)) - - # NOT WORKING: _check_call(await client.execute(req_diag.ForceListenOnlyModeRequest(slave=SLAVE))) - # does not send a response - - await client.execute(req_diag.ClearCountersRequest()) - await client.execute(req_diag.ReturnBusCommunicationErrorCountRequest(slave=SLAVE)) - await client.execute(req_diag.ReturnBusExceptionErrorCountRequest(slave=SLAVE)) - await client.execute(req_diag.ReturnSlaveMessageCountRequest(slave=SLAVE)) - await client.execute(req_diag.ReturnSlaveNoResponseCountRequest(slave=SLAVE)) - await client.execute(req_diag.ReturnSlaveNAKCountRequest(slave=SLAVE)) - await client.execute(req_diag.ReturnSlaveBusyCountRequest(slave=SLAVE)) - await client.execute( - req_diag.ReturnSlaveBusCharacterOverrunCountRequest(slave=SLAVE) - ) - await client.execute(req_diag.ReturnIopOverrunCountRequest(slave=SLAVE)) - await client.execute(req_diag.ClearOverrunCountRequest(slave=SLAVE)) - # NOT WORKING _check_call(await client.execute(req_diag.GetClearModbusPlusRequest(slave=SLAVE))) + await client.diag_restart_communication(True, slave=SLAVE) + await client.diag_read_diagnostic_register(slave=SLAVE) + await client.diag_change_ascii_input_delimeter(slave=SLAVE) + + # NOT WORKING: _await client.diag_force_listen_only(slave=SLAVE) + + await client.diag_clear_counters() + await client.diag_read_bus_comm_error_count(slave=SLAVE) + await client.diag_read_bus_exception_error_count(slave=SLAVE) + await client.diag_read_slave_message_count(slave=SLAVE) + await client.diag_read_slave_no_response_count(slave=SLAVE) + await client.diag_read_slave_nak_count(slave=SLAVE) + await client.diag_read_slave_busy_count(slave=SLAVE) + await client.diag_read_bus_char_overrun_count(slave=SLAVE) + await client.diag_read_iop_overrun_count(slave=SLAVE) + await client.diag_clear_overrun_counter(slave=SLAVE) + # NOT WORKING await client.diag_getclear_modbus_response(slave=SLAVE) # ------------------------ diff --git a/examples/client_calls.py b/examples/client_calls.py index 09889192f1..ddaff9bc44 100755 --- a/examples/client_calls.py +++ b/examples/client_calls.py @@ -39,7 +39,6 @@ from pymodbus.exceptions import ModbusException -logging.basicConfig() _logger = logging.getLogger(__file__) _logger.setLevel("DEBUG") diff --git a/examples/client_custom_msg.py b/examples/client_custom_msg.py index 0ce4dbdf45..2b8dbc99ec 100755 --- a/examples/client_custom_msg.py +++ b/examples/client_custom_msg.py @@ -11,7 +11,6 @@ """ import asyncio -import logging import struct from pymodbus import Framer @@ -20,13 +19,6 @@ from pymodbus.pdu import ModbusExceptions, ModbusRequest, ModbusResponse -# --------------------------------------------------------------------------- # -# configure the client logging -# --------------------------------------------------------------------------- # -logging.basicConfig() -log = logging.getLogger(__file__) -log.setLevel(logging.DEBUG) - # --------------------------------------------------------------------------- # # create your custom message # --------------------------------------------------------------------------- # diff --git a/examples/client_payload.py b/examples/client_payload.py index a4ffdf4522..437aff07ef 100755 --- a/examples/client_payload.py +++ b/examples/client_payload.py @@ -7,7 +7,6 @@ Works out of the box together with payload_server.py """ import asyncio -import logging from collections import OrderedDict import client_async @@ -16,8 +15,6 @@ from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder -logging.basicConfig() -_logger = logging.getLogger(__file__) ORDER_DICT = {"<": "LITTLE", ">": "BIG"} diff --git a/examples/client_sync.py b/examples/client_sync.py index abce01bc66..fd3c20fbb7 100755 --- a/examples/client_sync.py +++ b/examples/client_sync.py @@ -34,18 +34,10 @@ import helper -# --------------------------------------------------------------------------- # -# import the various client implementations -# --------------------------------------------------------------------------- # -from pymodbus.client import ( - ModbusSerialClient, - ModbusTcpClient, - ModbusTlsClient, - ModbusUdpClient, -) - - -logging.basicConfig() +import pymodbus.client as modbusClient +from pymodbus import ModbusException + + _logger = logging.getLogger(__file__) _logger.setLevel("DEBUG") @@ -59,7 +51,7 @@ def setup_sync_client(description=None, cmdline=None): ) _logger.info("### Create client object") if args.comm == "tcp": - client = ModbusTcpClient( + client = modbusClient.ModbusTcpClient( args.host, port=args.port, # Common optional parameters: @@ -73,7 +65,7 @@ def setup_sync_client(description=None, cmdline=None): # source_address=("localhost", 0), ) elif args.comm == "udp": - client = ModbusUdpClient( + client = modbusClient.ModbusUdpClient( args.host, port=args.port, # Common optional parameters: @@ -87,7 +79,7 @@ def setup_sync_client(description=None, cmdline=None): # source_address=None, ) elif args.comm == "serial": - client = ModbusSerialClient( + client = modbusClient.ModbusSerialClient( port=args.port, # serial port # Common optional parameters: # framer=ModbusRtuFramer, @@ -104,7 +96,7 @@ def setup_sync_client(description=None, cmdline=None): # handle_local_echo=False, ) elif args.comm == "tls": # pragma no cover - client = ModbusTlsClient( + client = modbusClient.ModbusTlsClient( args.host, port=args.port, # Common optional parameters: @@ -136,12 +128,14 @@ def run_sync_client(client, modbus_calls=None): def run_a_few_calls(client): """Test connection works.""" - rr = client.read_coils(32, 1, slave=1) - assert len(rr.bits) == 8 - rr = client.read_holding_registers(4, 2, slave=1) - assert rr.registers[0] == 17 - assert rr.registers[1] == 17 - + try: + rr = client.read_coils(32, 1, slave=1) + assert len(rr.bits) == 8 + rr = client.read_holding_registers(4, 2, slave=1) + assert rr.registers[0] == 17 + assert rr.registers[1] == 17 + except ModbusException as exc: + raise exc def main(cmdline=None): """Combine setup and run.""" diff --git a/examples/contrib/serial_forwarder.py b/examples/contrib/serial_forwarder.py index 259e4f2a6c..da2b153b93 100644 --- a/examples/contrib/serial_forwarder.py +++ b/examples/contrib/serial_forwarder.py @@ -14,7 +14,6 @@ from pymodbus.server.async_io import ModbusTcpServer -logging.basicConfig() _logger = logging.getLogger(__file__) diff --git a/examples/contrib/solar.py b/examples/contrib/solar.py index 7759737e6c..da64662ba2 100755 --- a/examples/contrib/solar.py +++ b/examples/contrib/solar.py @@ -17,7 +17,6 @@ from pymodbus.transaction import ModbusSocketFramer -logging.basicConfig() _logger = logging.getLogger(__file__) _logger.setLevel(logging.DEBUG) diff --git a/examples/datastore_simulator.py b/examples/datastore_simulator.py index e681c8a0ba..6496e4113b 100755 --- a/examples/datastore_simulator.py +++ b/examples/datastore_simulator.py @@ -31,7 +31,6 @@ from pymodbus.server import StartAsyncTcpServer -logging.basicConfig() _logger = logging.getLogger(__file__) demo_config = { diff --git a/examples/message_generator.py b/examples/message_generator.py index e381a7290b..8ceda47bd2 100755 --- a/examples/message_generator.py +++ b/examples/message_generator.py @@ -20,7 +20,6 @@ ) -logging.basicConfig() _logger = logging.getLogger(__file__) diff --git a/examples/message_parser.py b/examples/message_parser.py index 5b2537cb24..c3e733eda2 100755 --- a/examples/message_parser.py +++ b/examples/message_parser.py @@ -20,7 +20,6 @@ ) -logging.basicConfig() _logger = logging.getLogger(__file__) diff --git a/examples/modbus_forwarder.py b/examples/modbus_forwarder.py index f65fcaa2c9..17e50c5280 100755 --- a/examples/modbus_forwarder.py +++ b/examples/modbus_forwarder.py @@ -28,7 +28,6 @@ from pymodbus.server import StartAsyncTcpServer -logging.basicConfig() _logger = logging.getLogger(__file__) diff --git a/examples/server_async.py b/examples/server_async.py index 0df1185fd2..1220aa50e8 100755 --- a/examples/server_async.py +++ b/examples/server_async.py @@ -53,7 +53,6 @@ ) -logging.basicConfig() _logger = logging.getLogger(__file__) _logger.setLevel(logging.INFO) diff --git a/examples/simulator.py b/examples/simulator.py index 8897526b8f..db76461104 100755 --- a/examples/simulator.py +++ b/examples/simulator.py @@ -16,7 +16,6 @@ from pymodbus.server import ModbusSimulatorServer, get_simulator_commandline -logging.basicConfig() _logger = logging.getLogger(__file__) diff --git a/pymodbus/__init__.py b/pymodbus/__init__.py index f52caa7bd0..978798bb41 100644 --- a/pymodbus/__init__.py +++ b/pymodbus/__init__.py @@ -5,11 +5,13 @@ __all__ = [ "Framer", + "ModbusException", "pymodbus_apply_logging_config", "__version__", "__version_full__", ] +from pymodbus.exceptions import ModbusException from pymodbus.framer import Framer from pymodbus.logging import pymodbus_apply_logging_config diff --git a/pymodbus/client/__init__.py b/pymodbus/client/__init__.py index 245bcdf0d9..6303ab7d5c 100644 --- a/pymodbus/client/__init__.py +++ b/pymodbus/client/__init__.py @@ -5,14 +5,12 @@ "AsyncModbusTcpClient", "AsyncModbusTlsClient", "AsyncModbusUdpClient", - "ModbusBaseClient", "ModbusSerialClient", "ModbusTcpClient", "ModbusTlsClient", "ModbusUdpClient", ] -from pymodbus.client.base import ModbusBaseClient from pymodbus.client.serial import AsyncModbusSerialClient, ModbusSerialClient from pymodbus.client.tcp import AsyncModbusTcpClient, ModbusTcpClient from pymodbus.client.tls import AsyncModbusTlsClient, ModbusTlsClient diff --git a/test/test_framers.py b/test/test_framers.py index e9a450ff48..ed5601927c 100644 --- a/test/test_framers.py +++ b/test/test_framers.py @@ -5,7 +5,7 @@ from pymodbus import Framer from pymodbus.bit_read_message import ReadCoilsRequest -from pymodbus.client import ModbusBaseClient +from pymodbus.client.base import ModbusBaseClient from pymodbus.exceptions import ModbusIOException from pymodbus.factory import ClientDecoder from pymodbus.framer.ascii_framer import ModbusAsciiFramer