Skip to content
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

Remove control encode in framers #2095

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__