📄 gpscomm.cpp
字号:
}
pByte2=pByte1;
//
while (*pByte2 !=',') pByte2++;
//
Len =pByte2-pByte1;
for (int i=0;i<Len;i++)
{
ConStr[i] = pByte1[i];
}
ConStr[Len]=0;
return float(atof(ConStr));
}
//检查是否已经获得一条完整的数据
//返回
// 1:数据已经完整
// 0:数据不完整
//##ModelId=3FE8F86C01A8
int CGpsComm::CheckCommand(void *pVoid,int *Len)
{
unsigned char TmpByte1;
unsigned char TmpByte2;
unsigned char TmpByte3;
unsigned char TmpByte4;
unsigned char TmpByte5;
unsigned char TmpByte6;
int CommType;
unsigned char *pByte;
int nAnotherLen ;
int i;
CommType=0;
nAnotherLen = *Len;
//数据的长度没有达到
//if (*Len < 40)
// return 0;
pByte=(unsigned char *)pVoid;
//查找信息包的头部,丢弃前面不完整的部分
for (i=0;i<nAnotherLen-1;i++)
{
if ( (*pByte=='$') && (*(pByte+1)=='G') && (*(pByte+2)=='P'))
break;
pByte++;
}
//(pVoid + i)指向头部
//i是应该丢弃的字节数
int Count;
BYTE *TmpPointer;
Count=i;
nAnotherLen -= Count;
//
*Len = nAnotherLen ;
TmpPointer=(unsigned char *) pVoid;
if (TmpPointer != pByte)
for (i=0; i<nAnotherLen; i++)
*TmpPointer++=*pByte++;
//丢弃前面的非法数据
pByte=(unsigned char *)pVoid;
TmpByte1=*(pByte);
TmpByte2=*(pByte+1);
TmpByte3=*(pByte+2);
TmpByte4=*(pByte+3);
TmpByte5=*(pByte+4);
TmpByte6=*(pByte+5);
//返回信息格式错误
if (!((TmpByte1=='$') &&
(TmpByte2=='G') &&
(TmpByte3=='P')))
return 0;
//查找其中是否存在'CR' 'LF'
for (i=0;i<nAnotherLen-1;i++)
{
unsigned char TmpByte1;
unsigned char TmpByte2;
TmpByte1=CONST_CR;
TmpByte2=CONST_LF;
if (*(pByte+i)==',')
++CommType;
if ( *(pByte+i)== CONST_CR )
{
if ( *(pByte+i+1)== CONST_LF )
{
if ((TmpByte4 == 'G' ) && (TmpByte5 == 'G' ) && (TmpByte6 == 'A'))
return CONST_GGA;
else if ((TmpByte4 == 'G' ) && (TmpByte5 == 'L' ) && (TmpByte6 == 'L' ))
return CONST_GLL;
else if ((TmpByte4 == 'G' ) && (TmpByte5 == 'S' ) && (TmpByte6 == 'A' ))
return CONST_GSA;
else if ((TmpByte4 == 'G' ) && (TmpByte5 == 'S' ) && (TmpByte6 == 'V' ))
return CONST_GSV;
else if ((TmpByte4 == 'R' ) && (TmpByte5 == 'M' ) && (TmpByte6 == 'C' ))
return CONST_RMC;
else if ((TmpByte4 == 'V' ) && (TmpByte5 == 'T' ) && (TmpByte6 == 'G' ))
return CONST_VTG;
else
return 0;
}
}
}
return 0;
}
//对数据缓冲区进行处理
//数据必然是完整的数据包
//返回:
// 0--错误
// 1--GGA
// 2--RMC
//##ModelId=3FE8F86C0073
int CGpsComm::ProcessData(void *pVoid,int Len)
{
unsigned char TmpByte1;
unsigned char TmpByte2;
unsigned char TmpByte3;
unsigned char TmpByte4;
unsigned char TmpByte5;
unsigned char TmpByte6;
unsigned char *pByte;
pByte=(unsigned char *)pVoid;
TmpByte1=*(pByte);
TmpByte2=*(pByte+1);
TmpByte3=*(pByte+2);
TmpByte4=*(pByte+3);
TmpByte5=*(pByte+4);
TmpByte6=*(pByte+5);
//返回信息格式错误
if (!((TmpByte1=='$') &&
(TmpByte2=='G') &&
(TmpByte3=='P')))
return 0; //丢弃全部数据
//下面对不同的信息进行处理
//对信息进行显示GGA
if ( TmpByte4 == 'G' &&
TmpByte5 == 'G' &&
TmpByte6 == 'A' )
return 1;
else if ( TmpByte4 == 'R' &&
TmpByte5 == 'M' &&
TmpByte6 == 'C' )
return 2;
return 0;
}
//获得可见卫星数量
//##ModelId=3FE8F86C0067
unsigned char CGpsComm::GetSeeSatNumFromInfo(void *pVoid)
{
unsigned char *pByte1;
unsigned char *pByteS;
int Count=0;
pByte1 = (unsigned char *)pVoid;
pByteS = pByte1;
//从缓冲中搜索',',取出第6个','数字
while (Count<6)
{
if (*pByte1 == ',')
{
Count ++;
pByte1 ++;
continue;
}
while (*pByte1 != ',')
{
pByte1 ++;
if (*pByte1 == ',')
{
Count++;
pByte1 ++;
break;
}
if (pByte1-pByteS>=MaxInfoBlock)
return 0;
}
}
return (*pByte1-'0')*10+(*(pByte1+1)-'0');
}
//获得跟踪卫星数量
//##ModelId=3FE8F86C0054
unsigned char CGpsComm::GetTrackSatNumFromInfo(void *pVoid)
{
unsigned char *pByte1;
unsigned char *pByteS;
int Count=0;
pByte1 = (unsigned char *)pVoid;
pByteS = pByte1;
//从缓冲中搜索',',取出第6个','数字
while (Count<6)
{
if (*pByte1 == ',')
{
Count ++;
pByte1 ++;
continue;
}
while (*pByte1 != ',')
{
pByte1 ++;
if (*pByte1 == ',')
{
Count++;
pByte1 ++;
break;
}
if (pByte1-pByteS>=MaxInfoBlock)
return 0;
}
}
return (*pByte1-'0')*10+(*(pByte1+1)-'0');
}
//获得状态
//##ModelId=3FE8F86C01BC
int CGpsComm::GetStatusFromInfo(void *pVoid)
{
unsigned char *pByte1;
unsigned char *pByteS;
int Count=0;
pByte1 = (unsigned char *)pVoid;
pByteS = pByte1;
//从缓冲中搜索',',取出第6个','后面的第一个数字
while (Count<6)
{
if (*pByte1 == ',')
{
Count ++;
pByte1 ++;
continue;
}
while (*pByte1 != ',')
{
pByte1 ++;
if (*pByte1 == ',')
{
Count++;
pByte1 ++;
break;
}
if (pByte1-pByteS>=MaxInfoBlock)
return 0;
}
}
//
return (*pByte1-'0');
}
// 获得经度、纬度、高度、是否有效、时、分、秒、卫星数量
//##ModelId=3FE8F86C0049
void CGpsComm::ProcessGGA(BYTE *pBuf, CGPSInfo &GPSInfo)
{
unsigned char *pByte;
unsigned char *pByte1; // 指定一段数据的起点
unsigned char *pByte2; // 指定一段数据的终点
unsigned char *pByteS; // 缓冲的起始位置
int Degree; // 度
int Minute; // 分
int MiniMinute; // 分的小数部分
pByte = (unsigned char *)pBuf;
pByteS=pByte;
// 从缓冲中搜索',',取出第2个','与第3个','之间的数据转换成纬度
pByte1 = pByte;
//从缓冲中搜索',',取出第1个','后的数据
while (*pByte1 != ',') pByte1++;
pByte1++; // pByte1 指向HHMMSS.sss
GPSInfo.Hour = (*pByte1-'0')*10 + (*(pByte1+1)-'0');
pByte1 += 2;
GPSInfo.Minute = (*pByte1-'0')*10 + (*(pByte1+1)-'0');
pByte1 += 2;
GPSInfo.Second = (*pByte1-'0')*10 + (*(pByte1+1)-'0');
//pByte1 += 3; // 第2个','后面的第一个字符
while (*pByte1 != ',') pByte1++;
pByte1 ++;
pByte2 = pByte1;
while (*pByte2 != ',' ) // 3
{
pByte2++;
if ((pByte2-pByteS) >=MaxInfoBlock)
return;
}
Degree = (*pByte1-'0')*10 +(*(pByte1+1) -'0'); // 度
pByte1 += 2; // 分
Minute = (*pByte1-'0')*10 + (*(pByte1+1) -'0');
pByte1 += 3; // 分的小数部分
MiniMinute = (*pByte1-'0')*1000 + (*(pByte1+1) -'0')*100+
(*(pByte1+2)-'0')*10 + (*(pByte1+3) -'0');
// 计算纬度
GPSInfo.Lat = static_cast<double>(Degree) + (static_cast<double>(Minute) +static_cast<double>(MiniMinute) / 10000.0) /60.0;
//////////////////////////////////////////////////////////////////////////////
// 上面已经计算了纬度,下面计算经度 pByte1指向纬度的起点,pByte2指向纬度的终点","
//////////////////////////////////////////////////////////////////////////////
pByte1 = pByte2 + 3; // pByte1指向经度的起点
pByte2 = pByte1;
while (*pByte2 != ',' ) // pByte2指向经度的终点
{
pByte2++;
if ((pByte2-pByteS) >=MaxInfoBlock)
return;
}
Degree = (*pByte1-'0')*100 + (*(pByte1+1)-'0')*10 + *(pByte1+2)-'0'; // 度
pByte1 += 3; // 分
Minute = (*pByte1-'0')*10 + (*(pByte1+1) -'0');
pByte1 += 3; // 分的小数部分
MiniMinute = (*pByte1-'0')*1000 + (*(pByte1+1) -'0')*100+
(*(pByte1+2)-'0')*10 + (*(pByte1+3) -'0');
GPSInfo.Lon = static_cast<double>(Degree) + (static_cast<double>(Minute) +static_cast<double>(MiniMinute) / 10000.0) /60.0;
//////////////////////////////////////////////////////////////////////////////
// 下面计算有效标志
//////////////////////////////////////////////////////////////////////////////
pByte1 = pByte2 + 3; // pByte1有效标志
pByte2 = pByte1+1; // pByte1有效标志后面的,
GPSInfo.ValidFlag = *pByte1 - '0';
//////////////////////////////////////////////////////////////////////////////
// 下面计算卫星数量
//////////////////////////////////////////////////////////////////////////////
pByte1 = pByte2 + 1; // pByte1数量
pByte2 = pByte1+2; // pByte1数量后面的,
GPSInfo.SatNum = (*pByte1 - '0') * 10 + (*(pByte1+1) - '0');
//////////////////////////////////////////////////////////////////////////////
// 下面计算高程
//////////////////////////////////////////////////////////////////////////////
pByte1 = pByte2 + 1; // DOP
while (*pByte1 != ',' ) // pByte1
{
pByte1++;
if ((pByte1-pByteS) >=MaxInfoBlock)
return;
}
pByte1++; // 高程
pByte2 = pByte1;
while (*pByte2 != ',' ) // pByte2
{
pByte2++;
if ((pByte2-pByteS) >=MaxInfoBlock)
return;
}
pByte2 --; // 高程终点
char ConStr[10];
int HeightLen = pByte2 - pByte1 + 1; // 高程长度
for (int i = 0; i < HeightLen; i++ )
ConStr[i] = * (pByte1 + i);
ConStr[HeightLen] = char(0);
GPSInfo.Height = atof(ConStr);
}
// 获得年、月、日、速度、方向
//##ModelId=3FE8F86C0035
void CGpsComm::ProcessRMC(BYTE *pBuf, CGPSInfo &GPSInfo)
{
unsigned char *pByte1;
unsigned char *pByte2;
unsigned char *pByteS;
char ConStr[10];
int Count=0;
int Len;
pByte1 = (unsigned char *)pBuf;
pByteS = pByte1;
//从缓冲中搜索',',取出第7个','后面的第一个数字
while (Count<7)
{
if (*pByte1 == ',')
{
Count ++;
pByte1 ++;
continue;
}
while (*pByte1 != ',')
{
pByte1 ++;
if (*pByte1 == ',')
{
Count++;
pByte1 ++;
break;
}
if (pByte1-pByteS>=MaxInfoBlock)
return;
}
}
pByte2 = pByte1;
while (*pByte2 !=',') pByte2++; // 速度后面的,
Len = pByte2 - pByte1; // 速度的长度
for (int i=0 ; i < Len; i++)
ConStr[i] = pByte1[i];
ConStr[Len]=0;
GPSInfo.Velocity = atof(ConStr); // 速度
pByte1 = pByte2 + 1; // 方向
while (*pByte1 !=',') pByte1++; // 方向后面的,
pByte1 ++; // Day
GPSInfo.Day = (*pByte1 - '0') * 10 + (*(pByte1 + 1) - '0');
pByte1 += 2; // Month
GPSInfo.Month = (*pByte1 - '0') * 10 + (*(pByte1 + 1) - '0');
pByte1 += 2; // Year
GPSInfo.Year = (*pByte1 - '0') * 10 + (*(pByte1 + 1) - '0');
}
// 获得是否是3-D定位
//##ModelId=3FE8F86C0023
void CGpsComm::ProcessGSA(BYTE *pBuf, CGPSInfo &GPSInfo)
{
ASSERT( pBuf );
unsigned char *pByte1;
unsigned char *pByte2;
unsigned char *pByteS;
pByte1 = (unsigned char *)pBuf;
pByteS = pByte1;
pByte1 += 9;
GPSInfo.TribD = (*pByte1 == '3');
pByte2 = pByte1 + 1; // pByte2指向是否3-D定位后面的,
// 向后移动12个,
pByte1 = pByte2 + 1; // pByte1指向第一个卫星编号
int Count = 0;
while (Count <= 11)
{
if (*pByte1 == ',')
{
Count ++;
pByte1 ++;
continue;
}
while (*pByte1 != ',')
{
pByte1 ++;
if (*pByte1 == ',')
{
Count++;
pByte1 ++;
break;
}
if (pByte1-pByteS>=MaxInfoBlock)
return;
}
}
int Len = 0;
int i = 0;
char ConStr[10];
// pByte1是DOP数据
if (*pByte1 != ',')
{
pByte2 = pByte1 + 1; // DOP数据
while (*pByte2 != ',') pByte2++; // DOP后面的,
Len = pByte2 - pByte1; // 长度
for (i = 0; i < Len; i++) ConStr[i] = *(pByte1 + i);
ConStr[Len] = char(0);
GPSInfo.DOP = atof(ConStr); // DOP
}
else
{
pByte2 = pByte1;
GPSInfo.DOP = 0; // DOP
}
pByte1 = pByte2 + 1; // HDOP
if (*pByte1 != ',')
{
pByte2 = pByte1 + 1;
while (*pByte2 != ',') pByte2++; // HDOP后面的,
Len = pByte2 - pByte1; // 长度
for (i = 0; i < Len; i++) ConStr[i] = *(pByte1 + i);
ConStr[Len] = char(0);
GPSInfo.HDOP = atof(ConStr); // HDOP
}
else
{
pByte2 = pByte1;
GPSInfo.HDOP = 0; // HDOP
}
pByte1 = pByte2 + 1; // VDOP
if (*pByte1 != '*') // 2003-07-04 modified to prevent access violation
{
pByte2 = pByte1 + 1;
while (*pByte2 != '*') pByte2++; // VDOP后面的*
Len = pByte2 - pByte1; // 长度
for (i = 0; i < Len; i++) ConStr[i] = *(pByte1 + i);
ConStr[Len] = char(0);
GPSInfo.VDOP = atof(ConStr); // VDOP
}
else
GPSInfo.VDOP = 0; // VDOP
}
// 判断GPS是否有效
//##ModelId=3FE8F86C0017
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -