[英]trouble decoding the serial GPS using readfile function
im trying to use readfile function to read the serial port in c++. 我试图使用readfile函数读取c ++中的串行端口。 i manage to open and read the serial port in c++.
我设法打开并读取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.
当我运行代码时,我的解码循环无法检测到((*&szChar =='$')),并且由于打印错误而退出循环。 May i know how could i decode the gps data that i read from my serial port?
我可以知道如何解码从串行端口读取的gps数据吗? 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. 举例来说,您将
szChar
创建为100个char
数组,并将ReadBuffer
为BUFFERSIZE
char
数组。 When you call ReadFile
, however, you're passing the base address of szChar with the size given as BUFFERSIZE
. 但是,当您调用
ReadFile
时,您将传递大小为BUFFERSIZE
的szChar基地址。 Unless, by some coincidence, BUFFERSIZE
happens to equal 100, that looks a lot like a potential buffer overrun. 除非巧合,否则
BUFFERSIZE
恰好等于100,这看起来很像潜在的缓冲区溢出。
Then we get to *&szChar
. 然后我们到达
*&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. 从外观
szChar[0]
,您可能需要szChar[0]
,但这并不是一个好主意,因为您可能无法以完全行大小的片段接收数据。 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? @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.