NMEA Checksum gone wrong

Hi All,

I am trying to run a checksum algorithm found in this forum (probably from the ArduPilot project).

Using the code enclosed I keep getting the wrong checksum 0, the correct checksum is 5B.

However, using the commented out string I get the correct checksum of 68.

What am I doing wrong?

Thanks

//$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E68
//$GPRMC,050000,A,331.57,N,00356.61,E,000.0,269.0,010116,004.0,W
5B

int index;
char inchar;
//String tempPos = "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*";
String tempPos ="$GPRMC,050000,A,331.57,N,00356.61,E,000.0,269.0,010116,004.0,W*";
char pos[80];

byte start_with = 0;
byte end_with = 0;
byte CRC = 0;

void setup() {

Serial.begin(9600);

}

void loop() {
tempPos.toCharArray(pos, 80);
// Serial.print(pos);
for ( index = 0; index < 80; index++) {
inchar = pos[index];

if ( inchar == '$') {
start_with = index;
}

if (inchar == '*') {
end_with = index;
}

for (byte x = start_with + 1; x < end_with; x++) { // XOR every character in between '$' and '*'
CRC = CRC ^ pos[x];

}
}
Serial.println(CRC, HEX);}

As best as I can tell, it might have something to do with this part of the program:

josefvs:
CRC = CRC ^ pos[x];

Try posting with code tags.

//$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68
//$GPRMC,050000,A,331.57,N,00356.61,E,000.0,269.0,010116,004.0,W*5B

int index;
char inchar;
//String temp = "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*";
String temp="$GPRMC,050000,A,331.57,N,00356.61,E,000.0,269.0,010116,004.0,W*";
char pos[80];

byte start_with = 0;
byte end_with = 0;
byte CRC = 0;

void setup() {

  Serial.begin(9600);

}

void loop() {
  temp.toCharArray(pos, 80);

//  Serial.print(pos);
  for ( index = 0; index < 80; index++) {
    inchar = pos[index];

    if ( inchar == '

) {
      start_with = index;
    }

if (inchar == '*') {
      end_with = index;
    }

for (byte x = start_with + 1; x < end_with; x++) { // XOR every character in between '


 and '*'
      CRC = CRC ^ pos[x];
    }
  }
  Serial.println(CRC, HEX);}

The loop function has improperly nested loops, so the checksum (not CRC) calculation is repeated for every step of the "index" loop.

Thanks jremington, that will teach me to check better copied code in the future.

The working version:

//$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68
//$GPRMC,050000,A,331.57,N,00356.61,E,000.0,269.0,010116,004.0,W*5B

int index;
char inchar;
//String temp = "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*";
String temp = "$GPRMC,050000,A,331.57,N,678453.61,E,000.0,269.0,010116,004.0,W*";
char pos[80];

byte start_with = 0;
byte end_with = 0;
byte CRC = 0;

void setup() {

  Serial.begin(9600);

}

void loop() {

  temp.toCharArray(pos, 80);

  Serial.print(pos);

  for ( index = 0; index < 80; index++) {
    inchar = pos[index];

    if ( inchar == '

) {
      start_with = index;
    }

if (inchar == '*') {
      end_with = index;
    }

}

for (byte x = start_with + 1; x < end_with; x++) { // XOR every character in between '


 and '*'
    CRC = CRC ^ pos[x];
  }
  Serial.println(CRC, HEX);
  CRC = 0;
}