Skip to content

Commit

Permalink
No local_addr for udp.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Jul 5, 2023
1 parent b03c6ea commit 8aef213
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -154,20 +183,19 @@ 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,
)
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],
Expand All @@ -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)
Expand Down

0 comments on commit 8aef213

Please sign in to comment.