checksum generate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yazixchi
    New Member
    • May 2011
    • 1

    checksum generate

    Dear all,

    i have the problem with checksum generate array data.
    This is a sample piece of my program
    Code:
    unsigned char data1[3];
    unsigned char data2[4]={0x7F,0xA0,0x00,0x00};
    
    void send(unsigned char *a, unsigned char b, unsigned char *b){
    
    //I do not know what program should i write 
    // in this modular, i want to display result of checksum
    
    }
    
    //main program
    
    //call the modular of send
    send(data1, 0x33, data2); //
    
    //end of main program
    so, how to generate checksum data in modular send?
    i want to generate checksum data from array data1, array data2 and 0x33

    note : i want to use XOR for checksum the data.

    checksum in other words in this case is error detection.
    error detection is generated by "exclusive-OR" all bytes data.

    give me advice

    many thanks
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    Hi!

    XOR is mainly used for parity check. The simplest checksum you could use is a 1-byte checksum, that is you add all your transmitted bytes to a single byte. Of course overflow expected, and a propability of 1/256 to miss something without realizing it. A more "real life" solution would be to use 2 checksum bytes (propability drops to 1/65536 and overflow comes only in large frames). If something is lost or corrupted, receiver understands it from the checksum bytes and asks for data again. So reading your example, a checksum value would be:

    Code:
    uint16 checksum = data1[0]+data1[1]+data1[2]+0x33
                   +data2[0]+data2[1]+data2[2]+data2[3]

    If now you need highest security, then you should go for parity frame or even better for CRC, it is the safest method.

    Furthermore, a start and stop byte (not to say frame) MUST be used. For example start a frame with bytes 0x10 0x02 and finish the frame with 0x11 0x03. If you don't receive those frames at the desired position (ie 0x10 should be received at position 0 and 0x02 at position 1), then reset the communication (the byte counter actually) and wait for 0x10 0x02 to be properly alligned to incoming frame again. Checksum could be send just before stop frame.

    Finally in your send routine you wrote:
    void send(unsigned char *a, unsigned char b, unsigned char *b)
    You declare "b" variable twice, this is not good.

    Hope that helped.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Are you trying to comply with some preexisting standard for how these messages are checksummed; or are you designing your own scheme?

      What checksum value do you expect the following 10-byte message to generate?
      0x0A, 0x19, 0x28, 0x37, 0x46, 0x55, 0x64, 0x73, 0x82, 0x91

      Comment

      Working...