My problem is that the strcmp() functions makes the following problems:
main.c:35: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
While compiling and the following while running the program:
Segmentation fault: 11
I know what the errors means, I just don't know any other way to do it...Have thought about using itoa() or sprintf(), but i need the checksum in hex so can't see how I can accomplish that.
The code is as follows:
#include "checksum.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char nmea[] ="$GPRMC,131637.000,A,5820.0658,N,00834.5652,E,0.00,,090911,,,A*7E";
unsigned char checksum[] = "00";
char data[82];
int length = strlen(nmea);
int k = 0;
int i;
unsigned char XOR;
checksum[0] = nmea[(length-2)];
checksum[1] = nmea[(length-1)];
for(i=1;i < (length-3);i++){
data[(i-1)] = nmea[i];
}
int dataLength = strlen(data);
for (XOR = 0, i = 0; i < dataLength; i++) //XORer for å finne checksum
XOR ^= (unsigned char)data[i];
printf("*******************************************************\n");
printf("Calculating checksum...\n");
printf("Read checksum: %s\n",checksum);
printf("Read data: %s\n",data);
printf("Calculated checksum: %X \n",XOR);
if(strcmp(XOR,checksum) == 0){ //sammenligner checksumer.
printf("Checksum: OK!\n");
//return 1;
}
else{
printf("Checksum: Mismatch!\n");
//return 0;
}
printf("*******************************************************\n");
//nmeachecksum(buf);
return 0;
}
Any help is greatly appreciated!
char XOR[2] = { 0, 0 };
And then just use XOR[0]
everywhere.
The above is just my no-segfault rewrite of what your code was actually trying to do.
What it should do . . . the strcmp()
is comparing binary text against a formatted version of the same value, so it won't work. Your unsigned char XOR;
(or perhaps an 8-bit <stdint.h>
type) is about right. But compare this with ==
as a scalar instead of with strcmp()
.
What you compare it to . . . you will need to turn your NMEA printable hex into a scalar operand suitable for ==
so use sscanf() or just open code the hex conversion.1
1. Hex input conversion code examples are available in many stackoverflow answers.