Checksum error

Hi,

I am having an error somewhere with this checksum, and I cannot see where I've gone wrong with my code.

void mdb_expansion(void) { **
** static uint8_t data[30] = {0,0,0,0,0,0,0,0,0,0,

** 0,0,0,0,0,0,0,0,0,0,**
** 0,0,0,0,0,0,0,0,0,0};**
** uint8_t checksum = MDB_EXPANSION;**
** if(buffer_level(MDB_USART,RX) < 60) return; **

** #if DEBUG == 1**
** send_str_p(UPLINK_USART, PSTR("EXPANSION\r\n"));**
** #endif**
** for(uint8_t i=0; i<30; i++) {**
data = (uint8_t) recv_mdb(MDB_USART);
* #if DEBUG == 1*
mdb_dump(RX,data*);
_
#endif*
* if(i != 30) {
checksum += data
;
}
}
// validate checksum*
* if(checksum != data[30]) {_
mdb_active_cmd = MDB_IDLE;
mdb_poll_reply = MDB_REPLY_ACK;
checksum = MDB_EXPANSION;
send_str_p(UPLINK_USART,PSTR("Error: invalid checksum [EXPANSION]\r\n"));
_ return;
}*_

* send_mdb(MDB_USART, 0x009);
_ checksum = 0x09;
#if DEBUG == 1*
* mdb_dump(TX,0x009);*
* #endif*
* for(uint8_t j=1; j<29; j++) {
send_mdb(MDB_USART,data[j]);*

* checksum += data[j];
#if DEBUG == 1*
* mdb_dump(TX,data[j]);*
* #endif*
* }_
send_mdb(MDB_USART,checksum);
_ #if DEBUG == 1*
* mdb_dump(TX,checksum);*
* #endif*_

* mdb_active_cmd = MDB_IDLE;
mdb_poll_reply = MDB_REPLY_ACK;
_}*
The output is:
EXPANSION
RAW RX MDB: 0x000
RAW RX MDB: 0x041
RAW RX MDB: 0x050
RAW RX MDB: 0x049
RAW RX MDB: 0x030
RAW RX MDB: 0x031
RAW RX MDB: 0x032
RAW RX MDB: 0x033
RAW RX MDB: 0x034
RAW RX MDB: 0x035
RAW RX MDB: 0x036
RAW RX MDB: 0x037
RAW RX MDB: 0x038
RAW RX MDB: 0x039
RAW RX MDB: 0x030
RAW RX MDB: 0x031
RAW RX MDB: 0x053
RAW RX MDB: 0x054
RAW RX MDB: 0x055
RAW RX MDB: 0x044
RAW RX MDB: 0x049
RAW RX MDB: 0x04f
RAW RX MDB: 0x04c
RAW RX MDB: 0x049
RAW RX MDB: 0x04e
RAW RX MDB: 0x045
RAW RX MDB: 0x052
RAW RX MDB: 0x056
RAW RX MDB: 0x000
RAW RX MDB: 0x069
Error: invalid checksum [EXPANSION]
RAW RX MDB: 0x070
RAW RX MDB: 0x117
Please help me see where I am getting this wrong. Thank you._

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

    static uint8_t data[30] = {0,0,0,0,0,0,0,0,0,0,

...

    if(checksum != data[30]) {

Index 30 is outside the array.

An array of size 30 has indices 0 to 29, so you probably mean 29 if you intend the last character.

Hi, I have changed that, thanks, but still receiving the same error.

Thank you

Regards

Please post your corrected code using code tags, not bold tags.

I apologize. Herein is my code

void mdb_expansion(void) {  

    static uint8_t data[30] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    uint8_t checksum = MDB_EXPANSION;

    if(buffer_level(MDB_USART,RX) < 60) return;     
    
    #if DEBUG == 1
    send_str_p(UPLINK_USART, PSTR("EXPANSION\r\n"));
    #endif

    for(uint8_t i=0; i<30; i++) {
        data[i] = (uint8_t) recv_mdb(MDB_USART);
        #if DEBUG == 1
        mdb_dump(RX,data[i]);
        #endif
        if(i != 29) {
            checksum += data[i];
        }
    }

    // validate checksum
    if(checksum != data[29]) {
        mdb_active_cmd = MDB_IDLE;
        mdb_poll_reply = MDB_REPLY_ACK;
        checksum = MDB_EXPANSION;
        send_str_p(UPLINK_USART,PSTR("Error: invalid checksum [EXPANSION]\r\n"));
        return;  
    }
    
    send_mdb(MDB_USART, 0x009);
    checksum = 0x09;
    #if DEBUG == 1
    mdb_dump(TX,0x009);
    #endif
    for(uint8_t j=1; j<29; j++) {
        send_mdb(MDB_USART,data[j]);
        checksum += data[j];
        #if DEBUG == 1
        mdb_dump(TX,data[j]);
        #endif
    }

    send_mdb(MDB_USART,checksum);
    #if DEBUG == 1
    mdb_dump(TX,checksum);
    #endif
    
    mdb_active_cmd = MDB_IDLE;
    mdb_poll_reply = MDB_REPLY_ACK;
}

Thank you

RAW RX MDB: 0x117

I'm having problems seeing a three digit value fitting in a two digit variable.

Often it is easier to see the problem if one can see the whole picture.

... And there is no way that a byte quantity can have 3 digits.

Are you sure that you are printing the value in hex all the time? Can't see mdb_dump anywhere.

Helpful debug would also be to print the value it should be when the test fails. That may give you a clue.

Checksums may not include every character received, for example the start of a packet, so are you sure you are using the right data?

The mdb_dump displays this:

RAW RX MDB: 0x070

RAW RX MDB: 0x117

EXPANSION
RAW RX MDB: 0x000

RAW RX MDB: 0x041

RAW RX MDB: 0x050

RAW RX MDB: 0x049

RAW RX MDB: 0x030

RAW RX MDB: 0x031

RAW RX MDB: 0x032

RAW RX MDB: 0x033

RAW RX MDB: 0x034

RAW RX MDB: 0x035

RAW RX MDB: 0x036

RAW RX MDB: 0x037

RAW RX MDB: 0x038

RAW RX MDB: 0x039

RAW RX MDB: 0x030

RAW RX MDB: 0x031

RAW RX MDB: 0x053

RAW RX MDB: 0x054

RAW RX MDB: 0x055

RAW RX MDB: 0x044

RAW RX MDB: 0x049

RAW RX MDB: 0x04f

RAW RX MDB: 0x04c

RAW RX MDB: 0x049

RAW RX MDB: 0x04e

RAW RX MDB: 0x045

RAW RX MDB: 0x052

RAW RX MDB: 0x056

RAW RX MDB: 0x000

RAW RX MDB: 0x069

Error: invalid checksum [EXPANSION]

kelidel:
I apologize. Herein is my code

When posting code it's always best to post a complete sketch that demonstrates the problem. If the problem is in a large sketch then if possible extract the code with the problem into a separate minimal sketch that demonstrates the problem in the simplest possible way. Quite often, the act of doing this will show up some false assumption you've made which will enable you to spot the fault for yourself, but if not then you've saved everyone else wasted effort in trawling through code that is not relevant to the problem.