簡體   English   中英

使用readfile函數解碼串行GPS時遇到問題

[英]trouble decoding the serial GPS using readfile function

我試圖使用readfile函數讀取c ++中的串行端口。 我設法打開並讀取C ++中的串行端口。 我現在面臨的問題是從串行端口讀取數據后對數據進行解碼。 以下是我的代碼。 當我運行代碼時,我的解碼循環無法檢測到((*&szChar =='$')),並且由於打印錯誤而退出循環。 我可以知道如何解碼從串行端口讀取的gps數據嗎? 謝謝

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");

我不得不說,我發現您的代碼非常混亂和混亂。 舉例來說,您將szChar創建為100個char數組,並將ReadBufferBUFFERSIZE char數組。 但是,當您調用ReadFile時,您將傳遞大小為BUFFERSIZE的szChar基地址。 除非巧合,否則BUFFERSIZE恰好等於100,這看起來很像潛在的緩沖區溢出。

然后我們到達*&szChar 這也不是很有意義。 從外觀szChar[0] ,您可能需要szChar[0] ,但這並不是一個好主意,因為您可能無法以完全行大小的片段接收數據。 因此,您可能希望瀏覽數據以查找“ $”。

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:謝謝..所以我在下面編輯了我的代碼以解碼數據,將ReadBuffer放入另一個數組進行檢查是否正確?

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM