[Solved] Add callback functions to SimpleModbus library

EDIT: Just as I typed this out... I realized I had put the function pointer into the wrong function the whole time.. I added it to modbus_configure, which would have of course required it to be saved in the local scope if I want to call it from inside modbus_update()- I just added it to modbus_update() now, where it makes more sense, and also works.


Hi,

I want to add callback functions to the SimpleModbus library, in order to call functions defined in my sketch from within the modbus_update() loop (e.g. when a "reading registers" command is received, change the values of a register before the reply is sent back).

After googling for function pointers (e.g. here, there), I tried changing the definition of modbus_configure in the cpp and header files like this:

typedef void (*callback)(int);
void modbus_configure(HardwareSerial *SerialPort,long baud, unsigned char byteFormat, unsigned char _slaveID,
                      unsigned char _TxEnablePin, unsigned int _holdingRegsSize, unsigned int* _regs, callback cb)

And added my callbackfunction to the parameters in the call in setup(). However, the compiler is not having it - I get the following error (from the call to cb() inside SimpleModbus.ccp):

SimpleModbusSlave.cpp:x: error: ‘cb’ was not declared in this scope

What am I doing wrong?

Hola, buenas:

Soy nuevo en esto y estoy intentando comunicar un arduino con una pantalla tactil HMI de Schneider eletric metiante un max485. Alguien me puede echar una mano.