Hi, can someone please help me with calc checksum ? Iam new with communicating over uart with protocols.
I have working sending request packet, and i get an answer from BMS. I can decode all data, but iam stuck on checksum, i tried internet calculators etc. And i didnt get same result as is received...
I dont know eather how is checksum calculated, because chineese documentation is low :D...
I sent reqest : 5A5A00000000.
Response from BMS is :
AA55AAFF025D0EC50EC50EC40EC50EC50EC50EC50EC50EC60EC50EC50EC50EC50EC50EC40EC40000000000000000000000000000000000000000000000000000000000000000000000003F003D09000026504100000389002CE55A0019001C00180017FFD8FFD8010100000000000000000000090EC6030EC40EC4100000007F007B02AD0000000000001911
First 4 bytes are header :AA55AAFF, then is value of first cell 025D - high byte first, and continous to 2 cell value etc as in PDF.
But last bytes two bytes 1911 is checksum. And i dont know how to calculate it.
In datasheet is something like first 4 bytes (header) are not present in checksum ?
thanks for help
140byteprotocol.pdf (346.5 KB)
gcjr
May 7, 2022, 11:24am
2
seems there may be useful information re: checksum on the right side of the pdf that has been cut off
and where is "Remark 4"?
gcjr
May 7, 2022, 11:30am
3
looks like it's the sum of bytes, skipping the first 4 bytes and not including the 2 byte checksum
try this
unsigned char data[]="AA55AAFF025D0EC50EC50EC40EC50EC50EC50EC50EC50EC60EC50"\
"EC50EC50EC50EC50EC40EC4000000000000000000000000000000000000000000000000000000000"\
"0000000000000003F003D09000026504100000389002CE55A0019001C00180017FFD8FFD8010100000000000000000000090EC6030EC40EC4100000007F007B02AD0000000000001911";
int main(void) {
printf("%s\n", data);
unsigned short checksum=0;
for(int i=8; i<strlen(data)-4;i+=2) {
char d[5]={0};
memcpy(d, &data[i],2);
printf("string %s\n", d);
char text[20];
unsigned short int hexvalue;
sscanf(d, "%2x",&hexvalue);
// hexvalue &= 0xffff;
checksum+=hexvalue;
printf("hex value %0x %0x\n", hexvalue, checksum);
}
printf("checksum %x\n", checksum);
//checksun=
printf("checksum %0x\n", checksum);
}
a run gives
AA55AAFF025D0EC50EC50EC40EC50EC50EC50EC50EC50EC60EC50EC50EC50EC50EC50EC40EC40000000000000000000000000000000000000000000000000000000000000000000000003F003D09000026504100000389002CE55A0019001C00180017FFD8FFD8010100000000000000000000090EC6030EC40EC4100000007F007B02AD0000000000001911
string 02
hex value 2 2
string 5D
hex value 5d 5f
string 0E
hex value e 6d
string C5
hex value c5 132
string 0E
hex value e 140
string C5
hex value c5 205
string 0E
hex value e 213
string C4
hex value c4 2d7
string 0E
hex value e 2e5
string C5
hex value c5 3aa
string 0E
hex value e 3b8
string C5
hex value c5 47d
string 0E
hex value e 48b
string C5
hex value c5 550
string 0E
hex value e 55e
string C5
hex value c5 623
string 0E
hex value e 631
string C5
hex value c5 6f6
string 0E
hex value e 704
string C6
hex value c6 7ca
string 0E
hex value e 7d8
string C5
hex value c5 89d
string 0E
hex value e 8ab
string C5
hex value c5 970
string 0E
hex value e 97e
string C5
hex value c5 a43
string 0E
hex value e a51
string C5
hex value c5 b16
string 0E
hex value e b24
string C5
hex value c5 be9
string 0E
hex value e bf7
string C4
hex value c4 cbb
string 0E
hex value e cc9
string C4
hex value c4 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 00
hex value 0 d8d
string 3F
hex value 3f dcc
string 00
hex value 0 dcc
string 3D
hex value 3d e09
string 09
hex value 9 e12
string 00
hex value 0 e12
string 00
hex value 0 e12
string 26
hex value 26 e38
string 50
hex value 50 e88
string 41
hex value 41 ec9
string 00
hex value 0 ec9
string 00
hex value 0 ec9
string 03
hex value 3 ecc
string 89
hex value 89 f55
string 00
hex value 0 f55
string 2C
hex value 2c f81
string E5
hex value e5 1066
string 5A
hex value 5a 10c0
string 00
hex value 0 10c0
string 19
hex value 19 10d9
string 00
hex value 0 10d9
string 1C
hex value 1c 10f5
string 00
hex value 0 10f5
string 18
hex value 18 110d
string 00
hex value 0 110d
string 17
hex value 17 1124
string FF
hex value ff 1223
string D8
hex value d8 12fb
string FF
hex value ff 13fa
string D8
hex value d8 14d2
string 01
hex value 1 14d3
string 01
hex value 1 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 00
hex value 0 14d4
string 09
hex value 9 14dd
string 0E
hex value e 14eb
string C6
hex value c6 15b1
string 03
hex value 3 15b4
string 0E
hex value e 15c2
string C4
hex value c4 1686
string 0E
hex value e 1694
string C4
hex value c4 1758
string 10
hex value 10 1768
string 00
hex value 0 1768
string 00
hex value 0 1768
string 00
hex value 0 1768
string 7F
hex value 7f 17e7
string 00
hex value 0 17e7
string 7B
hex value 7b 1862
string 02
hex value 2 1864
string AD
hex value ad 1911
string 00
hex value 0 1911
string 00
hex value 0 1911
string 00
hex value 0 1911
string 00
hex value 0 1911
string 00
hex value 0 1911
string 00
hex value 0 1911
checksum 1911
checksum 1911
checksum is 1911 hex
gcjr
May 7, 2022, 12:14pm
5
consider
void
calcByte (
uint8_t *buf,
int nByte )
{
printf ("\n%s: %d\n", __func__, nByte);
int sum = 0;
for (unsigned n = 4; n < nByte - 2; n++) {
sum += buf [n];
sum &= 0xffff;
// printf (" %3d 0x%04x 0x%04x\n", n, sum, buf [n]);
}
printf (" 0x%04x\n", sum);
}
arduino version of code
#include <string.h>
#include <stdio.h>
// function to calculate checksum
// skip first 8 bytes header - checksum is last 4 bytes
unsigned short checksum(byte *data) {
unsigned short checksum=0, hexvalue;;
for(int i=8; i<strlen(data)-4; i+=2) {
sscanf(&(data[i]), "%2x",&hexvalue);
checksum+=hexvalue;
}
return checksum;
}
void setup(void) {
Serial.begin(115200);
byte data[]="AA55AAFF025D0EC50EC50EC40EC50EC50EC50EC50EC50EC60EC50"
"EC50EC50EC50EC50EC40EC4000000000000000000000000000000000000000000000000000000000"
"0000000000000003F003D09000026504100000389002CE55A0019001C00180017FFD8FFD8010100000000000000000000090EC6030EC40EC4100000007F007B02AD0000000000001911";
Serial.print("checksum "); Serial.println(checksum(data), HEX);
}
void loop(void) {}
a run gives
checksum 1911
Thanks! its working, i had a same code as yours but then i was doing
sum = (sum^0xFFFF);
sum +=sum+01;
i found it somewhere on internet i wasnt expecting its so easy only sum of all bytes
system
Closed
November 3, 2022, 8:23pm
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.