Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/common/async_asyncio_serial_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging

from pymodbus.client.asynchronous.serial import AsyncModbusSerialClient as ModbusClient
from pymodbus.framer.rtu_framer import ModbusRtuFramer

# --------------------------------------------------------------------------- #
# configure the client logging
Expand Down Expand Up @@ -129,7 +130,7 @@ async def start_async_test(client): # pylint: disable=redefined-outer-name
loop, client = ModbusClient( # pylint: disable=unpacking-non-sequence
port="/tmp/ttyp0", # nosec
baudrate=9600,
method="rtu",
framer=ModbusRtuFramer,
)
loop.run_until_complete(start_async_test(client.protocol))
loop.close()
37 changes: 3 additions & 34 deletions pymodbus/client/asynchronous/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
import logging

from pymodbus.client.asynchronous.factory.serial import async_io_factory
from pymodbus.exceptions import ParameterException
from pymodbus.factory import ClientDecoder
from pymodbus.transaction import (
ModbusAsciiFramer,
ModbusBinaryFramer,
ModbusRtuFramer,
ModbusSocketFramer,
)

_logger = logging.getLogger(__name__)

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

@classmethod
def _framer(cls, method):
"""Return the requested framer

:method: The serial framer to instantiate
:returns: The requested serial framer
:raises Exception: Failure
"""
method = method.lower()
if method == "ascii":
return ModbusAsciiFramer(ClientDecoder())
if method == "rtu":
return ModbusRtuFramer(ClientDecoder())
if method == "binary":
return ModbusBinaryFramer(ClientDecoder())
if method == "socket":
return ModbusSocketFramer(ClientDecoder())

raise ParameterException("Invalid framer method requested")

def __new__(cls, method, port, **kwargs):
def __new__(cls, framer, port, **kwargs):
"""Do setup of client.

The methods to connect are::
- ascii
- rtu
- binary
:param method: The method to use for connection
:param framer: Modbus Framer
:param port: The serial port to attach to
:param stopbits: The number of stop bits to use
:param bytesize: The bytesize of the serial messages
Expand All @@ -58,6 +27,6 @@ def __new__(cls, method, port, **kwargs):
:param kwargs:
:return:
"""
framer = cls._framer(method)
framer = framer(ClientDecoder())
yieldable = async_io_factory(framer=framer, port=port, **kwargs)
return yieldable
13 changes: 6 additions & 7 deletions test/test_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,19 @@ def test_udp_asyncio_client(
@patch("asyncio.get_event_loop")
@patch("asyncio.gather", side_effect=mock_asyncio_gather)
@pytest.mark.parametrize(
"method, framer",
"framer",
[
("rtu", ModbusRtuFramer),
("socket", ModbusSocketFramer),
("binary", ModbusBinaryFramer),
("ascii", ModbusAsciiFramer),
ModbusRtuFramer,
ModbusSocketFramer,
ModbusBinaryFramer,
ModbusAsciiFramer,
],
)
@pytest.mark.asyncio
async def test_serial_asyncio_client(
self,
mock_gather, # pylint: disable=unused-argument
mock_event_loop,
method,
framer,
): # pylint: disable=unused-argument
"""Test that AsyncModbusSerialClient instantiates AsyncioModbusSerialClient for asyncio scheduler."""
Expand All @@ -112,7 +111,7 @@ async def test_serial_asyncio_client(
loop,
client,
) = AsyncModbusSerialClient(
method=method,
framer=framer,
port=pytest.SERIAL_PORT,
loop=loop,
baudrate=19200,
Expand Down
13 changes: 6 additions & 7 deletions test/test_client_async2.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,18 @@ def test_udp_asyncio_client(
@patch("asyncio.get_event_loop")
@patch("asyncio.gather", side_effect=mock_asyncio_gather)
@pytest.mark.parametrize(
"method, framer",
"framer",
[
("rtu", ModbusRtuFramer),
("socket", ModbusSocketFramer),
("binary", ModbusBinaryFramer),
("ascii", ModbusAsciiFramer),
ModbusRtuFramer,
ModbusSocketFramer,
ModbusBinaryFramer,
ModbusAsciiFramer,
],
)
def test_serial_asyncio_client(
self,
mock_gather, # pylint: disable=unused-argument
mock_event_loop,
method,
framer,
): # pylint: disable=unused-argument
"""Test that AsyncModbusSerialClient instantiates AsyncioModbusSerialClient for asyncio scheduler."""
Expand All @@ -98,7 +97,7 @@ def test_serial_asyncio_client(
loop,
client,
) = AsyncModbusSerialClient(
method=method,
framer=framer,
port=pytest.SERIAL_PORT,
loop=loop,
baudrate=19200,
Expand Down
4 changes: 2 additions & 2 deletions test/test_client_async_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pymodbus.client.asynchronous.udp import AsyncModbusUDPClient
from pymodbus.exceptions import ConnectionException
from pymodbus.factory import ClientDecoder
from pymodbus.transaction import ModbusSocketFramer
from pymodbus.transaction import ModbusSocketFramer, ModbusRtuFramer

protocols = [
BaseModbusAsyncClientProtocol,
Expand Down Expand Up @@ -126,7 +126,7 @@ async def test_initialization_tls_in_loop(self):
def test_initialization_serial_in_loop(self):
"""Test initialization serial in loop."""
_, client = AsyncModbusSerialClient( # pylint: disable=unpacking-non-sequence
port="/tmp/ptyp0", baudrate=9600, method="rtu" # nosec
port="/tmp/ptyp0", baudrate=9600, framer=ModbusRtuFramer # nosec
)
assert client.port == "/tmp/ptyp0" # nosec
assert client.baudrate == 9600 # nosec
Expand Down
2 changes: 1 addition & 1 deletion test/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# ---------------------------------------------------------------------------#


class SimpleDataStoreTest(unittest.TestCase): # pylint: disable=too-many-public-methods
class SimpleDataStoreTest(unittest.TestCase):
"""Unittest for the pymodbus.device module."""

# -----------------------------------------------------------------------#
Expand Down