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

Rework host/port and listener setup #1866

Merged
merged 5 commits into from
Oct 31, 2023
Merged
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
45 changes: 21 additions & 24 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,39 +176,36 @@ def __init__(
else:
host = self.comm_params.host
port = int(self.comm_params.port)
if self.comm_params.comm_type == CommType.SERIAL:
host, port = self.init_setup_serial(host, port)
if not host and not port:
return
if self.comm_params.comm_type == CommType.SERIAL and NULLMODEM_HOST in host:
host, port = NULLMODEM_HOST, int(host[9:].split(":")[1])
if host == NULLMODEM_HOST:
self.call_create = lambda: self.create_nullmodem(port)
return
# TCP/TLS/UDP
self.init_setup_connect_listen(host, port)

def init_setup_serial(self, host: str, _port: int) -> tuple[str, int]:
"""Split host for serial if needed."""
if NULLMODEM_HOST in host:
return NULLMODEM_HOST, int(host[9:].split(":")[1])
if self.is_server and host.startswith("socket"):
if (
self.comm_params.comm_type == CommType.SERIAL
and self.is_server
and host.startswith("socket")
):
# format is "socket://<host>:port"
self.comm_params.comm_type = CommType.TCP
parts = host.split(":")
return parts[1][2:], int(parts[2])
self.call_create = lambda: create_serial_connection(
self.loop,
self.handle_new_connection,
host,
baudrate=self.comm_params.baudrate,
bytesize=self.comm_params.bytesize,
parity=self.comm_params.parity,
stopbits=self.comm_params.stopbits,
timeout=self.comm_params.timeout_connect,
)
return None, None
host, port = parts[1][2:], int(parts[2])
self.init_setup_connect_listen(host, port)

def init_setup_connect_listen(self, host: str, port: int) -> None:
"""Handle connect/listen handler."""
if self.comm_params.comm_type == CommType.SERIAL:
self.call_create = lambda: create_serial_connection(
self.loop,
self.handle_new_connection,
host,
baudrate=self.comm_params.baudrate,
bytesize=self.comm_params.bytesize,
parity=self.comm_params.parity,
stopbits=self.comm_params.stopbits,
timeout=self.comm_params.timeout_connect,
)
return
if self.comm_params.comm_type == CommType.UDP:
if self.is_server:
self.call_create = lambda: self.loop.create_datagram_endpoint(
Expand Down