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
497 stars 182 forks source link

How to add exeptions to RTU SLAVE #354

Open MarioGG7 opened 1 month ago

MarioGG7 commented 1 month ago

mb.slave(SLAVE_ID); mb.addHreg(REGN); mb.Hreg(REGN, 100); ....................... if(REGN > 150){"send exeption -3 illegal value"}; // how to do this? .............. }

void loop() { mb.task(); yield(); }

emelianov commented 4 weeks ago

Is not exactly clear at which point you want to get exception. As of Modbus specification EX_ILLEGAL_ADDRESS is returned to master on non-existent address. If you want to override this behaviour you can use onRaw() callback but much easier is to modify the library code for this specific case. If you want to block addReg() from adding registers on some condition you have to inherit own class from ModbusRTU and override addReg() implementation.