Fix SR04 serial checksum for AJ-SR04M (#25147)

The AJ-SR04M sends FF H L SUM with SUM = H + L (header excluded).
Accept that form as well as FF + H + L when the header byte is 0xFF.


Claude-Session: https://claude.ai/code/session_01Ma8u1on2wkeXraAzZBn2iM

Co-authored-by: Pacif13r <22977757+Pacif13r@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-16 16:13:59 +02:00
1 parent dad74600a1
commit 2289c619bb
1 file changed
+3 -1
+3 -1
View File
@@ -88,7 +88,9 @@ uint16_t Sr04TMode2Distance(uint32_t i) {
uint8_t crc = buffer[0];
crc += buffer[1];
crc += buffer[2];
if (crc == buffer[3]) { // Check crc sum
uint8_t crc_aj = buffer[1] + buffer[2]; // AJ-SR04M excludes the 0xFF header from its sum
if ((crc == buffer[3]) || // Check crc sum
((0xFF == buffer[0]) && (crc_aj == buffer[3]))) {
distance = (buffer[1] << 8) + buffer[2];
} else {
AddLog(LOG_LEVEL_ERROR, PSTR("SR4-%d: CRC error"), i+1);