Skip to content

address offset with server with callback #2626

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
elsamuko opened this issue Apr 7, 2025 · 1 comment
Closed

address offset with server with callback #2626

elsamuko opened this issue Apr 7, 2025 · 1 comment

Comments

@elsamuko
Copy link

elsamuko commented Apr 7, 2025

When I run a server/client example:

#!/usr/bin/env python3

import multiprocessing as mp
import time

from pymodbus.client import ModbusTcpClient
from pymodbus.datastore import (ModbusDeviceContext, ModbusSequentialDataBlock,
                                ModbusServerContext)
from pymodbus.server import StartTcpServer


class CallbackDataBlock(ModbusSequentialDataBlock):
    def setValues(self, address, value):
        print(f"Callback from setValues with address {address}, value {value}")
        super().setValues(address, value)


def server():
    PORT = 1234
    block = CallbackDataBlock(0x00, [0]*10)
    store = ModbusDeviceContext(di=block, co=block, hr=block, ir=block)
    context = ModbusServerContext(devices=store, single=True)
    print(f"Starting server 127.0.0.1:{PORT}")
    StartTcpServer(context=context, address=("", PORT))


def client():
    with ModbusTcpClient(host='127.0.0.1', port=1234) as client:
        print("Writing to address 4")
        client.write_coil(address=4, value=True)


if __name__ == "__main__":
    p_server = mp.Process(target=server)
    p_server.start()
    time.sleep(1)

    p_client = mp.Process(target=client)
    p_client.start()
    time.sleep(1)

    p_server.terminate()
    p_server.kill()
    p_client.join()

I'd expect:

Writing to address 4
Callback from setValues with address 4, value [True]

but I'm getting a

Writing to address 4
Callback from setValues with address 5, value [True]

What could be the issue here?

@janiversen
Copy link
Collaborator

There are no issue, this is how ModbusSequentialDataBlock works for historical reasons.

ModbusSequentialDataBlock have a difference between "addressing" a value and "defining" the value.

The simulator as well as the upcoming datastore have addressing == defining.

@janiversen janiversen closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2025
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