From ee9b26ecfa86fa33dc374f19d7779e9bc066ac7b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 12 Aug 2018 16:15:03 +0200 Subject: [PATCH] 6.1.1.5 - Fix Pow R2 and S31 Fix some Pow R2 and S31 checksum errors using optimized re-sync --- sonoff/_changelog.ino | 7 ++++-- sonoff/sonoff_version.h | 2 +- sonoff/xdrv_03_energy.ino | 50 +++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 1aa01acaf..b985976c8 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,6 +1,9 @@ -/* 6.1.1.4 +/* 6.1.1.5 + * Fix some Pow R2 and S31 checksum errors using optimized re-sync + * + * 6.1.1.4 * Change version representation from 1.1.1a to 1.1.1.1 for better change reference - * + * * 6.1.1c * Add iFan02 Fanspeed + and Fanspeed - command options (#3415) * Fix some Pow R2 and S31 checksum errors (#3425) diff --git a/sonoff/sonoff_version.h b/sonoff/sonoff_version.h index 9ce29bcea..534ed4d7c 100644 --- a/sonoff/sonoff_version.h +++ b/sonoff/sonoff_version.h @@ -20,6 +20,6 @@ #ifndef _SONOFF_VERSION_H_ #define _SONOFF_VERSION_H_ -#define VERSION 0x06010104 +#define VERSION 0x06010105 #endif // _SONOFF_VERSION_H_ diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index 5fef4631e..8a5e38eff 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -307,7 +307,6 @@ void CseReceived() // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // 55 5A 02 F7 60 00 03 AB 00 40 10 02 60 5D 51 A6 58 03 E9 EF 71 0B 7A 36 // Hd Id VCal---- Voltage- ICal---- Current- PCal---- Power--- Ad CF--- Ck - AddLogSerial(LOG_LEVEL_DEBUG_MORE); uint8_t header = serial_in_buffer[0]; if ((header & 0xFC) == 0xFC) { @@ -315,14 +314,6 @@ void CseReceived() return; } - // Calculate checksum - uint8_t checksum = 0; - for (byte i = 2; i < 23; i++) checksum += serial_in_buffer[i]; - if (checksum != serial_in_buffer[23]) { - AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: " D_CHECKSUM_FAILURE)); - return; - } - // Get chip calibration data (coefficients) and use as initial defaults if (HLW_UREF_PULSE == Settings.energy_voltage_calibration) { long voltage_coefficient = 191200; // uSec @@ -392,24 +383,33 @@ bool CseSerialInput() if (cse_receive_flag) { serial_in_buffer[serial_in_byte_counter++] = serial_in_byte; if (24 == serial_in_byte_counter) { - CseReceived(); - cse_receive_flag = 0; - return 1; + AddLogSerial(LOG_LEVEL_DEBUG_MORE); + uint8_t checksum = 0; + for (byte i = 2; i < 23; i++) { checksum += serial_in_buffer[i]; } + if (checksum == serial_in_buffer[23]) { + CseReceived(); + cse_receive_flag = 0; + return 1; + } else { + AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: " D_CHECKSUM_FAILURE)); + do { // Sync buffer with data (issue #1907 and #3425) + memmove(serial_in_buffer, serial_in_buffer +1, 24); + serial_in_byte_counter--; + } while ((serial_in_byte_counter > 1) && (0x5A != serial_in_buffer[1])); + if (0x5A == serial_in_buffer[1]) { + AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: Fixed out-of-sync")); + serial_in_byte_counter++; + } else { + cse_receive_flag = 0; + serial_in_byte_counter = 0; + } + } } } else { - if ((0x5A == serial_in_byte) && (serial_in_byte_counter)) { // 0x5A - Packet header 2 - if ((0x55 == serial_in_buffer[serial_in_byte_counter -1]) || - (0xAA == serial_in_buffer[serial_in_byte_counter -1]) || - (serial_in_buffer[serial_in_byte_counter -1] > 0xF0)) { - if (serial_in_byte_counter > 1) { // Sync buffer with data (issue #1907 and #3425) - serial_in_buffer[0] = serial_in_buffer[--serial_in_byte_counter]; - serial_in_byte_counter = 1; - AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: Fixed out-of-sync")); - } - cse_receive_flag = 1; - } else { - serial_in_byte_counter = 0; - } + if ((0x5A == serial_in_byte) && (1 == serial_in_byte_counter)) { // 0x5A - Packet header 2 + cse_receive_flag = 1; + } else { + serial_in_byte_counter = 0; } serial_in_buffer[serial_in_byte_counter++] = serial_in_byte; }