On the receiver, it states the checksum is calculated as as the sum of all bytes, however i'm calculating different values then the receiver is calculating. I thought perhaps it could be because of byte overflow?
Can anyone point out what i am doing wrong?
byte chk = 0;
for ( int i=0; i<length; i++)
chk += data[i];
(b) indicates number of information bytes contained in field (e) = 10h = 16 byte
(c) indicates the starting location of RAM space into which the data byte to be stored
(d) indicates (if 00) not the end-of-file; more frames are to arrive.
(e) indicates information (data) bytes contained in the current frame,
(f) indicated CHKSUM. It is calculated in the following way:
all data bytes from and including field (b) to field (e) are added. The carry is discarded. Two's complement of the remaining 8-bit is taken as CHECKSUM, and it is transmitted as the last byte of the frame.
//---------------------------------------------------------------------------------------------------------------- (1) The frame contains 22 bytes data including all fields.
(2) Field-(a) and Field-(f) will not be taken into account to calculate CHKSUM.
(3) Your codes:
byte chk = 0;
for ( int i=0; i<length; i++) //length = 19 (22-02 = 20 - 1)
chk += data[i];
//sum=10+C1+00+00+00+3E+90+32+00+21+16+10+1E+11+01+33+C1+0A+CD+19 = 042C
//discard accumulated carries; we get 2C = 0010 1100
//take 2's complemet code : 1101 0011 + 1 = D4
//----take the 2's complement of chk; you will get D4-----
chk = (chk^0xFF); //chk = D3
chk +=chk+01; //chk = D4