Skip to content

Commit f7e3c0c

Browse files
committed
feat(modbus): disconnect and drop transactions on modbus timeout
1 parent 38df659 commit f7e3c0c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Inverter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ bool Inverter::update() {
2121
this->_modbus.connect(this->ipAddress, this->port);
2222
} else {
2323
this->isConnected = true;
24-
auto cb = [this](Modbus::ResultCode event, uint16_t transactionId, void* data) -> bool {
24+
auto cb = [this](Modbus::ResultCode event, uint16_t transactionId, void *data) -> bool {
25+
// Modified example:
26+
// https://github.com/emelianov/modbus-esp8266/blob/master/examples/Callback/Transactional/Transactional.ino#L40-L48
2527
if (event != Modbus::EX_SUCCESS) {
2628
Serial.printf("Modbus error [%d]: %02X\n", transactionId, event);
2729
}
30+
if (event == Modbus::EX_TIMEOUT) {
31+
this->_modbus.disconnect(this->ipAddress);
32+
this->_modbus.dropTransactions();
33+
}
2834
return true;
2935
};
3036
this->_modbus.readHreg(this->ipAddress, 37004, (uint16_t *) &this->_batteryStateOfChargeRaw, 1, cb, this->modbusUnit);

0 commit comments

Comments
 (0)