From 1eb494a3e90208b7f4c245709b8ad1537d59df15 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Thu, 26 Oct 2023 11:26:34 -0500 Subject: [PATCH 1/4] Rework host/port and listener setup --- pymodbus/transport/transport.py | 48 ++++++++++++++++----------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 56dd82fe9..e5875b6e2 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -176,39 +176,37 @@ 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 host == NULLMODEM_HOST: + if NULLMODEM_HOST in host: + if self.comm_params.comm_type == CommType.SERIAL: + host, port = NULLMODEM_HOST, int(host[9:].split(":")[1]) 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://:port" self.comm_params.comm_type = CommType.TCP + host = host.strip("socket://") 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[0], int(parts[1]) + 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( From 2e68e2646544db2939efbf7c9178a41f565f54bd Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Fri, 27 Oct 2023 16:32:46 -0500 Subject: [PATCH 2/4] simplify --- pymodbus/transport/transport.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index e5875b6e2..f6aa33a7c 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -188,8 +188,7 @@ def __init__( ): # format is "socket://:port" self.comm_params.comm_type = CommType.TCP - host = host.strip("socket://") - parts = host.split(":") + parts = host.strip("socket://").split(":") host, port = parts[0], int(parts[1]) self.init_setup_connect_listen(host, port) From bcce9ae1156f04a9152512839741edce3bf9818d Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Mon, 30 Oct 2023 15:02:24 -0500 Subject: [PATCH 3/4] rework nullmodem --- pymodbus/transport/transport.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index f6aa33a7c..464b81498 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -176,9 +176,9 @@ def __init__( else: host = self.comm_params.host port = int(self.comm_params.port) - if NULLMODEM_HOST in host: - if self.comm_params.comm_type == CommType.SERIAL: - host, port = NULLMODEM_HOST, int(host[9:].split(":")[1]) + 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 if ( From f9b4f410d8ee91fca3514a2e4628d5e48cbfbbf4 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Mon, 30 Oct 2023 18:58:20 -0500 Subject: [PATCH 4/4] jan strongly prefers this style :P --- pymodbus/transport/transport.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 464b81498..0f1ba0375 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -188,8 +188,8 @@ def __init__( ): # format is "socket://:port" self.comm_params.comm_type = CommType.TCP - parts = host.strip("socket://").split(":") - host, port = parts[0], int(parts[1]) + parts = host.split(":") + 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: