Skip to content

Commit

Permalink
Clean client API imports. (#1819)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Oct 31, 2023
1 parent d03d128 commit 25ffa53
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 158 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
63 changes: 26 additions & 37 deletions examples/client_async_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@

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 +50,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 @@ -160,24 +154,24 @@ async def async_handle_input_registers(client):
async def async_execute_information_requests(client):
"""Execute extended information requests."""
_logger.info("### Running information requests.")
rr = await client.execute(req_mei.ReadDeviceInformationRequest(slave=SLAVE))
rr = await client.read_device_information(slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.information[0] == b"Pymodbus"

rr = await client.execute(req_other.ReportSlaveIdRequest(slave=SLAVE))
rr = await client.report_slave_id(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert rr.status
# assert not rr.status

rr = await client.execute(req_other.ReadExceptionStatusRequest(slave=SLAVE))
rr = await client.read_exception_status(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert not rr.status

rr = await client.execute(req_other.GetCommEventCounterRequest(slave=SLAVE))
rr = await client.diag_get_comm_event_counter(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert rr.status
# assert not rr.count

rr = await client.execute(req_other.GetCommEventLogRequest(slave=SLAVE))
rr = await client.diag_get_comm_event_log(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert rr.status
# assert not (rr.event_count + rr.message_count + len(rr.events))
Expand All @@ -187,32 +181,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
59 changes: 26 additions & 33 deletions examples/client_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@

import client_sync

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 @@ -55,14 +49,14 @@ def template_call(client):
"""Show complete modbus call, sync version."""
try:
rr = client.read_coils(32, 1, slave=SLAVE)
except ModbusException as exc:
except client_sync.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_sync.ModbusException(txt)

# Validate data
txt = f"### Template coils response: {rr.bits!s}"
Expand Down Expand Up @@ -159,24 +153,24 @@ def handle_input_registers(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))
rr = client.read_device_information(slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.information[0] == b"Pymodbus"

rr = client.execute(req_other.ReportSlaveIdRequest(slave=SLAVE))
rr = client.report_slave_id(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert rr.status
# assert not rr.status

rr = client.execute(req_other.ReadExceptionStatusRequest(slave=SLAVE))
rr = client.read_exception_status(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert not rr.status

rr = client.execute(req_other.GetCommEventCounterRequest(slave=SLAVE))
rr = client.diag_get_comm_event_counter(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert rr.status
# assert not rr.count

rr = client.execute(req_other.GetCommEventLogRequest(slave=SLAVE))
rr = client.diag_get_comm_event_log(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert rr.status
# assert not (rr.event_count + rr.message_count + len(rr.events))
Expand All @@ -186,28 +180,27 @@ def execute_diagnostic_requests(client): # pragma no cover
"""Execute extended diagnostic requests."""
_logger.info("### Running diagnostic requests.")
message = b"OK"
rr = client.execute(req_diag.ReturnQueryDataRequest(message=message, slave=SLAVE))
rr = client.diag_query_data(msg=message, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.message == message

client.execute(req_diag.RestartCommunicationsOptionRequest(slave=SLAVE))
client.execute(req_diag.ReturnDiagnosticRegisterRequest(slave=SLAVE))
client.execute(req_diag.ChangeAsciiInputDelimiterRequest(slave=SLAVE))

# NOT WORKING: _check_call(client.execute(req_diag.ForceListenOnlyModeRequest(slave=SLAVE)))
# does not send a response

client.execute(req_diag.ClearCountersRequest())
client.execute(req_diag.ReturnBusCommunicationErrorCountRequest(slave=SLAVE))
client.execute(req_diag.ReturnBusExceptionErrorCountRequest(slave=SLAVE))
client.execute(req_diag.ReturnSlaveMessageCountRequest(slave=SLAVE))
client.execute(req_diag.ReturnSlaveNoResponseCountRequest(slave=SLAVE))
client.execute(req_diag.ReturnSlaveNAKCountRequest(slave=SLAVE))
client.execute(req_diag.ReturnSlaveBusyCountRequest(slave=SLAVE))
client.execute(req_diag.ReturnSlaveBusCharacterOverrunCountRequest(slave=SLAVE))
client.execute(req_diag.ReturnIopOverrunCountRequest(slave=SLAVE))
client.execute(req_diag.ClearOverrunCountRequest(slave=SLAVE))
# NOT WORKING _check_call(client.execute(req_diag.GetClearModbusPlusRequest(slave=SLAVE)))
client.diag_restart_communication(True, slave=SLAVE)
client.diag_read_diagnostic_register(slave=SLAVE)
client.diag_change_ascii_input_delimeter(slave=SLAVE)

# NOT WORKING: await client.diag_force_listen_only(slave=SLAVE)

client.diag_clear_counters()
client.diag_read_bus_comm_error_count(slave=SLAVE)
client.diag_read_bus_exception_error_count(slave=SLAVE)
client.diag_read_slave_message_count(slave=SLAVE)
client.diag_read_slave_no_response_count(slave=SLAVE)
client.diag_read_slave_nak_count(slave=SLAVE)
client.diag_read_slave_busy_count(slave=SLAVE)
client.diag_read_bus_char_overrun_count(slave=SLAVE)
client.diag_read_iop_overrun_count(slave=SLAVE)
client.diag_clear_overrun_counter(slave=SLAVE)
# NOT WORKING client.diag_getclear_modbus_response(slave=SLAVE)


# ------------------------
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
Loading

0 comments on commit 25ffa53

Please sign in to comment.