From 4d861c17525129d58f4301c346918709f1703ee2 Mon Sep 17 00:00:00 2001 From: Norbert Richter Date: Mon, 26 Jul 2021 09:32:58 +0200 Subject: [PATCH] Fix NeoPool communication error --- tasmota/xsns_83_neopool.ino | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/tasmota/xsns_83_neopool.ino b/tasmota/xsns_83_neopool.ino index ff4bb17d9..30517ea24 100644 --- a/tasmota/xsns_83_neopool.ino +++ b/tasmota/xsns_83_neopool.ino @@ -1029,13 +1029,29 @@ void NeoPoolLogRW(const char *name, uint16_t addr, uint16_t *data, uint16_t cnt) #endif // DEBUG_TASMOTA_SENSOR +void NeoPool250msSetStatus(bool status) +{ + neopool_poll = status; + + if (!status) { + // clear rec buffer from possible prev periodical communication + uint32_t timeoutMS = millis() + 100 * NEOPOOL_READ_TIMEOUT; // Max delay before we timeout + while (NeoPoolModbus->available() && millis() < timeoutMS) { + NeoPoolModbus->read(); + SleepDelay(0); + } + + } +} + + uint8_t NeoPoolReadRegister(uint16_t addr, uint16_t *data, uint16_t cnt) { bool data_ready; uint32_t timeoutMS; uint16_t *origin = data; - neopool_poll = false; + NeoPool250msSetStatus(false); *data = 0; NeoPoolModbus->Send(NEOPOOL_MODBUS_ADDRESS, NEOPOOL_READ_REGISTER, addr, cnt); @@ -1049,14 +1065,14 @@ uint8_t NeoPoolReadRegister(uint16_t addr, uint16_t *data, uint16_t cnt) #ifdef DEBUG_TASMOTA_SENSOR AddLog(LOG_LEVEL_DEBUG, PSTR("NEO: addr 0x%04X read data error %d"), addr, error); #endif // DEBUG_TASMOTA_SENSOR - neopool_poll = true; + NeoPool250msSetStatus(true); free(buffer); return NEOPOOL_MODBUS_ERROR_RW_DATA; } for(uint64_t i = 0; i < cnt; i++) { *data++ = (buffer[i*2+3] << 8) | buffer[i*2+4]; } - neopool_poll = true; + NeoPool250msSetStatus(true); delay(2); free(buffer); #ifdef DEBUG_TASMOTA_SENSOR @@ -1072,7 +1088,7 @@ uint8_t NeoPoolReadRegister(uint16_t addr, uint16_t *data, uint16_t cnt) #ifdef DEBUG_TASMOTA_SENSOR AddLog(LOG_LEVEL_DEBUG, PSTR("NEO: addr 0x%04X read data timeout"), addr); #endif // DEBUG_TASMOTA_SENSOR - neopool_poll = true; + NeoPool250msSetStatus(true); return NEOPOOL_MODBUS_ERROR_TIMEOUT; } @@ -1087,7 +1103,7 @@ uint8_t NeoPoolWriteRegister(uint16_t addr, uint16_t *data, uint16_t cnt) #ifdef DEBUG_TASMOTA_SENSOR NeoPoolLogRW("NeoPoolWriteRegister", addr, data, cnt); #endif // DEBUG_TASMOTA_SENSOR - neopool_poll = false; + NeoPool250msSetStatus(false); numbytes = 7+cnt*2; frame = (uint8_t*)malloc(numbytes+2); if (nullptr == frame) { @@ -1127,7 +1143,7 @@ uint8_t NeoPoolWriteRegister(uint16_t addr, uint16_t *data, uint16_t cnt) #ifdef DEBUG_TASMOTA_SENSOR AddLog(LOG_LEVEL_DEBUG, PSTR("NEO: addr 0x%04X write data response error %d"), addr, error); #endif // DEBUG_TASMOTA_SENSOR - neopool_poll = true; + NeoPool250msSetStatus(true); return NEOPOOL_MODBUS_ERROR_RW_DATA; } if (9 == error) { @@ -1136,7 +1152,7 @@ uint8_t NeoPoolWriteRegister(uint16_t addr, uint16_t *data, uint16_t cnt) NeoPoolModbus->read(); } } - neopool_poll = true; + NeoPool250msSetStatus(true); delay(2); if (MBF_SAVE_TO_EEPROM == addr) { // EEPROM write can take some time, wait until device is ready @@ -1149,7 +1165,7 @@ uint8_t NeoPoolWriteRegister(uint16_t addr, uint16_t *data, uint16_t cnt) #ifdef DEBUG_TASMOTA_SENSOR AddLog(LOG_LEVEL_DEBUG, PSTR("NEO: addr 0x%04X write data response timeout"), addr); #endif // DEBUG_TASMOTA_SENSOR - neopool_poll = true; + NeoPool250msSetStatus(true); return NEOPOOL_MODBUS_ERROR_TIMEOUT; }