Problem with XOR on Char array

Hi,

I just get crazzy with a bit of code for calculating a simple XOR checksum on a char array to build a message for a serial stream over an xbee. The message has the same format as NEMA message $…*checksum

The checksum is even wrong calculated. I checked it here: cecksum calculator.

The code on the other side for checkin the checksum works fine with the same algorithm!? Hope you have an idea. I extracted the part to a test program to debug it more easy. Only to avoid confusion:
First I have string to build with the message. Only for the checksum test I transfered it to char array.

String MessageStr ="";
bool read = false;
char MessageBuffer[100]="";

void setup()
{

	Serial.begin(9600);
	Serial.println("Checksum-Test");
	Serial.println("Enter String to check: ");
}

void loop()
{

	if (Serial.available())
	{
		while (Serial.available())
		{
			MessageStr=Serial.readString();
			read = true;
			
		}
		
		if (read)
		{
			Serial.print("String read: ");
			Serial.println(MessageStr);
			BuildMessage();
			read=false;
			Serial.println("Next String: ");
			
		}
		
	}

}

void BuildMessage(void) {
	int MesChecksum=0;
	
	char buf[12];
	
	//here i build the message

	/*Generating and parsing message checksum
	starting at the 2. Position to not calculate initial '

Here is the part that checks the checksum without any problem:

boolean TelegramCheckChecksum (char* Message, int MessageLength)

{
	byte checksumReceived=0, checksum=0;
	for (int x = 1; x < MessageLength; x++)
	{
		if (Message[x] == '*') //Checksum starts with '*'
		{
			checksumReceived = strtol(&Message[x + 1], NULL, 16); //Parsing received checksum... |strtol parsing string to integer
			break; //Exit for and continue with next if
		}
		else
		{
			checksum ^= Message[x]; //XOR the received data...
		}
	}
	
	if (checksum==checksumReceived)
	{
		return true;
	}
	
	else
	{
		return false;
	}
	
}

I hope somebody has an idea where the failure is.

Regards

Andreas*/

MessageStr.toCharArray(MessageBuffer, MessageStr.length());
//for (int x = 1; x < (sizeof(MessageBuffer)/sizeof(MessageBuffer[0])); x++)
//{
//
//if (MessageBuffer[x] == '*')
//{
//Serial.println("Hier ist der break");
//break;
//}
//else
//{
//Serial.print(MessageBuffer[x]);
//MesChecksum ^= MessageBuffer[x]; //XOR the Message data...
//}
//
//Serial.println(x);
//}

int x = 1;

while (MessageBuffer[x]!='*')
{
	MesChecksum ^= MessageBuffer[x]; //XOR the Message data...
	Serial.print(x); Serial.print("  ");
	Serial.print(MessageBuffer[x]); Serial.print("  ");Serial.println(MessageBuffer[x], BIN);
	x++;
	
}
	
MessageStr += MesChecksum;

Serial.println(MessageStr);

//for clearing the MessageStr:

for (int x = 0; x < (sizeof(MessageBuffer)/sizeof(MessageBuffer[0])); x++)
{
	MessageBuffer[x]=0;
}
MesChecksum=0;

}


Here is the part that checks the checksum without any problem:

§DISCOURSE_HOISTED_CODE_1§


I hope somebody has an idea where the failure is. 

Regards

Andreas
		while (Serial.available())
		{
			MessageStr=Serial.readString();
			read = true;
			
		}

Why is this in a while loop()?

	////Converting float to string
	//dtostrf(lat, 4,6,buf);
	//MessageStr ="$GPS,";
	//MessageStr =MessageStr + buf+","+laterr + ",";
	//dtostrf(lon, 4,6,buf);
	//MessageStr =MessageStr + buf+"," + lonerr;
	//MessageStr =MessageStr + "," +ground_speed + ","
	//+ ground_course + "," + alt_MSL + ","
	//+ alterr + "*";

	/*Generating and parsing message checksum
	starting at the 2. Position to not calculate initial '

How much of this shit was it necessary to post?

	//for (int x = 1; x < (sizeof(MessageBuffer)/sizeof(MessageBuffer[0])); x++)
	//{
	//
	//if (MessageBuffer[x] == '*')
	//{
	//Serial.println("Hier ist der break");
	//break;
	//}
	//else
	//{
	//Serial.print(MessageBuffer[x]);
	//MesChecksum ^= MessageBuffer[x]; //XOR the Message data...
	//}
	//
	//Serial.println(x);
	//}

Or this?

	MessageStr.toCharArray(MessageBuffer, MessageStr.length());

Bullshit. The second argument is the size of the array, not the size of the useless String.

	int x = 1;
	
	while (MessageBuffer[x]!='*')

Array indices start at 0.]

Here is the part that checks the checksum without any problem:

So, why don’t you call that function? What you implement instead is NOT the same.*/


How much of this shit was it necessary to post?

§DISCOURSE_HOISTED_CODE_2§


Or this?

§DISCOURSE_HOISTED_CODE_3§


Bullshit. The second argument is the size of the array, not the size of the useless String.

§DISCOURSE_HOISTED_CODE_4§


Array indices start at 0.]

> Here is the part that checks the checksum without any problem:

So, why don't you call that function? What you implement instead is NOT the same.

Paul, tone it down a bit, please.

I try to ignore the useless behaviour. Though I wanne answer your questions hoping to get a more helpfull reply:

I started with the index at 1 because I don't wan't to include the leading "$" in the calculation. Maybe you should also read the comments written.

I used exactly the same code als ofor building the XOR "it is commented out. I leads to the same result as the while code. But the while code is more lean.

I read from serial with a while loop because I wanted to first, that the complete message is read, because within the real code I also have all informatio navailable before starting to build the checksum.

Regards

Andreas

Maybe you should also read the comments written.

Maybe you should reduce the amount of commented-out code (and excess whitespace) before posting.

But the while code is more lean.

"lean" and "String" don't belong in the same sketch, in my book.