Page 1 of 1

ESP32 CALCULATION INVALID

Posted: Sat May 17, 2025 11:49 pm
by tomero
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);
}

Re: ESP32 CALCULATION INVALID

Posted: Sun May 18, 2025 10:58 am
by Sprite
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).

Re: ESP32 CALCULATION INVALID

Posted: Sun May 18, 2025 10:53 pm
by MicroController
To that end,

Code: Select all

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

Re: ESP32 CALCULATION INVALID

Posted: Mon May 19, 2025 7:33 am
by tomero
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:

Re: ESP32 CALCULATION INVALID

Posted: Wed May 21, 2025 5:47 am
by Sprite
@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?

Re: ESP32 CALCULATION INVALID

Posted: Wed May 21, 2025 11:56 am
by tomero
@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