Go Down

Topic: Checksum error (Read 928 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.
http://www.gammon.com.au/electronics

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<br />Parola for Arduino 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.

kelidel

I apologize.  Herein is my code

Code: [Select]


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

AWOL

Quote
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.
"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.

marco_c

#7
Feb 08, 2013, 12:13 pm Last Edit: Feb 08, 2013, 12:15 pm by marco_c Reason: 1
... 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?
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

kelidel

The mdb_dump displays this:

Quote


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]



PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up