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

Reading coil data bits is reversed #1

Merged
merged 1 commit into from
Sep 2, 2023
Merged

Conversation

vladimir1284
Copy link
Owner

Fixes brainelectronics#38
Tested in hardware in a Modbus TCP server hosted in ESP32 connected to ScadaBr.

@vladimir1284 vladimir1284 merged commit 3dc7ec3 into develop Sep 2, 2023
@JustMe-NL
Copy link

JustMe-NL commented Nov 1, 2023

Ran into this when requesting 8 coils and upon investigating it looks like fix brainelectronics#38 only works when less then 16 coils are requested.

I made my own fix which should work regardless of how many coils are requested:

`
bitcounter = 0
for addr in range(request.register_addr,
request.register_addr + request.quantity):
value = reg_dict.get(addr, default_value)['val']

        if reg_type == 'COILS':
            # Insert the bits at the right position, per byte from right to left
            data.insert(int(bitcounter / 8) * 8, value)
            bitcounter += 1
        else:
            if isinstance(value, (list, tuple)):
                data.extend(value)
            else:
                data.append(value)
        
    if reg_type == 'COILS':
        if bitcounter % 8 > 0:
            # add padding if needed
            for i in range(8 - (bitcounter % 8)):
                data.insert(int(bitcounter / 8) * 8, False)

`

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.

Reading coil data bits is reversed
2 participants