Skip to content

Commit

Permalink
Remove control encode in framersRemove control encode in framers. (#2095
Browse files Browse the repository at this point in the history
)
  • Loading branch information
janiversen authored Mar 7, 2024
1 parent a61db90 commit f63a4eb
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 61 deletions.
27 changes: 3 additions & 24 deletions pymodbus/framer/ascii_framer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Ascii_framer."""
# pylint: disable=missing-type-doc
import struct
from binascii import a2b_hex, b2a_hex
from binascii import a2b_hex

from pymodbus.exceptions import ModbusIOException
from pymodbus.framer.base import BYTE_ORDER, FRAME_HEADER, ModbusFramer
Expand Down Expand Up @@ -96,29 +95,9 @@ def check_frame(self):
def buildPacket(self, message):
"""Create a ready to send modbus packet.
Built off of a modbus request/response
:param message: The request/response to send
:return: The encoded packet
"""
encoded = message.encode()
buffer = struct.pack(
ASCII_FRAME_HEADER, message.slave_id, message.function_code
)
checksum = MessageAscii.compute_LRC(buffer + encoded)

packet = bytearray()
packet.extend(self._start)
packet.extend(f"{message.slave_id:02x}{message.function_code:02x}".encode())
packet.extend(b2a_hex(encoded))
packet.extend(f"{checksum:02x}".encode())
packet.extend(self._end)
packet = bytes(packet).upper()

data = message.function_code.to_bytes(1,'big') + encoded
packet_new = self.message_handler.encode(data, message.slave_id, message.transaction_id)
assert packet == packet_new, "ASCII FRAMER BuildPacket failed!"
data = message.function_code.to_bytes(1,'big') + message.encode()
packet = self.message_handler.encode(data, message.slave_id, message.transaction_id)
return packet


# __END__
3 changes: 0 additions & 3 deletions pymodbus/framer/binary_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,3 @@ def _preflight(self, data):
array.append(item)
array.append(item)
return bytes(array)


# __END__
12 changes: 2 additions & 10 deletions pymodbus/framer/rtu_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,8 @@ def buildPacket(self, message):
:param message: The populated request/response to send
"""
data = message.encode()
packet = (
struct.pack(RTU_FRAME_HEADER, message.slave_id, message.function_code)
+ data
)
packet += struct.pack(">H", MessageRTU.compute_CRC(packet))

data_new = message.function_code.to_bytes(1, 'big') + data
packet_new = self.message_handler.encode(data_new, message.slave_id, message.transaction_id)
assert packet == packet_new, "RTU FRAMER BuildPacket failed!"
data = message.function_code.to_bytes(1, 'big') + message.encode()
packet = self.message_handler.encode(data, message.slave_id, message.transaction_id)

# Ensure that transaction is actually the slave id for serial comms
if message.slave_id:
Expand Down
16 changes: 2 additions & 14 deletions pymodbus/framer/socket_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,6 @@ def buildPacket(self, message):
:param message: The populated request/response to send
"""
data = message.encode()
packet = struct.pack(
SOCKET_FRAME_HEADER,
message.transaction_id,
message.protocol_id,
len(data) + 2,
message.slave_id,
message.function_code,
)
packet += data

data_new = message.function_code.to_bytes(1, 'big') + data
packet_new = self.message_handler.encode(data_new, message.slave_id, message.transaction_id)
assert packet == packet_new, "SOCKET FRAMER BuildPacket failed!"
data = message.function_code.to_bytes(1, 'big') + message.encode()
packet = self.message_handler.encode(data, message.slave_id, message.transaction_id)
return packet
12 changes: 2 additions & 10 deletions pymodbus/framer/tls_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ def buildPacket(self, message):
:param message: The populated request/response to send
"""
data = message.encode()
packet = struct.pack(TLS_FRAME_HEADER, message.function_code)
packet += data

data_new = message.function_code.to_bytes(1,'big') + data
packet_new = self.message_encoder.encode(data_new, message.slave_id, message.transaction_id)
assert packet == packet_new, "TLS FRAMER BuildPacket failed!"
data = message.function_code.to_bytes(1,'big') + message.encode()
packet = self.message_encoder.encode(data, message.slave_id, message.transaction_id)
return packet


# __END__

0 comments on commit f63a4eb

Please sign in to comment.