Arduino Modbus Library read/write callbacks?

I'm using the Arduino Modbus library: ArduinoModbus - Arduino Reference
to communicate between two Arduino over Rs485.
One is the RTU client, and the other is the RTU server.

Using the example code on GitHub I can get the master to read/write data to the holdRegisters on the server.

I am now trying to trigger a callback function on the server whenever the client has changed or read a holding register value. Is that possible? I don't see it in the documentation but maybe I am missing something?
Or can anyone recommend a better Modbus library for Arduino?

The server uses the .poll() function to receive messages. Once one had been read and decoded, you can call any code you want based on what you read. Or am I not understanding what you mean by callback function? A server does not send a message on its own.

Just did a modbus slave unit.
ended up using ModbusRTUSlave.
liked how it was implemented, using all callbacks like you mentioned.
The unit I did..

might be worth a peek.

For me there are several problems with doing something after .poll().
First, you don't know which register(s) the client read or wrote to.
Then you can't control the response the server gives, which can be convenient in many cases.
And last when not continuing with the next .poll() right away the server will become unresponsive to new requests that come in until the next .poll() call.

I'm looking for something similar to what the eModbus library does for esp32. See the example here: eModbus/main.cpp at master · eModbus/eModbus · GitHub

you can check my Modbus Server library:
https://werner.rothschopf.net/microcontroller/202112_arduino_modbus_server.htm

on incoming requests callbacks are called specific for each function code.
Furthermore there is one callback for "any" client activity.

See also the doc folder of the library.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.