Skip to content

Commit f5c226b

Browse files
authored
Merge pull request #325 from riptideio/#254-Backward-Comaptibility
#254 backward compatibility for asyncio clients with twisted.
2 parents 30703dd + bc6d821 commit f5c226b

File tree

10 files changed

+364
-89
lines changed

10 files changed

+364
-89
lines changed

examples/common/async_twisted_client.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,7 @@
2020
# choose the requested modbus protocol
2121
# --------------------------------------------------------------------------- #
2222

23-
#from pymodbus.client.async import ModbusUdpClientProtocol
24-
2523
from twisted.internet import reactor, protocol
26-
from pymodbus.constants import Defaults
27-
28-
# --------------------------------------------------------------------------- #
29-
# choose the requested modbus protocol
30-
# --------------------------------------------------------------------------- #
3124

3225
# --------------------------------------------------------------------------- #
3326
# configure the client logging
@@ -65,6 +58,7 @@ def _assertor(value):
6558

6659
UNIT = 0x01
6760

61+
6862
def processResponse(result):
6963
log.debug(result)
7064

@@ -91,6 +85,7 @@ def exampleRequests(client):
9185
# deferred assert helper(dassert).
9286
# --------------------------------------------------------------------------- #
9387

88+
9489
def stopAsynchronousTest(client):
9590
# ----------------------------------------------------------------------- #
9691
# close the client at some time later

examples/contrib/modbus_scraper.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
from pymodbus.datastore import ModbusSequentialDataBlock
1212
from pymodbus.datastore import ModbusSlaveContext
1313
from pymodbus.factory import ClientDecoder
14-
from pymodbus.client.async import ModbusClientProtocol
14+
from pymodbus.client.async.twisted import ModbusClientProtocol
1515

16-
#--------------------------------------------------------------------------#
16+
# -------------------------------------------------------------------------- #
1717
# Configure the client logging
18-
#--------------------------------------------------------------------------#
18+
# -------------------------------------------------------------------------- #
1919
import logging
2020
log = logging.getLogger("pymodbus")
2121

@@ -32,14 +32,16 @@
3232
# --------------------------------------------------------------------------- #
3333
COUNT = 8 # The number of bits/registers to read at once
3434
DELAY = 0 # The delay between subsequent reads
35-
SLAVE = 0x01 # The slave unit id to read from
35+
SLAVE = 0x01 # The slave unit id to read from
3636

3737
# --------------------------------------------------------------------------- #
3838
# A simple scraper protocol
3939
# --------------------------------------------------------------------------- #
4040
# I tried to spread the load across the device, but feel free to modify the
4141
# logic to suit your own purpose.
42-
# --------------------------------------------------------------------------- #
42+
# --------------------------------------------------------------------------- #
43+
44+
4345
class ScraperProtocol(ModbusClientProtocol):
4446

4547
address = None
@@ -114,7 +116,8 @@ def start_next_cycle(self, response):
114116
if self.address >= self.factory.ending:
115117
self.endpoint.finalize()
116118
self.transport.loseConnection()
117-
else: reactor.callLater(DELAY, self.scrape_holding_registers)
119+
else:
120+
reactor.callLater(DELAY, self.scrape_holding_registers)
118121

119122
def error_handler(self, failure):
120123
""" Handle any twisted errors

pymodbus/client/async/__init__.py

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -31,75 +31,4 @@
3131
# For asyncio the actual client is returned and event loop is asyncio loop
3232
3333
"""
34-
from __future__ import unicode_literals
35-
from __future__ import absolute_import
36-
37-
import logging
38-
39-
from pymodbus.client.sync import BaseModbusClient
40-
41-
from pymodbus.constants import Defaults
42-
43-
from pymodbus.factory import ClientDecoder
44-
from pymodbus.transaction import ModbusSocketFramer
45-
46-
47-
LOGGER = logging.getLogger(__name__)
48-
49-
50-
class BaseAsyncModbusClient(BaseModbusClient):
51-
"""
52-
This represents the base ModbusAsyncClient.
53-
"""
54-
55-
def __init__(self, framer=None, **kwargs):
56-
""" Initializes the framer module
57-
58-
:param framer: The framer to use for the protocol. Default:
59-
ModbusSocketFramer
60-
:type framer: pymodbus.transaction.ModbusSocketFramer
61-
"""
62-
self._connected = False
63-
64-
super(BaseAsyncModbusClient, self).__init__(
65-
framer or ModbusSocketFramer(ClientDecoder()), **kwargs
66-
)
67-
68-
69-
class AsyncModbusClientMixin(BaseAsyncModbusClient):
70-
"""
71-
Async Modbus client mixing for UDP and TCP clients
72-
"""
73-
def __init__(self, host="127.0.0.1", port=Defaults.Port, framer=None,
74-
source_address=None, timeout=None, **kwargs):
75-
"""
76-
Initializes a Modbus TCP/UDP async client
77-
:param host: Host IP address
78-
:param port: Port
79-
:param framer: Framer to use
80-
:param source_address: Specific to underlying client being used
81-
:param timeout: Timeout in seconds
82-
:param kwargs: Extra arguments
83-
"""
84-
super(AsyncModbusClientMixin, self).__init__(framer=framer, **kwargs)
85-
self.host = host
86-
self.port = port
87-
self.source_address = source_address or ("", 0)
88-
self.timeout = timeout if timeout is not None else Defaults.Timeout
89-
90-
91-
class AsyncModbusSerialClientMixin(BaseAsyncModbusClient):
92-
"""
93-
Async Modbus Serial Client Mixing
94-
"""
95-
def __init__(self, framer=None, port=None, **kwargs):
96-
"""
97-
Initializes a Async Modbus Serial Client
98-
:param framer: Modbus Framer
99-
:param port: Serial port to use
100-
:param kwargs: Extra arguments if any
101-
"""
102-
super(AsyncModbusSerialClientMixin, self).__init__(framer=framer)
103-
self.port = port
104-
self.serial_settings = kwargs
105-
34+
from pymodbus.client.async.deprecated.async import *

pymodbus/client/async/asyncio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import asyncio
66
import functools
77
from pymodbus.exceptions import ConnectionException
8-
from pymodbus.client.async import AsyncModbusClientMixin
8+
from pymodbus.client.async.mixins import AsyncModbusClientMixin
99
from pymodbus.compat import byte2int
1010
import logging
1111

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import warnings
2+
warnings.simplefilter('always', DeprecationWarning)
3+
4+
WARNING = """
5+
Usage of '{}' is deprecated from 2.0.0 and will be removed in future releases.
6+
Use the new Async Modbus Client implementation based on Twisted, tornado
7+
and asyncio
8+
------------------------------------------------------------------------
9+
10+
Example run::
11+
12+
from pymodbus.client.async import schedulers
13+
14+
# Import The clients
15+
16+
from pymodbus.client.async.tcp import AsyncModbusTCPClient as Client
17+
from pymodbus.client.async.serial import AsyncModbusSerialClient as Client
18+
from pymodbus.client.async.udp import AsyncModbusUDPClient as Client
19+
20+
# For tornado based async client use
21+
event_loop, future = Client(schedulers.IO_LOOP, port=5020)
22+
23+
# For twisted based async client use
24+
event_loop, deferred = Client(schedulers.REACTOR, port=5020)
25+
26+
# For asyncio based async client use
27+
event_loop, client = Client(schedulers.ASYNC_IO, port=5020)
28+
29+
# Here event_loop is a thread which would control the backend and future is
30+
# a Future/deffered object which would be used to
31+
# add call backs to run asynchronously.
32+
33+
# The Actual client could be accessed with future.result() with Tornado
34+
# and future.result when using twisted
35+
36+
# For asyncio the actual client is returned and event loop is asyncio loop
37+
38+
Refer:
39+
https://pymodbus.readthedocs.io/en/dev/source/example/async_twisted_client.html
40+
https://pymodbus.readthedocs.io/en/dev/source/example/async_tornado_client.html
41+
https://pymodbus.readthedocs.io/en/dev/source/example/async_asyncio_client.html
42+
43+
"""
44+
45+
46+
def deprecated(name): # pragma: no cover
47+
warnings.warn(WARNING.format(name), DeprecationWarning)

0 commit comments

Comments
 (0)