-
Notifications
You must be signed in to change notification settings - Fork 949
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
Convert endianness #2506
Merged
+72
−33
Merged
Convert endianness #2506
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a6436e6
added support for endiannes in conversion functions
daanwtb 72591cc
python to_byte only accepts string so convert endianness to string
daanwtb ee6c03e
Merge branch 'dev' into convert_endianness
daanwtb 4d5706b
actions failed: using literals for endian instead
daanwtb b9ff989
Merge branch 'convert_endianness' of https://github.com/daanwtb/pymod…
daanwtb b6adab0
changed to word_order and byte_order
daanwtb 04e9512
Remove byte_order.
janiversen c05127d
Correct convert_to/from word_order.
janiversen 6ef1fde
Fix CI.
janiversen e6061ab
mypy 3.9 !!
janiversen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
import struct | ||
from abc import abstractmethod | ||
from enum import Enum | ||
from typing import Generic, TypeVar | ||
from typing import Generic, TypeVar, Literal | ||
|
||
import pymodbus.pdu.bit_message as pdu_bit | ||
import pymodbus.pdu.diag_message as pdu_diag | ||
|
@@ -695,18 +695,25 @@ class DATATYPE(Enum): | |
|
||
@classmethod | ||
def convert_from_registers( | ||
cls, registers: list[int], data_type: DATATYPE | ||
cls, registers: list[int], data_type: DATATYPE, byte_order: Literal["big", "little"] = "big", | ||
word_order: Literal["big", "little"] = "big" | ||
) -> int | float | str | list[bool]: | ||
"""Convert registers to int/float/str. | ||
|
||
:param registers: list of registers received from e.g. read_holding_registers() | ||
:param data_type: data type to convert to | ||
:param byte_order: Literal[big, small] order of to bytes in 16 bit register encoding (ALMOST always big) | ||
(default is big) | ||
:param word_order: Literal[big, small] order of words (registers) when encoding in multiple registers | ||
(default is big) | ||
:returns: int, float, str or list[bool] depending on "data_type" | ||
:raises ModbusException: when size of registers is not 1, 2 or 4 | ||
""" | ||
byte_list = bytearray() | ||
for x in registers: | ||
byte_list.extend(int.to_bytes(x, 2, "big")) | ||
byte_list.extend(int.to_bytes(x, 2, byte_order)) | ||
if word_order == "little": | ||
byte_list = byte_list[::-1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not convert the order of all registers. |
||
if data_type == cls.DATATYPE.STRING: | ||
# remove trailing null bytes | ||
trailing_nulls_begin = len(byte_list) | ||
|
@@ -725,15 +732,22 @@ def convert_from_registers( | |
|
||
@classmethod | ||
def convert_to_registers( | ||
cls, value: int | float | str | list[bool], data_type: DATATYPE | ||
cls, value: int | float | str | list[bool], data_type: DATATYPE, byte_order: Literal["big", "little"] = "big", | ||
word_order: Literal["big", "little"] = "big" | ||
) -> list[int]: | ||
"""Convert int/float/str to registers (16/32/64 bit). | ||
|
||
:param value: value to be converted | ||
:param data_type: data type to be encoded as registers | ||
:param byte_order: Literal[big, small] order of to bytes in 16 bit register encoding (ALMOST always big) | ||
(default is big) | ||
:param word_order: Literal[big, small] order of words (registers) when encoding in multiple registers | ||
(default is big) | ||
:returns: List of registers, can be used directly in e.g. write_registers() | ||
:raises TypeError: when there is a mismatch between data_type and value | ||
""" | ||
if word_order == "little" and isinstance(value, list): | ||
value = value[::-1] | ||
if data_type == cls.DATATYPE.BITS: | ||
if not isinstance(value, list): | ||
raise TypeError(f"Value should be string but is {type(value)}.") | ||
|
@@ -749,7 +763,7 @@ def convert_to_registers( | |
else: | ||
byte_list = struct.pack(f">{data_type.value[0]}", value) | ||
regs = [ | ||
int.from_bytes(byte_list[x : x + 2], "big") | ||
int.from_bytes(byte_list[x : x + 2], byte_order) | ||
for x in range(0, len(byte_list), 2) | ||
] | ||
return regs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Byte order is fixed by the standard.