Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check client and frametype. #2426

Merged
merged 1 commit into from
Oct 29, 2024
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
4 changes: 4 additions & 0 deletions pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def __init__( # pylint: disable=too-many-arguments
"Serial client requires pyserial "
'Please install with "pip install pyserial" and try again.'
)
if framer not in [FramerType.ASCII, FramerType.RTU]:
raise TypeError("Only FramerType RTU/ASCII allowed.")
self.comm_params = CommParams(
comm_type=CommType.SERIAL,
host=port,
Expand Down Expand Up @@ -163,6 +165,8 @@ def __init__( # pylint: disable=too-many-arguments
retries: int = 3,
) -> None:
"""Initialize Modbus Serial Client."""
if framer not in [FramerType.ASCII, FramerType.RTU]:
raise TypeError("Only RTU/ASCII allowed.")
self.comm_params = CommParams(
comm_type=CommType.SERIAL,
host=port,
Expand Down
4 changes: 4 additions & 0 deletions pymodbus/client/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def __init__( # pylint: disable=too-many-arguments
) -> None:
"""Initialize Asyncio Modbus TCP Client."""
if not hasattr(self,"comm_params"):
if framer not in [FramerType.SOCKET, FramerType.RTU, FramerType.ASCII]:
raise TypeError("Only FramerType SOCKET/RTU/ASCII allowed.")
self.comm_params = CommParams(
comm_type=CommType.TCP,
host=host,
Expand Down Expand Up @@ -138,6 +140,8 @@ def __init__(
) -> None:
"""Initialize Modbus TCP Client."""
if not hasattr(self,"comm_params"):
if framer not in [FramerType.SOCKET, FramerType.RTU, FramerType.ASCII]:
raise TypeError("Only FramerType SOCKET/RTU/ASCII allowed.")
self.comm_params = CommParams(
comm_type=CommType.TCP,
host=host,
Expand Down
4 changes: 4 additions & 0 deletions pymodbus/client/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def __init__( # pylint: disable=too-many-arguments
on_connect_callback: Callable[[bool], None] | None = None,
):
"""Initialize Asyncio Modbus TLS Client."""
if framer not in [FramerType.TLS]:
raise TypeError("Only FramerType TLS allowed.")
self.comm_params = CommParams(
comm_type=CommType.TLS,
host=host,
Expand Down Expand Up @@ -159,6 +161,8 @@ def __init__( # pylint: disable=too-many-arguments
retries: int = 3,
):
"""Initialize Modbus TLS Client."""
if framer not in [FramerType.TLS]:
raise TypeError("Only FramerType TLS allowed.")
self.comm_params = CommParams(
comm_type=CommType.TLS,
host=host,
Expand Down
4 changes: 4 additions & 0 deletions pymodbus/client/udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def __init__( # pylint: disable=too-many-arguments
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
if framer not in [FramerType.SOCKET, FramerType.RTU, FramerType.ASCII]:
raise TypeError("Only FramerType SOCKET/RTU/ASCII allowed.")
ModbusBaseClient.__init__(
self,
framer,
Expand Down Expand Up @@ -139,6 +141,8 @@ def __init__(
retries: int = 3,
) -> None:
"""Initialize Modbus UDP Client."""
if framer not in [FramerType.SOCKET, FramerType.RTU, FramerType.ASCII]:
raise TypeError("Only FramerType SOCKET/RTU/ASCII allowed.")
self.comm_params = CommParams(
comm_type=CommType.UDP,
host=host,
Expand Down
6 changes: 3 additions & 3 deletions test/sub_client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ def fake_execute(_self, _no_response_expected, request):
"tls": {
"pos_arg": "192.168.1.2",
"opt_args": {
"port": 211,
"framer": FramerType.ASCII,
"source_address": ("195.6.7.8", 1025),
"port": 802,
"framer": FramerType.TLS,
"source_address": None,
"sslctx": None,
},
"defaults": {
Expand Down
5 changes: 0 additions & 5 deletions test/sub_client/test_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from pymodbus.framer import (
FramerAscii,
FramerRTU,
FramerSocket,
FramerTLS,
)
from test.conftest import mockSocket
Expand Down Expand Up @@ -313,10 +312,6 @@ def test_sync_serial_client_instantiation(self):
ModbusSerialClient("/dev/null", framer=FramerType.RTU).framer,
FramerRTU,
)
assert isinstance(
ModbusSerialClient("/dev/null", framer=FramerType.SOCKET).framer,
FramerSocket,
)

def test_sync_serial_rtu_client_timeouts(self):
"""Test sync serial rtu."""
Expand Down