ESP32 CALCULATION INVALID

tomero
Posts: 3
Joined: Sat May 17, 2025 11:43 pm

ESP32 CALCULATION INVALID

Postby tomero » Sat May 17, 2025 11:49 pm

I try write some code to calculate some bytes, but the result is invalid, result should be "0x28". Please help.

Code: Select all

void setup() {
  Serial.begin(115200);

  uint8_t data[] = {0x13, 0x00, 0x93, 0x00, 0x64, 0x09, 0xC4, 0x01, 0x7E, 0x8B, 0x01, 0x06, 0x00, 0x08, 0xE0, 0x02, 0x8E, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x02, 0x7E, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x02, 0x6E, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x02, 0x5E, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x02, 0x4E, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x02, 0x3E, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x02, 0x2E, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x02, 0x1E, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xF5, 0x00, 0xFB};
  uint8_t cs = CalcChecksum(data, sizeof(data));
  
  Serial.println(String(cs));
}

void loop() {}

uint8_t CalcChecksum(const uint8_t data[], const uint8_t dataLen)
{
  uint32_t cs = 0;
  for (uint16_t i = 0; i < dataLen; i++)
  {
    cs += data[i];
  }

  return (((cs ^ 0xFF) + 1) & 0xFF);
}

Sprite
Espressif staff
Espressif staff
Posts: 10609
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 CALCULATION INVALID

Postby Sprite » Sun May 18, 2025 10:58 am

That's what the code seems to generate here (quickly ported to normal C); what exactly are you getting? Note that you're outputting the value in decimal, so you're likely seeing 40 (which is the decimal equivalent of 0x28).

MicroController
Posts: 2668
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32 CALCULATION INVALID

Postby MicroController » Sun May 18, 2025 10:53 pm

To that end,

Code: Select all

Serial.println(cs, HEX);
may give a better result.

tomero
Posts: 3
Joined: Sat May 17, 2025 11:43 pm

Re: ESP32 CALCULATION INVALID

Postby tomero » Mon May 19, 2025 7:33 am

Hi all, thank you for your response.

I have solution by modification the formula of 2's complement:

Code: Select all

(((cs ^ 0xFF) + 1) & 0xFF);
To

Code: Select all

 ((~cs + 1) & 0xFF);
But im still confused, why its return different but in same purpose. :shock:

Sprite
Espressif staff
Espressif staff
Posts: 10609
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 CALCULATION INVALID

Postby Sprite » Wed May 21, 2025 5:47 am

@tomero Those two code fragments are equivalent, it doesn't change the calculation. And as far as we can see the code works fine. If you still do not think so, can you tell us what you expect, what you get, and why you think it is the wrong answer?

tomero
Posts: 3
Joined: Sat May 17, 2025 11:43 pm

Re: ESP32 CALCULATION INVALID

Postby tomero » Wed May 21, 2025 11:56 am

@tomero Those two code fragments are equivalent, it doesn't change the calculation. And as far as we can see the code works fine. If you still do not think so, can you tell us what you expect, what you get, and why you think it is the wrong answer?
Yes as you say that code is equivalent, and I try that on simulator like WOKWI, and direct board Arduino Uno. But I try it to direct board ESP32 it come random calculation every execute.

Just info, I have make application with BLUTOOTH communication serial, every communication will calculate checksum to avoid broken message. Now after I change the calculation formula, my problem is gone. :D

Who is online

Users browsing this forum: Semrush [Bot], trendictionbot and 4 guests