-
Notifications
You must be signed in to change notification settings - Fork 1k
Check received data length #288
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
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| from threading import RLock | ||
|
|
||
| from pymodbus.exceptions import ModbusIOException, NotImplementedException | ||
| from pymodbus.exceptions import InvalidMessageRecievedException | ||
| from pymodbus.exceptions import InvalidMessageReceivedException | ||
| from pymodbus.constants import Defaults | ||
| from pymodbus.framer.ascii_framer import ModbusAsciiFramer | ||
| from pymodbus.framer.rtu_framer import ModbusRtuFramer | ||
|
|
@@ -74,14 +74,9 @@ def _set_adu_size(self): | |
| self.base_adu_size = 7 # start(1)+ Address(2), LRC(2) + end(2) | ||
| elif isinstance(self.client.framer, ModbusBinaryFramer): | ||
| self.base_adu_size = 5 # start(1) + Address(1), CRC(2) + end(1) | ||
| else: | ||
| self.base_adu_size = -1 | ||
|
|
||
| def _calculate_response_length(self, expected_pdu_size): | ||
| if self.base_adu_size == -1: | ||
| return None | ||
| else: | ||
| return self.base_adu_size + expected_pdu_size | ||
| return self.base_adu_size + expected_pdu_size | ||
|
|
||
| def _calculate_exception_length(self): | ||
| ''' Returns the length of the Modbus Exception Response according to | ||
|
|
@@ -94,8 +89,6 @@ def _calculate_exception_length(self): | |
| elif isinstance(self.client.framer, (ModbusRtuFramer, ModbusBinaryFramer)): | ||
| return self.base_adu_size + 2 # Fcode(1), ExcecptionCode(1) | ||
|
|
||
| return None | ||
|
|
||
| def _check_response(self, response): | ||
| ''' Checks if the response is a Modbus Exception. | ||
| ''' | ||
|
|
@@ -208,11 +201,11 @@ def _transact(self, packet, response_length, full=False): | |
| _logger.debug("Changing transaction state from 'SENDING' " | ||
| "to 'WAITING FOR REPLY'") | ||
| self.client.state = ModbusTransactionState.WAITING_FOR_REPLY | ||
| result = self._recv(response_length or 1024, full) | ||
| result = self._recv(response_length, full) | ||
| if _logger.isEnabledFor(logging.DEBUG): | ||
| _logger.debug("RECV: " + hexlify_packets(result)) | ||
| except (socket.error, ModbusIOException, | ||
| InvalidMessageRecievedException) as msg: | ||
| InvalidMessageReceivedException) as msg: | ||
| self.client.close() | ||
| _logger.debug("Transaction failed. (%s) " % msg) | ||
| last_exception = msg | ||
|
|
@@ -223,7 +216,6 @@ def _send(self, packet): | |
| return self.client.framer.sendPacket(packet) | ||
|
|
||
| def _recv(self, expected_response_length, full): | ||
| expected_response_length = expected_response_length or 1024 | ||
| if not full: | ||
| exception_length = self._calculate_exception_length() | ||
| if isinstance(self.client.framer, ModbusSocketFramer): | ||
|
|
@@ -238,31 +230,37 @@ def _recv(self, expected_response_length, full): | |
| min_size = expected_response_length | ||
|
|
||
| read_min = self.client.framer.recvPacket(min_size) | ||
| if read_min: | ||
| if not read_min: | ||
| return read_min | ||
|
|
||
| if len(read_min) < min_size: | ||
| raise InvalidMessageReceivedException( | ||
| "Incomplete message received, expected at least %d bytes (%d received)" | ||
| % (min_size, len(read_min))) | ||
|
|
||
| if isinstance(self.client.framer, ModbusSocketFramer): | ||
| func_code = byte2int(read_min[-1]) | ||
| elif isinstance(self.client.framer, ModbusRtuFramer): | ||
| func_code = byte2int(read_min[-1]) | ||
| elif isinstance(self.client.framer, ModbusAsciiFramer): | ||
| func_code = int(read_min[3:5], 16) | ||
| elif isinstance(self.client.framer, ModbusBinaryFramer): | ||
| func_code = byte2int(read_min[-1]) | ||
| else: | ||
| func_code = -1 | ||
|
|
||
| if func_code < 0x80: # Not an error | ||
| if isinstance(self.client.framer, ModbusSocketFramer): | ||
| func_code = byte2int(read_min[-1]) | ||
| elif isinstance(self.client.framer, ModbusRtuFramer): | ||
| func_code = byte2int(read_min[-1]) | ||
| elif isinstance(self.client.framer, ModbusAsciiFramer): | ||
| func_code = int(read_min[3:5], 16) | ||
| elif isinstance(self.client.framer, ModbusBinaryFramer): | ||
| func_code = byte2int(read_min[-1]) | ||
| else: | ||
| func_code = -1 | ||
|
|
||
| if func_code < 0x80: # Not an error | ||
| if isinstance(self.client.framer, ModbusSocketFramer): | ||
| # Ommit UID, which is included in header size | ||
| h_size = self.client.framer._hsize | ||
| length = struct.unpack(">H", read_min[4:6])[0] - 1 | ||
| expected_response_length = h_size + length | ||
| expected_response_length -= min_size | ||
| total = expected_response_length + min_size | ||
| else: | ||
| expected_response_length = exception_length - min_size | ||
| total = expected_response_length + min_size | ||
| # Ommit UID, which is included in header size | ||
| h_size = self.client.framer._hsize | ||
| length = struct.unpack(">H", read_min[4:6])[0] - 1 | ||
| expected_response_length = h_size + length | ||
| expected_response_length -= min_size | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would throw |
||
| total = expected_response_length + min_size | ||
| else: | ||
| total = expected_response_length | ||
| expected_response_length = exception_length - min_size | ||
| total = expected_response_length + min_size | ||
|
|
||
| else: | ||
| read_min = b'' | ||
| total = expected_response_length | ||
|
|
@@ -273,6 +271,9 @@ def _recv(self, expected_response_length, full): | |
| _logger.debug("Incomplete message received, " | ||
| "Expected {} bytes Recieved " | ||
| "{} bytes !!!!".format(total, actual)) | ||
| raise InvalidMessageReceivedException( | ||
| "Incomplete message received, %d bytes expected (%d received)" | ||
| % (total, actual)) | ||
| if self.client.state != ModbusTransactionState.PROCESSING_REPLY: | ||
| _logger.debug("Changing transaction state from " | ||
| "'WAITING FOR REPLY' to 'PROCESSING REPLY'") | ||
|
|
||
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.
We can not pass
Noneas response_length, in cases where a transaction returns no response, the argumentfullwould be set to true and subsequent transactions would try to read the full length (Nonein some cases) throwing an error. Can we have 1024 back here ?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.
I have made a wrong assumption, that is all messages have an expected_response_length.
If i get the 1024 back, the
client._recvfunction will wait until it receives 1024 bytes or the timeout expires, so probably it always waits until the timeout expires, making the communication very slow.Another solution should be to make the
client._recv(size)to return the data as soon as at least 1 byte is received, without waits that timeout expires whensize=Noneis passed.What do you think about it?
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.
In most of the normal cases (read/write registers/coils) expected_length is always calculated so it should not be an issue, for some diagnostic or mei requests the response length is dynamic and hence we could choose to read 1024 bytes till time out ,which is still OK with me.