From 8aef2137933119340d0744a50171c4024601ce05 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Wed, 5 Jul 2023 17:45:14 +0200 Subject: [PATCH] No local_addr for udp. --- pymodbus/transport/transport.py | 60 +++++++++++++++++---------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 7762b77add..2fdf1df645 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -140,7 +140,36 @@ def __init__( self.init_correct_serial() if self.init_check_nullmodem(): return + self.init_setup_connect_listen() + def init_correct_serial(self) -> None: + """Split host for serial if needed.""" + if self.is_server: + host = self.comm_params.source_address[0] + if host.startswith("socket"): + parts = host[9:].split(":") + self.comm_params.source_address = (parts[0], int(parts[1])) + self.comm_params.comm_type = CommType.TCP + elif host.startswith(NULLMODEM_HOST): + self.comm_params.source_address = (host, int(host[9:].split(":")[1])) + return + if self.comm_params.host.startswith(NULLMODEM_HOST): + self.comm_params.port = int(self.comm_params.host[9:].split(":")[1]) + + def init_check_nullmodem(self) -> bool: + """Check if nullmodem is needed.""" + if self.comm_params.host.startswith(NULLMODEM_HOST): + port = self.comm_params.port + elif self.comm_params.source_address[0].startswith(NULLMODEM_HOST): + port = self.comm_params.source_address[1] + else: + return False + + self.call_create = lambda: self.create_nullmodem(port) + return True + + def init_setup_connect_listen(self) -> None: + """Handle connect/listen handler.""" if self.comm_params.comm_type == CommType.SERIAL: self.call_create = lambda: create_serial_connection( self.loop, @@ -154,7 +183,7 @@ def __init__( ) return if self.comm_params.comm_type == CommType.UDP: - if is_server: + if self.is_server: self.call_create = lambda: self.loop.create_datagram_endpoint( self.handle_new_connection, local_addr=self.comm_params.source_address, @@ -162,12 +191,11 @@ def __init__( else: self.call_create = lambda: self.loop.create_datagram_endpoint( self.handle_new_connection, - local_addr=self.comm_params.source_address, remote_addr=(self.comm_params.host, self.comm_params.port), ) return # TLS and TCP - if is_server: + if self.is_server: self.call_create = lambda: self.loop.create_server( self.handle_new_connection, self.comm_params.source_address[0], @@ -185,32 +213,6 @@ def __init__( ssl=self.comm_params.sslctx, ) - def init_correct_serial(self) -> None: - """Split host for serial if needed.""" - if self.is_server: - host = self.comm_params.source_address[0] - if host.startswith("socket"): - parts = host[9:].split(":") - self.comm_params.source_address = (parts[0], int(parts[1])) - self.comm_params.comm_type = CommType.TCP - elif host.startswith(NULLMODEM_HOST): - self.comm_params.source_address = (host, int(host[9:].split(":")[1])) - return - if self.comm_params.host.startswith(NULLMODEM_HOST): - self.comm_params.port = int(self.comm_params.host[9:].split(":")[1]) - - def init_check_nullmodem(self) -> bool: - """Check if nullmodem is needed.""" - if self.comm_params.host.startswith(NULLMODEM_HOST): - port = self.comm_params.port - elif self.comm_params.source_address[0].startswith(NULLMODEM_HOST): - port = self.comm_params.source_address[1] - else: - return False - - self.call_create = lambda: self.create_nullmodem(port) - return True - async def transport_connect(self) -> bool: """Handle generic connect and call on to specific transport connect.""" Log.debug("Connecting {}", self.comm_params.comm_name)