Skip to content

Commit

Permalink
Clean client API imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Oct 31, 2023
1 parent d03d128 commit ac85200
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 91 deletions.
27 changes: 9 additions & 18 deletions examples/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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


Expand Down
48 changes: 20 additions & 28 deletions examples/client_async_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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}"
Expand Down Expand Up @@ -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)


# ------------------------
Expand Down
1 change: 0 additions & 1 deletion examples/client_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
from pymodbus.exceptions import ModbusException


logging.basicConfig()
_logger = logging.getLogger(__file__)
_logger.setLevel("DEBUG")

Expand Down
8 changes: 0 additions & 8 deletions examples/client_custom_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"""
import asyncio
import logging
import struct

from pymodbus import Framer
Expand All @@ -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
# --------------------------------------------------------------------------- #
Expand Down
3 changes: 0 additions & 3 deletions examples/client_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,8 +15,6 @@
from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder


logging.basicConfig()
_logger = logging.getLogger(__file__)
ORDER_DICT = {"<": "LITTLE", ">": "BIG"}


Expand Down
38 changes: 16 additions & 22 deletions examples/client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down
1 change: 0 additions & 1 deletion examples/contrib/serial_forwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from pymodbus.server.async_io import ModbusTcpServer


logging.basicConfig()
_logger = logging.getLogger(__file__)


Expand Down
1 change: 0 additions & 1 deletion examples/contrib/solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from pymodbus.transaction import ModbusSocketFramer


logging.basicConfig()
_logger = logging.getLogger(__file__)
_logger.setLevel(logging.DEBUG)

Expand Down
1 change: 0 additions & 1 deletion examples/datastore_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from pymodbus.server import StartAsyncTcpServer


logging.basicConfig()
_logger = logging.getLogger(__file__)

demo_config = {
Expand Down
1 change: 0 additions & 1 deletion examples/message_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
)


logging.basicConfig()
_logger = logging.getLogger(__file__)


Expand Down
1 change: 0 additions & 1 deletion examples/message_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
)


logging.basicConfig()
_logger = logging.getLogger(__file__)


Expand Down
1 change: 0 additions & 1 deletion examples/modbus_forwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from pymodbus.server import StartAsyncTcpServer


logging.basicConfig()
_logger = logging.getLogger(__file__)


Expand Down
1 change: 0 additions & 1 deletion examples/server_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
)


logging.basicConfig()
_logger = logging.getLogger(__file__)
_logger.setLevel(logging.INFO)

Expand Down
1 change: 0 additions & 1 deletion examples/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from pymodbus.server import ModbusSimulatorServer, get_simulator_commandline


logging.basicConfig()
_logger = logging.getLogger(__file__)


Expand Down
2 changes: 2 additions & 0 deletions pymodbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions pymodbus/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_framers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ac85200

Please sign in to comment.