I have a compass that connects to my PC through RS232 @ 38400 BAUD. the compass outputs different "sentences" to send different kinds of data. Each sentence is a string that begins with a special header and ends with <CR><LF>. The string that I'm looking for is
"$IIHDG,A,B,C,D,E*hh<CR><LF>
where A, B, C, D, and E are numbers that change i.e. heading.
I have a basic serial class that reads a specified amount from the comm port and stores it in buffer. My code works most of the time but occasionally it errors out. To get around that I added an if statement that lets it return 400 if it errors out (bad I know but it's just a temporary workaround). Does anyone have any ideas how I can speed this up?
char buffer[5000];
char* ptr;
char* subStr = "$IIHDG,";
do {
port.read(buffer, 5000, true);
ptr = strstr(buffer, subStr);
} while(ptr == NULL);
ptr = strtok(ptr,",");
ptr = strtok(NULL,",");
if (ptr != NULL)
return strtod(ptr,NULL);
else
return 400;