-
Notifications
You must be signed in to change notification settings - Fork 1k
Fixes for synchronized client and transaction handling #575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c709c62
Improve sync client unit tests
thormick 8693468
Raise exception when modbus unit unexpectedly closes connection
thormick 8cbf88f
Default to 4K receive buffer size
thormick ec03fdb
Add ModbusTcpDiagClient
thormick 7973995
Improve timeout error message
thormick 870be43
Log debug on no response to unbounded read
thormick 24c0ffc
Close connection on no/erroneous message received
thormick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import socket | ||
| import logging | ||
| import time | ||
|
|
||
| from pymodbus.constants import Defaults | ||
| from pymodbus.client.sync import ModbusTcpClient | ||
| from pymodbus.transaction import ModbusSocketFramer | ||
| from pymodbus.exceptions import ConnectionException | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| LOG_MSGS = { | ||
| 'conn_msg': 'Connecting to modbus device %s', | ||
| 'connfail_msg': 'Connection to (%s, %s) failed: %s', | ||
| 'discon_msg': 'Disconnecting from modbus device %s', | ||
| 'timelimit_read_msg': | ||
| 'Modbus device read took %.4f seconds, ' | ||
| 'returned %s bytes in timelimit read', | ||
| 'timeout_msg': | ||
| 'Modbus device timeout after %.4f seconds, ' | ||
| 'returned %s bytes %s', | ||
| 'delay_msg': | ||
| 'Modbus device read took %.4f seconds, ' | ||
| 'returned %s bytes of %s expected', | ||
| 'read_msg': | ||
| 'Modbus device read took %.4f seconds, ' | ||
| 'returned %s bytes of %s expected', | ||
| 'unexpected_dc_msg': '%s %s'} | ||
|
|
||
|
|
||
| class ModbusTcpDiagClient(ModbusTcpClient): | ||
| """ | ||
| Variant of pymodbus.client.sync.ModbusTcpClient with additional | ||
| logging to diagnose network issues. | ||
|
|
||
| The following events are logged: | ||
|
|
||
| +---------+-----------------------------------------------------------------+ | ||
| | Level | Events | | ||
| +=========+=================================================================+ | ||
| | ERROR | Failure to connect to modbus unit; unexpected disconnect by | | ||
| | | modbus unit | | ||
| +---------+-----------------------------------------------------------------+ | ||
| | WARNING | Timeout on normal read; read took longer than warn_delay_limit | | ||
| +---------+-----------------------------------------------------------------+ | ||
| | INFO | Connection attempt to modbus unit; disconnection from modbus | | ||
| | | unit; each time limited read | | ||
| +---------+-----------------------------------------------------------------+ | ||
| | DEBUG | Normal read with timing information | | ||
| +---------+-----------------------------------------------------------------+ | ||
|
|
||
| Reads are differentiated between "normal", which reads a specified number of | ||
| bytes, and "time limited", which reads all data for a duration equal to the | ||
| timeout period configured for this instance. | ||
| """ | ||
|
|
||
| # pylint: disable=no-member | ||
|
|
||
| def __init__(self, host='127.0.0.1', port=Defaults.Port, | ||
| framer=ModbusSocketFramer, **kwargs): | ||
| """ Initialize a client instance | ||
|
|
||
| The keys of LOG_MSGS can be used in kwargs to customize the messages. | ||
|
|
||
| :param host: The host to connect to (default 127.0.0.1) | ||
| :param port: The modbus port to connect to (default 502) | ||
| :param source_address: The source address tuple to bind to (default ('', 0)) | ||
| :param timeout: The timeout to use for this socket (default Defaults.Timeout) | ||
| :param warn_delay_limit: Log reads that take longer than this as warning. | ||
| Default True sets it to half of "timeout". None never logs these as | ||
| warning, 0 logs everything as warning. | ||
| :param framer: The modbus framer to use (default ModbusSocketFramer) | ||
|
|
||
| .. note:: The host argument will accept ipv4 and ipv6 hosts | ||
| """ | ||
| self.warn_delay_limit = kwargs.get('warn_delay_limit', True) | ||
| super().__init__(host, port, framer, **kwargs) | ||
| if self.warn_delay_limit is True: | ||
| self.warn_delay_limit = self.timeout / 2 | ||
|
|
||
| # Set logging messages, defaulting to LOG_MSGS | ||
| for (k, v) in LOG_MSGS.items(): | ||
| self.__dict__[k] = kwargs.get(k, v) | ||
|
|
||
| def connect(self): | ||
| """ Connect to the modbus tcp server | ||
|
|
||
| :returns: True if connection succeeded, False otherwise | ||
| """ | ||
| if self.socket: | ||
| return True | ||
| try: | ||
| _logger.info(self.conn_msg, self) | ||
| self.socket = socket.create_connection( | ||
| (self.host, self.port), | ||
| timeout=self.timeout, | ||
| source_address=self.source_address) | ||
| except socket.error as msg: | ||
| _logger.error(self.connfail_msg, self.host, self.port, msg) | ||
| self.close() | ||
| return self.socket is not None | ||
|
|
||
| def close(self): | ||
| """ Closes the underlying socket connection | ||
| """ | ||
| if self.socket: | ||
| _logger.info(self.discon_msg, self) | ||
| self.socket.close() | ||
| self.socket = None | ||
|
|
||
| def _recv(self, size): | ||
| try: | ||
| start = time.time() | ||
|
|
||
| result = super()._recv(size) | ||
|
|
||
| delay = time.time() - start | ||
| if self.warn_delay_limit is not None and delay >= self.warn_delay_limit: | ||
| self._log_delayed_response(len(result), size, delay) | ||
| elif not size: | ||
| _logger.debug(self.timelimit_read_msg, delay, len(result)) | ||
| else: | ||
| _logger.debug(self.read_msg, delay, len(result), size) | ||
|
|
||
| return result | ||
| except ConnectionException as ex: | ||
| # Only log actual network errors, "if not self.socket" then it's a internal code issue | ||
| if 'Connection unexpectedly closed' in ex.string: | ||
| _logger.error(self.unexpected_dc_msg, self, ex) | ||
| raise ex | ||
|
|
||
| def _log_delayed_response(self, result_len, size, delay): | ||
| if not size and result_len > 0: | ||
| _logger.info(self.timelimit_read_msg, delay, result_len) | ||
| elif (result_len == 0 or (size and result_len < size)) and delay >= self.timeout: | ||
| read_type = ("of %i expected" % size) if size else "in timelimit read" | ||
| _logger.warning(self.timeout_msg, delay, result_len, read_type) | ||
| else: | ||
| _logger.warning(self.delay_msg, delay, result_len, size) | ||
|
|
||
| def __str__(self): | ||
| """ Builds a string representation of the connection | ||
|
|
||
| :returns: The string representation | ||
| """ | ||
| return "ModbusTcpDiagClient(%s:%s)" % (self.host, self.port) | ||
|
|
||
|
|
||
| def get_client(): | ||
| """ Returns an appropriate client based on logging level | ||
|
|
||
| This will be ModbusTcpDiagClient by default, or the parent class | ||
| if the log level is such that the diagnostic client will not log | ||
| anything. | ||
|
|
||
| :returns: ModbusTcpClient or a child class thereof | ||
| """ | ||
| return ModbusTcpDiagClient if _logger.isEnabledFor(logging.ERROR) else ModbusTcpClient | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- # | ||
| # Exported symbols | ||
| # --------------------------------------------------------------------------- # | ||
|
|
||
| __all__ = [ | ||
| "ModbusTcpDiagClient", "get_client" | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This value is debatable. This would essentially block the
recvtill all4096bytes are received or timeout. Adding further to delay in reads.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not how
recvworks. From the documentation:So it never blocks when a single byte is available, regardless of what
buffersizeis provided. I.e. it's a maximum limit, the minimum limit is always 1 regardless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thormick That matches the underlying recv man page too:
https://man7.org/linux/man-pages/man2/recv.2.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, I got this confused with
Serial.Apologies for the mistake. Looks good.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it's good that you picked on it, it made me gave it a quick look over and I realize now that
ModbusTlsClientdoes the same, and now those two will be inconsistent. I don't have a setup ready for testing modbus TLS, so I'm not sure about touching that part of the code. Not necessarily a problem, though?