Skip to content

convert_from_registers wrong transformation #2623

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

Closed
milannvidia opened this issue Apr 3, 2025 · 3 comments
Closed

convert_from_registers wrong transformation #2623

milannvidia opened this issue Apr 3, 2025 · 3 comments

Comments

@milannvidia
Copy link

print(client.convert_from_registers(registers=[0,8194], data_type=client.DATATYPE.BITS, word_order="big"))

[False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, False, False, False, True, False, False, False, False, False, False]

whilst it should give

[False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, True, False]

Is this a bug?

@janiversen
Copy link
Collaborator

Sounds like a bug, but why do you want to convert bits ??? when you use e.g. read_coils it is already unpacked.

Pull requests are welcome.

A single register show the same problem (from dev):

>>> print(client.convert_from_registers(registers=[8194], data_type=client.DATATYPE.BITS))
[False, False, False, False, False, True, False, False, False, True, False, False, False, False, False, False]

@janiversen
Copy link
Collaborator

Bug confirmed:

>>> from pymodbus.utilities import pack_bitstring, unpack_bitstring
>>> 
>>> a = int.to_bytes(1409, 2, "big")
>>> print(a)
b'\x05\x81'
>>> b = unpack_bitstring(a)
>>> print(b)
[True, False, True, False, False, False, False, False, True, False, False, False, False, False, False, True]
>>> c = [True] + [False]*6 + [True, True, False, True] + [False]*5
>>> d = pack_bitstring(c)
>>> print(d)
b'\x81\x05'
>>> 

This is really a very very old bug (since v1.something). Good catch.

@janiversen
Copy link
Collaborator

janiversen commented Apr 7, 2025

#2627 solves the problem. Thanks for catching it, it was really a deeply buried bug and most likely affect a number of apps using pymodbus.

Extensive testing have been added to ensure it does not happen again.

It will be release soon as v3.9.0

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

No branches or pull requests

2 participants