I'm glad your project helped me. During the project, I found the following problems:
The receiving function in the project has a sticky package problem (In the type3e.py line 149):
origin code:
def _recv(self):
"""recieve mc protocol data
Returns:
recv_data
"""
recv_data = self._sock.recv(self._SOCKBUFSIZE)
return recv_data
fix code:
def _recv(self):
"""recieve mc protocol data with proper TCP fragmentation handling
Reads the response frame in two stages to avoid sticky packet issues:
1. Read the header to determine the total frame length from the Data Length field
2. Read remaining data in a loop until the complete frame is received
Returns:
recv_data (bytes): complete mc protocol response data
"""
# Stage 1: Read header (up to Status field, which includes Data Length)
header_size = self._get_answerstatus_index()
data = b''
while len(data) < header_size:
chunk = self._sock.recv(header_size - len(data))
if not chunk:
raise ConnectionError("Connection closed by PLC")
data += chunk
# Parse Data Length from header
dl_start = header_size - self._wordsize
if self.commtype == const.COMMTYPE_BINARY:
data_length = int.from_bytes(data[dl_start:header_size], "little")
else:
data_length = int(data[dl_start:header_size].decode(), 16)
total_len = header_size + data_length
# Stage 2: Read remaining data until complete frame
while len(data) < total_len:
chunk = self._sock.recv(total_len - len(data))
if not chunk:
raise ConnectionError("Connection closed by PLC")
data += chunk
return data
This is the revised type3e.py attachment: type3e.py
I'm glad your project helped me. During the project, I found the following problems:
The receiving function in the project has a sticky package problem (In the type3e.py line 149):
origin code:
fix code:
This is the revised type3e.py attachment: type3e.py