Go Down

Topic: Checksum error (Read 915 times) previous topic - next topic

kelidel

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.


Nick Gammon

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.


Code: [Select]

    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.

marco_c

An array of size 30 has indices 0 to 29, so you probably mean 29 if you intend the last character.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

kelidel

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

Thank you

Regards

AWOL

Please post your corrected code using code tags, not bold tags.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up