Pages: [1]   Go Down
Author Topic: Checksum error  (Read 817 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    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.
Logged


Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thank you

Regards
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I apologize.  Herein is my code

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

... 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?
« Last Edit: February 08, 2013, 06:15:39 am by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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]

Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: