Skip to content

Commit 8aef213

Browse files
committed
No local_addr for udp.
1 parent b03c6ea commit 8aef213

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

pymodbus/transport/transport.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,36 @@ def __init__(
140140
self.init_correct_serial()
141141
if self.init_check_nullmodem():
142142
return
143+
self.init_setup_connect_listen()
143144

145+
def init_correct_serial(self) -> None:
146+
"""Split host for serial if needed."""
147+
if self.is_server:
148+
host = self.comm_params.source_address[0]
149+
if host.startswith("socket"):
150+
parts = host[9:].split(":")
151+
self.comm_params.source_address = (parts[0], int(parts[1]))
152+
self.comm_params.comm_type = CommType.TCP
153+
elif host.startswith(NULLMODEM_HOST):
154+
self.comm_params.source_address = (host, int(host[9:].split(":")[1]))
155+
return
156+
if self.comm_params.host.startswith(NULLMODEM_HOST):
157+
self.comm_params.port = int(self.comm_params.host[9:].split(":")[1])
158+
159+
def init_check_nullmodem(self) -> bool:
160+
"""Check if nullmodem is needed."""
161+
if self.comm_params.host.startswith(NULLMODEM_HOST):
162+
port = self.comm_params.port
163+
elif self.comm_params.source_address[0].startswith(NULLMODEM_HOST):
164+
port = self.comm_params.source_address[1]
165+
else:
166+
return False
167+
168+
self.call_create = lambda: self.create_nullmodem(port)
169+
return True
170+
171+
def init_setup_connect_listen(self) -> None:
172+
"""Handle connect/listen handler."""
144173
if self.comm_params.comm_type == CommType.SERIAL:
145174
self.call_create = lambda: create_serial_connection(
146175
self.loop,
@@ -154,20 +183,19 @@ def __init__(
154183
)
155184
return
156185
if self.comm_params.comm_type == CommType.UDP:
157-
if is_server:
186+
if self.is_server:
158187
self.call_create = lambda: self.loop.create_datagram_endpoint(
159188
self.handle_new_connection,
160189
local_addr=self.comm_params.source_address,
161190
)
162191
else:
163192
self.call_create = lambda: self.loop.create_datagram_endpoint(
164193
self.handle_new_connection,
165-
local_addr=self.comm_params.source_address,
166194
remote_addr=(self.comm_params.host, self.comm_params.port),
167195
)
168196
return
169197
# TLS and TCP
170-
if is_server:
198+
if self.is_server:
171199
self.call_create = lambda: self.loop.create_server(
172200
self.handle_new_connection,
173201
self.comm_params.source_address[0],
@@ -185,32 +213,6 @@ def __init__(
185213
ssl=self.comm_params.sslctx,
186214
)
187215

188-
def init_correct_serial(self) -> None:
189-
"""Split host for serial if needed."""
190-
if self.is_server:
191-
host = self.comm_params.source_address[0]
192-
if host.startswith("socket"):
193-
parts = host[9:].split(":")
194-
self.comm_params.source_address = (parts[0], int(parts[1]))
195-
self.comm_params.comm_type = CommType.TCP
196-
elif host.startswith(NULLMODEM_HOST):
197-
self.comm_params.source_address = (host, int(host[9:].split(":")[1]))
198-
return
199-
if self.comm_params.host.startswith(NULLMODEM_HOST):
200-
self.comm_params.port = int(self.comm_params.host[9:].split(":")[1])
201-
202-
def init_check_nullmodem(self) -> bool:
203-
"""Check if nullmodem is needed."""
204-
if self.comm_params.host.startswith(NULLMODEM_HOST):
205-
port = self.comm_params.port
206-
elif self.comm_params.source_address[0].startswith(NULLMODEM_HOST):
207-
port = self.comm_params.source_address[1]
208-
else:
209-
return False
210-
211-
self.call_create = lambda: self.create_nullmodem(port)
212-
return True
213-
214216
async def transport_connect(self) -> bool:
215217
"""Handle generic connect and call on to specific transport connect."""
216218
Log.debug("Connecting {}", self.comm_params.comm_name)

0 commit comments

Comments
 (0)