Skip to content
Open
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
20 changes: 18 additions & 2 deletions pyModbusTCP/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class _ModbusExcept(_InternalError):
def __init__(self, code):
self.code = code

def __init__(self, host='localhost', port=502, unit_id=1, timeout=30.0, auto_open=True, auto_close=False):
def __init__(self, host='localhost', port=502, unit_id=1, timeout=30.0, auto_open=True, auto_close=False, is_internal=True):
"""Constructor.

:param host: hostname or IPv4/IPv6 address server address
Expand All @@ -102,6 +102,8 @@ def __init__(self, host='localhost', port=502, unit_id=1, timeout=30.0, auto_ope
:type auto_open: bool
:param auto_close: auto TCP close)
:type auto_close: bool
:param is_internal: for socket connection
:type is_internal: bool
:return: Object ModbusClient
:rtype: ModbusClient
"""
Expand All @@ -113,6 +115,7 @@ def __init__(self, host='localhost', port=502, unit_id=1, timeout=30.0, auto_ope
self._timeout = None
self._auto_open = None
self._auto_close = None
self._is_internal = None
self._sock = socket.socket()
self._transaction_id = 0 # MBAP transaction ID
self._version = VERSION # this package version number
Expand All @@ -126,6 +129,7 @@ def __init__(self, host='localhost', port=502, unit_id=1, timeout=30.0, auto_ope
self.timeout = timeout
self.auto_open = auto_open
self.auto_close = auto_close
self.is_internal = is_internal

def __repr__(self):
r_str = 'ModbusClient(host=\'%s\', port=%d, unit_id=%d, timeout=%.2f, auto_open=%s, auto_close=%s)'
Expand Down Expand Up @@ -274,6 +278,15 @@ def auto_close(self, value):
# enforce type
self._auto_close = bool(value)

@property
def is_internal(self):
"""Get or set whether the modbus connection is internal or external."""
return self._is_internal

@is_internal.setter
def is_internal(self, value):
self._is_internal = bool(value)

@property
def is_open(self):
"""Get current status of the TCP connection (True = open)."""
Expand Down Expand Up @@ -310,7 +323,10 @@ def _open(self):
continue
try:
self._sock.settimeout(self.timeout)
self._sock.connect(sa)
if self.is_internal:
self._sock.connect(sa)
else:
self._sock.connect_ex(sa)
except socket.error:
self._sock.close()
continue
Expand Down