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

Simplify some BinaryPayload pack operations #2224

Merged
merged 1 commit into from
Jun 28, 2024

Conversation

jameshilliard
Copy link
Contributor

Slightly cleaner and easier to read.

@jameshilliard jameshilliard force-pushed the binary-payload branch 13 times, most recently from 28448cc to 531cddf Compare June 27, 2024 00:00
Copy link
Collaborator

@janiversen janiversen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest it is quite diffecult to see if the changes have any side effects.

This needed to be tested extra in the test harness, please do "protest --cov" and check that all code is covered.

@@ -182,3 +186,11 @@ def hexlify_packets(packet):
if not packet:
return ""
return " ".join([hex(int(x)) for x in packet])


def system_endian():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong, because byte order can be different from word order.

It seems wrong that we add endian utilities, we should avoid that.

Copy link
Contributor Author

@jameshilliard jameshilliard Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the issue here is that the array class which I'm using (because it provides a nice way to operate on 16 bit integer lists as bytes) encodes integers with the system native byte format. In pymodbus AFAIU our registers list is encoded as a list of big endian integers so when converting from a register list to a python array(which has a convenient bytes method) we have to conditionally byteswap when converting if our native system byteformat is little endian so that the individual python array elements get ordered as big endian bytes. So this system endianness check is only actually being used when encoding/decoding the list of integers to/from bytes. We don't need to care about word order at this point either since we don't change word order when doing the register list to array to bytes conversions.

Once we are actually encoding/decoding integers i.e. in _pack_words/_unpack_words we can simply assume we are already big endian format and perform the byteswaping and wordswapping operations without having to care about system native byte format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up just removing all the system native endian dependent array operations for now as it's a bit difficult to test without a big endian system.

@jameshilliard
Copy link
Contributor Author

This needed to be tested extra in the test harness, please do "protest --cov" and check that all code is covered.

I went ahead and removed the system endian dependent conversion operations, all these changes now should have full coverage.

Copy link
Collaborator

@janiversen janiversen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@janiversen janiversen merged commit 5e146c0 into pymodbus-dev:dev Jun 28, 2024
1 check passed
@jameshilliard jameshilliard deleted the binary-payload branch June 28, 2024 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants