Hooks and/or notifications for writes to mapping block

28 views
Skip to first unread message

Memet B

unread,
Nov 17, 2021, 8:04:15 AM11/17/21
to libmodbus
Hello,

I'm using libmodbus on an embedded system, and I'd like to be able to get a callback of exactly what registers have been modified on a slave/server node so that I can avoid scanning the whole block for changes.

Is this doable with the current API?

I can see there was a thread back in 2013 that didn't result in any changes in the mainline code (https://groups.google.com/g/libmodbus/c/JSqbgYtgxxA/m/yhH9WqlEsIIJ)

This could be as simple as having a notification callback with start address and length since the Modbus protocol only allows for contiguous writes anyways.

The solution I see would be to add a default parameter(s) to modbus_mapping_new or modbus_mapping_new_start_address, and when a write command is sent, the mapping calls a notification function with the start address and the length of the write.

Thank you.


Memet B

unread,
Nov 17, 2021, 9:17:52 AM11/17/21
to libmodbus
For reference, here's an example implementation:


Usage:

```
    modbus_mapping_t* mb_mapping = modbus_mapping_new_start_address_ex(0, MODBUS_MAX_READ_BITS,
                                                                       0, 0, nullptr,
                                                                       0, 0,
                                                                       0, MODBUS_MAX_READ_REGISTERS, [](int a, int b){ std::clog << "NOTIFICATION: " << a << ": " << b <<std::endl; });

```

Memet B

unread,
Nov 17, 2021, 1:15:24 PM11/17/21
to libmodbus
Correction: slightly updated call signature with additional context variable:

    modbus_mapping_t* mb_mapping = modbus_mapping_new_start_address_ex(0, MODBUS_MAX_READ_BITS,
                                                                       0, 0, nullptr,
                                                                       0, 0,
                                                                       0, MODBUS_MAX_READ_REGISTERS, [](int a, int b, void* ctx){ std::clog << "NOTIFICATION: " << a << ": " << b <<std::endl; });
Reply all
Reply to author
Forward
0 new messages