|
5 | 5 | import struct |
6 | 6 | import socket |
7 | 7 | from binascii import b2a_hex, a2b_hex |
8 | | - |
| 8 | +from serial import SerialException |
9 | 9 | from pymodbus.exceptions import ModbusIOException, NotImplementedException |
10 | 10 | from pymodbus.exceptions import InvalidMessageRecievedException |
11 | 11 | from pymodbus.constants import Defaults |
@@ -57,13 +57,13 @@ def __init__(self, client, **kwargs): |
57 | 57 | def _set_adu_size(self): |
58 | 58 | # base ADU size of modbus frame in bytes |
59 | 59 | if isinstance(self.client.framer, ModbusSocketFramer): |
60 | | - self.base_adu_size = 7 # tid(2), pid(2), length(2), uid(1) |
| 60 | + self.base_adu_size = 7 # tid(2), pid(2), length(2), uid(1) |
61 | 61 | elif isinstance(self.client.framer, ModbusRtuFramer): |
62 | | - self.base_adu_size = 3 # address(1), CRC(2) |
| 62 | + self.base_adu_size = 3 # address(1), CRC(2) |
63 | 63 | elif isinstance(self.client.framer, ModbusAsciiFramer): |
64 | | - self.base_adu_size = 7 # start(1)+ Address(2), LRC(2) + end(2) |
| 64 | + self.base_adu_size = 7 # start(1)+ Address(2), LRC(2) + end(2) |
65 | 65 | elif isinstance(self.client.framer, ModbusBinaryFramer): |
66 | | - self.base_adu_size = 3 #, Address(1), CRC(2) |
| 66 | + self.base_adu_size = 5 # start(1) + Address(1), CRC(2) + end(1) |
67 | 67 | else: |
68 | 68 | self.base_adu_size = -1 |
69 | 69 |
|
@@ -169,20 +169,35 @@ def _recv(self, expected_response_length): |
169 | 169 | expected_response_length = self._calculate_exception_length() |
170 | 170 | continue |
171 | 171 | if isinstance(self.client.framer, ModbusSocketFramer): |
172 | | - length = struct.unpack(">H", result[4:6])[0] -1 # Ommit UID, which is included in header size |
173 | | - expected_response_length = self.client.framer._ModbusSocketFramer__hsize + length |
174 | | - |
175 | | - r = self.client._recv(expected_response_length - len(result)) |
176 | | - if not r: |
177 | | - # If no response being recived there is no point in conitnuing |
| 172 | + # Ommit UID, which is included in header size |
| 173 | + h_size = self.client.framer._ModbusSocketFramer__hsize |
| 174 | + length = struct.unpack(">H", result[4:6])[0] -1 |
| 175 | + expected_response_length = h_size + length |
| 176 | + |
| 177 | + if expected_response_length != len(result): |
| 178 | + _logger.debug("Expected - {} bytes, " |
| 179 | + "Actual - {} bytes".format( |
| 180 | + expected_response_length, len(result)) |
| 181 | + ) |
| 182 | + try: |
| 183 | + r = self.client._recv( |
| 184 | + expected_response_length - len(result) |
| 185 | + ) |
| 186 | + result += r |
| 187 | + if not r: |
| 188 | + # If no response being recived there |
| 189 | + # is no point in conitnuing |
| 190 | + break |
| 191 | + except (TimeoutError, SerialException): |
| 192 | + break |
| 193 | + else: |
178 | 194 | break |
179 | | - result += r |
| 195 | + |
180 | 196 | if result: |
181 | 197 | break |
182 | 198 | retries -= 1 |
183 | 199 | return result |
184 | 200 |
|
185 | | - |
186 | 201 | def addTransaction(self, request, tid=None): |
187 | 202 | ''' Adds a transaction to the handler |
188 | 203 |
|
|
0 commit comments