Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
i have to send the following packet format to a machine:-

C++
byte     byte     bit         bit
                       2       2         1           1
                 +---------+-------+-------------+----------+
                 |  CM     |   00  |  check sum  |  CR      |
                 +---------+-------+-------------+----------+


I have the following instructions:-

All data isASCII character code without checksumand CR.
Calculate sum total from top till before checksum ( 1 bite unit ),then recognize this complement of 1 as checksum.
Finish of format is CR (Hexadecimal: OD)

So to implement this
i took
This is main .cpp
C++
char data[6]="CM00";
typedef unsigned short int word16;
word16      check1;
int main()
{
int h=2;
check1 =calc_check(hh);
	data[4]= check1;
	data[5]=0x0D;
}


this is the file checksum .cpp
C++
typedef unsigned char      byte;    // Byte is a char
typedef unsigned short int word16;  // 16-bit word is a short int
typedef unsigned int       word32;  // 32-bit word is an int

//----- Defines ---------------------------------------------------------------
#define BUFFER_LEN        6      // Length of buffer
extern char data[6];

word16      check;  
//----- Prototypes ------------------------------------------------------------
word16 checksum(char *addr, word32 count);
//int calc_check(int);
//===== Main program ==========================================================
int calc_check(int w)
{
	//byte        buff[BUFFER_LEN]; // Buffer of packet bytes

	// 16-bit checksum value
	word32      i;                // Loop counter

	// Load buffer with BUFFER_LEN random bytes
	for (i=0; i<BUFFER_LEN; i++)
	{
		//buff[i] = (byte) rand();
		data[i]=(byte) rand();
	}

	// Compute the 16-bit checksum
	//check = checksum(buff, BUFFER_LEN);
	check = checksum(data, BUFFER_LEN);

	// Output the checksum
	printf("checksum = %04X \n", check);
	return check;

}

//=============================================================================
//=  Compute Internet Checksum for count bytes beginning at location addr     =
//=============================================================================
word16 checksum(char *addr, word32 count)
{
	register word32 sum = 0;
	word16 * waddr = reinterpret_cast<word16*>(addr);//added
	// Main summing loop
	while(count > 1)
	{
		//sum = sum + *((word16 *) addr)++;//main
		sum = sum + *waddr++;
		//sum += *((word16 *) addr)++;
		count = count - 2;
	}

	// Add left-over byte, if any
	if (count > 0)
		sum = sum + *((byte *) addr);

	// Fold 32-bit sum to 16 bits
	while (sum>>16)
		sum = (sum & 0xFFFF) + (sum >> 16);

	return(~sum);
}

I think i am doing something wrong in checksum.cpp as i have given the statement
Calculate sum total from top till before checksum so acc to this statement i should do
C++
for (i=0; i<2; i++)
    {
        //buff[i] = (byte) rand();
        data[i]=(byte) rand();
    }

Then am i doing this correct

C++
data[4]= check1;
	data[5]=0x0D;
Posted
Comments
Tarun Batra 13-Sep-12 2:09am    
sir as u can see in the above packet format the check sum field is one bit,and i have done data[4]= check1; so is it correct?

1 solution

No.
When you calculate your checksum, you overwrite everything in the buyffer, and then you are including all the characters in the message:
#define BUFFER_LEN        6      // Length of buffer
extern char data[6];
int calc_check(int w)
{
...
	// Load buffer with BUFFER_LEN random bytes
	for (i=0; i<BUFFER_LEN; i++)
	{
		data[i]=(byte) rand();
	}

	// Compute the 16-bit checksum
	check = checksum(data, BUFFER_LEN);
... 
}
Since the loop will overwrite the data you loaded before you called it.
Then you try to include the checksum byte in itself, and the CR terminator!

And that's ignoring that your code won't compile, because "h" and "hh" are not the same...


Why not hand the buffer and a length to the check routine, and have it return the check byte? If you did that, you could use the same function for incoming messages as well as outgoing...
 
Share this answer
 
Comments
Tarun Batra 13-Sep-12 2:05am    
sir to avoid the loop to overwrite the data I loaded before I called it,i should do something like this:-for (i=0; i<4; i++)
{
//buff[i] = (byte) rand();
data[i]=(byte) rand();
} and then i should do data[4]= check1;
data[5]=0x0D; as i have the following statement "Calculate sum total from top till before check sum then recognize this
complement of 1 as check sum. Now am i correct?
OriginalGriff 13-Sep-12 3:32am    
No. Why the heck are you filling it with random data at all? All that does is overwrite whatever messgae you had to start with, and generates a check on the random rubbish!
Tarun Batra 13-Sep-12 3:37am    
yes air iyou are correct i should comment this part -for (i=0; i<4; i++)
{
//buff[i] = (byte) rand();
data[i]=(byte) rand();} and sir can u tell me one more thing?
OriginalGriff 13-Sep-12 3:45am    
It's not a case of commenting it - it probably shouldn't be there at all!

And what is the "other thing"?
Tarun Batra 13-Sep-12 3:50am    
Ya sir you are correct,just tell me please that in this statement "check = checksum(data, BUFFER_LEN);" in check variable i will be getting the checksum value and as u can see i have data part as "char data[6]="CM00";" so is it correct data[4]= check; and acc to statement Finish of format is CR (Hexadecimal: OD) i am doing "data[5]=0x0D;",,,are these both correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900