Fix NeoPool communication error

This commit is contained in:
Norbert Richter 2021-07-26 09:32:58 +02:00
parent 6f3878c9bc
commit 4d861c1752
No known key found for this signature in database
GPG Key ID: 6628701A626FA674
1 changed files with 24 additions and 8 deletions

View File

@ -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;
}