Skip to content

Commit 6c2e88a

Browse files
authored
Change async ModbusSerialClient to framer= from method=. (#994)
1 parent 53577ed commit 6c2e88a

File tree

5 files changed

+19
-51
lines changed

5 files changed

+19
-51
lines changed

examples/common/async_asyncio_serial_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import logging
1111

1212
from pymodbus.client.asynchronous.serial import AsyncModbusSerialClient as ModbusClient
13+
from pymodbus.framer.rtu_framer import ModbusRtuFramer
1314

1415
# --------------------------------------------------------------------------- #
1516
# configure the client logging
@@ -129,7 +130,7 @@ async def start_async_test(client): # pylint: disable=redefined-outer-name
129130
loop, client = ModbusClient( # pylint: disable=unpacking-non-sequence
130131
port="/tmp/ttyp0", # nosec
131132
baudrate=9600,
132-
method="rtu",
133+
framer=ModbusRtuFramer,
133134
)
134135
loop.run_until_complete(start_async_test(client.protocol))
135136
loop.close()

pymodbus/client/asynchronous/serial.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22
import logging
33

44
from pymodbus.client.asynchronous.factory.serial import async_io_factory
5-
from pymodbus.exceptions import ParameterException
65
from pymodbus.factory import ClientDecoder
7-
from pymodbus.transaction import (
8-
ModbusAsciiFramer,
9-
ModbusBinaryFramer,
10-
ModbusRtuFramer,
11-
ModbusSocketFramer,
12-
)
136

147
_logger = logging.getLogger(__name__)
158

@@ -21,34 +14,10 @@ class AsyncModbusSerialClient: # pylint: disable=too-few-public-methods
2114
from pymodbus.client.asynchronous.serial import AsyncModbusSerialClient
2215
"""
2316

24-
@classmethod
25-
def _framer(cls, method):
26-
"""Return the requested framer
27-
28-
:method: The serial framer to instantiate
29-
:returns: The requested serial framer
30-
:raises Exception: Failure
31-
"""
32-
method = method.lower()
33-
if method == "ascii":
34-
return ModbusAsciiFramer(ClientDecoder())
35-
if method == "rtu":
36-
return ModbusRtuFramer(ClientDecoder())
37-
if method == "binary":
38-
return ModbusBinaryFramer(ClientDecoder())
39-
if method == "socket":
40-
return ModbusSocketFramer(ClientDecoder())
41-
42-
raise ParameterException("Invalid framer method requested")
43-
44-
def __new__(cls, method, port, **kwargs):
17+
def __new__(cls, framer, port, **kwargs):
4518
"""Do setup of client.
4619
47-
The methods to connect are::
48-
- ascii
49-
- rtu
50-
- binary
51-
:param method: The method to use for connection
20+
:param framer: Modbus Framer
5221
:param port: The serial port to attach to
5322
:param stopbits: The number of stop bits to use
5423
:param bytesize: The bytesize of the serial messages
@@ -58,6 +27,6 @@ def __new__(cls, method, port, **kwargs):
5827
:param kwargs:
5928
:return:
6029
"""
61-
framer = cls._framer(method)
30+
framer = framer(ClientDecoder())
6231
yieldable = async_io_factory(framer=framer, port=port, **kwargs)
6332
return yieldable

test/test_client_async.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,19 @@ def test_udp_asyncio_client(
8989
@patch("asyncio.get_event_loop")
9090
@patch("asyncio.gather", side_effect=mock_asyncio_gather)
9191
@pytest.mark.parametrize(
92-
"method, framer",
92+
"framer",
9393
[
94-
("rtu", ModbusRtuFramer),
95-
("socket", ModbusSocketFramer),
96-
("binary", ModbusBinaryFramer),
97-
("ascii", ModbusAsciiFramer),
94+
ModbusRtuFramer,
95+
ModbusSocketFramer,
96+
ModbusBinaryFramer,
97+
ModbusAsciiFramer,
9898
],
9999
)
100100
@pytest.mark.asyncio
101101
async def test_serial_asyncio_client(
102102
self,
103103
mock_gather, # pylint: disable=unused-argument
104104
mock_event_loop,
105-
method,
106105
framer,
107106
): # pylint: disable=unused-argument
108107
"""Test that AsyncModbusSerialClient instantiates AsyncioModbusSerialClient for asyncio scheduler."""
@@ -112,7 +111,7 @@ async def test_serial_asyncio_client(
112111
loop,
113112
client,
114113
) = AsyncModbusSerialClient(
115-
method=method,
114+
framer=framer,
116115
port=pytest.SERIAL_PORT,
117116
loop=loop,
118117
baudrate=19200,

test/test_client_async2.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,18 @@ def test_udp_asyncio_client(
7676
@patch("asyncio.get_event_loop")
7777
@patch("asyncio.gather", side_effect=mock_asyncio_gather)
7878
@pytest.mark.parametrize(
79-
"method, framer",
79+
"framer",
8080
[
81-
("rtu", ModbusRtuFramer),
82-
("socket", ModbusSocketFramer),
83-
("binary", ModbusBinaryFramer),
84-
("ascii", ModbusAsciiFramer),
81+
ModbusRtuFramer,
82+
ModbusSocketFramer,
83+
ModbusBinaryFramer,
84+
ModbusAsciiFramer,
8585
],
8686
)
8787
def test_serial_asyncio_client(
8888
self,
8989
mock_gather, # pylint: disable=unused-argument
9090
mock_event_loop,
91-
method,
9291
framer,
9392
): # pylint: disable=unused-argument
9493
"""Test that AsyncModbusSerialClient instantiates AsyncioModbusSerialClient for asyncio scheduler."""
@@ -98,7 +97,7 @@ def test_serial_asyncio_client(
9897
loop,
9998
client,
10099
) = AsyncModbusSerialClient(
101-
method=method,
100+
framer=framer,
102101
port=pytest.SERIAL_PORT,
103102
loop=loop,
104103
baudrate=19200,

test/test_client_async_asyncio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from pymodbus.client.asynchronous.udp import AsyncModbusUDPClient
2121
from pymodbus.exceptions import ConnectionException
2222
from pymodbus.factory import ClientDecoder
23-
from pymodbus.transaction import ModbusSocketFramer
23+
from pymodbus.transaction import ModbusSocketFramer, ModbusRtuFramer
2424

2525
protocols = [
2626
BaseModbusAsyncClientProtocol,
@@ -126,7 +126,7 @@ async def test_initialization_tls_in_loop(self):
126126
def test_initialization_serial_in_loop(self):
127127
"""Test initialization serial in loop."""
128128
_, client = AsyncModbusSerialClient( # pylint: disable=unpacking-non-sequence
129-
port="/tmp/ptyp0", baudrate=9600, method="rtu" # nosec
129+
port="/tmp/ptyp0", baudrate=9600, framer=ModbusRtuFramer # nosec
130130
)
131131
assert client.port == "/tmp/ptyp0" # nosec
132132
assert client.baudrate == 9600 # nosec

0 commit comments

Comments
 (0)