Skip to content

Commit

Permalink
check_frame.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Sep 30, 2024
1 parent f5ea97a commit 66dacdf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
43 changes: 2 additions & 41 deletions pymodbus/framer/old_framer_rtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,46 +70,6 @@ def decode_data(self, data):
return {"slave": uid, "fcode": fcode}
return {}


def xx_get_frame_start(self, buffer, slaves, broadcast, skip_cur_frame, function_codes):
"""Scan buffer for a relevant frame start."""
start = 1 if skip_cur_frame else 0
if (buf_len := len(buffer)) < 4:
return buffer, False
for i in range(start, buf_len - 3): # <slave id><function code><crc 2 bytes>
if not broadcast and buffer[i] not in slaves:
continue
if (
buffer[i + 1] not in function_codes
and (buffer[i + 1] - 0x80) not in function_codes
):
continue
if i:
buffer = buffer[i:] # remove preceding trash.
return buffer, True
if buf_len > 3:
buffer = buffer[-3:]
return buffer, False

def old_check_frame(self, buffer, decoder):
"""Check if the next frame is available."""
try:
self.dev_id = int(buffer[0])
func_code = int(buffer[1])
pdu_class = decoder.lookupPduClass(func_code)
size = pdu_class.calculateRtuFrameSize(buffer)
self.msg_len = size

if len(buffer) < size:
raise IndexError
frame_size = self.msg_len
data = buffer[: frame_size - 2]
crc = buffer[size - 2 : size]
crc_val = (int(crc[0]) << 8) + int(crc[1])
return FramerRTU.check_CRC(data, crc_val)
except (IndexError, KeyError, struct.error):
return False

def frameProcessIncomingPacket(self, _single, callback, slave, tid=None):
"""Process new packet pattern."""
broadcast = not slave[0]
Expand All @@ -122,7 +82,8 @@ def frameProcessIncomingPacket(self, _single, callback, slave, tid=None):
if not ok:
Log.debug("Frame - not ready")
break
if not self.old_check_frame(self._buffer, self.decoder):
self.dev_id, self.msg_len, ok = self.message_handler.old_check_frame(self._buffer, self.msg_len, self.decoder)
if not ok:
Log.debug("Frame check failed, ignoring!!")
x = self._buffer
self.resetFrame()
Expand Down
20 changes: 20 additions & 0 deletions pymodbus/framer/rtu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Modbus RTU frame implementation."""
from __future__ import annotations

import struct

from pymodbus.framer.base import FramerBase
from pymodbus.logging import Log

Expand Down Expand Up @@ -113,6 +115,24 @@ def old_get_frame_start(self, buffer, slaves, broadcast, skip_cur_frame, functio
buffer = buffer[-3:]
return buffer, False

def old_check_frame(self, buffer, msg_len, decoder):
"""Check if the next frame is available."""
try:
dev_id = int(buffer[0])
func_code = int(buffer[1])
pdu_class = decoder.lookupPduClass(func_code)
size = pdu_class.calculateRtuFrameSize(buffer)

if len(buffer) < size:
raise IndexError
frame_size = msg_len
data = buffer[: frame_size - 2]
crc = buffer[size - 2 : size]
crc_val = (int(crc[0]) << 8) + int(crc[1])
return dev_id, size, FramerRTU.check_CRC(data, crc_val)
except (IndexError, KeyError, struct.error):
return dev_id, size, False


def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
"""Decode ADU."""
Expand Down

0 comments on commit 66dacdf

Please sign in to comment.