emelianov / modbus-esp8266

Most complete Modbus library for Arduino. A library that allows your Arduino board to communicate via Modbus protocol, acting as a master, slave or both. Supports network transport (Modbus TCP) and Serial line/RS-485 (Modbus RTU). Supports Modbus TCP Security for ESP8266/ESP32.
Other
519 stars 186 forks source link

Ignore Callbacks when Register is Updated by Self #352

Closed seikosantana closed 4 months ago

seikosantana commented 4 months ago

Hi, is there a way to update register value without triggering the onSetCoil / onSetHreg? I am trying to integrate modbus into existing working program so that it can interface with modbus, but the problem is when I use onSetCoil/onSetHreg to handle incoming requests, and have my program to update the registers to represent the current state, it fires the callbacks too, causing recursive calls or long loops.

Do you have any tips or workarounds for this kind of situation? Thanks in advance

emelianov commented 4 months ago
mb.cbDisable();
mb.Hreg(REV, VALUE);
mb.cbEnable();

Or safer variant

bool saved_state = mb.cbEnable(false);
mb.Hreg(REV, VALUE);
mb.cbEnable(saved_state);
seikosantana commented 4 months ago

Is it safe to call those in a callback itself (inside an onSet)?

emelianov commented 4 months ago

Sure.

seikosantana commented 4 months ago

Thanks a lot for your response and effort on the library 😀