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

Improve payload types #2057

Merged
merged 1 commit into from
Feb 25, 2024
Merged
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
15 changes: 6 additions & 9 deletions pymodbus/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
self._wordorder = wordorder
self._repack = repack

def _pack_words(self, fstring, value):
def _pack_words(self, fstring: str, value) -> bytes:
"""Pack words based on the word order and byte order.

# ---------------------------------------------- #
Expand All @@ -69,18 +69,15 @@ def _pack_words(self, fstring, value):
:return:
"""
value = pack(f"!{fstring}", value)
wordorder = WC.get(fstring.lower()) // 2
wordorder = WC.get(fstring.lower()) // 2 # type: ignore[operator]
upperbyte = f"!{wordorder}H"
payload = unpack(upperbyte, value)

if self._wordorder == Endian.LITTLE:
payload = list(reversed(payload))
payload = payload[::-1]

fstring = self._byteorder + "H"
payload = [pack(fstring, word) for word in payload]
payload = b"".join(payload)

return payload
return b"".join(pack(fstring, word) for word in payload)

def encode(self) -> bytes:
"""Get the payload buffer encoded in bytes."""
Expand Down Expand Up @@ -327,7 +324,7 @@ def fromCoils(
return cls(payload, byteorder)
raise ParameterException("Invalid collection of coils supplied")

def _unpack_words(self, fstring, handle):
def _unpack_words(self, fstring: str, handle) -> bytes:
"""Unpack words based on the word order and byte order.

# ---------------------------------------------- #
Expand All @@ -339,7 +336,7 @@ def _unpack_words(self, fstring, handle):
:param handle: Value to be unpacked
:return:
"""
wc_value = WC.get(fstring.lower()) // 2
wc_value = WC.get(fstring.lower()) // 2 # type: ignore[operator]
handle = unpack(f"!{wc_value}H", handle)
if self._wordorder == Endian.LITTLE:
handle = list(reversed(handle))
Expand Down