简体   繁体   中英

trouble decoding the serial GPS using readfile function

im trying to use readfile function to read the serial port in c++. i manage to open and read the serial port in c++. the problem i facing now is the decoding of data after i read from the serial port. The below are my codes. When i run my code, my loop of decoding could not detect ((*&szChar == '$')), and it exit the loop by printing error. May i know how could i decode the gps data that i read from my serial port? thanks

char szChar[100];
int nRet;
DWORD  dwBytesRead = 10;
char   ReadBuffer[BUFFERSIZE] = {0};
nRet = ReadFile(hCom,&szChar,BUFFERSIZE-1,&dwBytesRead,NULL);
if((*&szChar == '$'))
{
    printf("%s\n", &szChar);
}
else
{
    printf("error\n");

I have to say, I find your code quite confused and confusing. Just for example, you're creating szChar as an array of 100 char , and ReadBuffer as an array of BUFFERSIZE char s. When you call ReadFile , however, you're passing the base address of szChar with the size given as BUFFERSIZE . Unless, by some coincidence, BUFFERSIZE happens to equal 100, that looks a lot like a potential buffer overrun.

Then we get to *&szChar . This doesn't really make much sense either. From the looks of things, you probably want szChar[0] -- but even that's not really a good idea, because you might not receive the data in exactly line-sized pieces. As such, you probably want to scan through the data to find the '$'.

int Ret;
DWORD  BytesRead;
char   ReadBuffer[BUFFERSIZE] = {0};
Ret = ReadFile(hCom,ReadBuffer,sizeof(ReadBuffer)-1,&BytesRead,NULL);

ReadBuffer[BytesRead] = '\0';

if (ReadBuffer[0] == '$')
    printf(%s\n", ReadBuffer);
else
    printf("error\n");

@Jerry: Thanks.. so i edited my code below to decode my data, is it correct way to put my ReadBuffer into another array for checking?

char lastCommaPosition;
char  latitudeString[11];
char     stringRead[MAXSIZE]; 
char  tempString[MAXSIZE];
char  *pChar;
char  dummyChar;
float        latitude;
int          latDegrees;
float        latMinutes;
int     numLinesRead;

int Ret,i,j,k; 
  if (ReadBuffer[0] == '$') 
{
    i = 0;
    numLinesRead++;
    stringRead[i] = ReadBuffer;

}
stringRead[i+1] = '\0';



j = 0;
pChar = stringRead;
while(*(pChar+j) != ',') 
{
       tempString[j] = *(pChar+j);
       j++;
 }
 tempString[j] = '\0';


if(tempString[3] == 'G' && tempString[4] == 'G' && tempString[5] == 'A')
{
     pChar = stringRead;


      j = lastCommaPosition + 1;
      k = 0;
      while(*(pChar+j) != ',') 
      {
       latitudeString[k] = *(pChar+j);
       j++;
       k++;
      }
      lastCommaPosition = j;
      latitudeString[k] = '\0';

      sscanf(latitudeString, "%f", &latitude);
      latDegrees = (int)(latitude/100);
      latMinutes = (float)(latitude - latDegrees*100);
      printf("\t%02d DEG\t%2.4f MIN", latDegrees, latMinutes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM