📄 gpsutil.cpp
字号:
break;
}
index++;
}
i++;
}
// 将经纬度坐标转换为平面坐标
GpsToNodeXY(latitude, longitude, 0, latiX, longX);
return true;
}
//解析GPGSA
BOOL ParseGPGSA(char *sBuff)
{
if(strlen(sBuff) < 1)return false;
int nstart, nend;
int i;
int index;
int num;
nstart = -1;
nend = 0;
i = 1;
index = 0;
nactiveplanet = 0;
//从第一个开始,这样保证但开始有'$'符号时,前面可以解析成一个字符串
SetAllDeActive();
while(*(sBuff+i) != '\0')
{
if(*(sBuff+i) == ',')
{
nend = i;
if(nend == nstart)
{
strcpy(sTempBuff, "");
}
else
{
nstart++; //保证除掉','
memcpy(sTempBuff, sBuff+nstart, nend-nstart);
sTempBuff[nend-nstart] = '\0';
}
nstart = i;
//获取上一个解析
switch(index){
case 0:
//$GPGSA
break;
case 1:
// 'M' or 'A'
break;
case 2:
//是否定位
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
//12棵活动卫星
num = atoi(sTempBuff);
if(num!= 0 )
{
nactiveplanet++;
SetActive(num);
}
break;
default:
break;
}
index++;
}
i++;
}
return false;
}
void SetActive(int num)
{
for(int i = 0; i<nplanet; i++)
{
if(planet[i].PRVNo == num)
{
planet[i].bActive = true;
break;
}
}
}
void SetAllDeActive()
{
for(int i = 0; i<nplanet; i++)
{
planet[i].bActive = false;
}
}
//解析GPGSV
BOOL ParseGPGSV(char *sBuff)
{
if(strlen(sBuff) < 1)return false;
int nstart, nend;
int i;
int index;
int num;
int start_index;
nstart = -1;
nend = 0;
i = 1;
index = 0;
start_index=0;
//从第一个开始,这样保证但开始有'$'符号时,前面可以解析成一个字符串
while(*(sBuff+i) != '\0')
{
if(*(sBuff+i) == ',')
{
nend = i;
if(nend == nstart)
{
strcpy(sTempBuff, "");
}
else
{
nstart++; //保证除掉','
memcpy(sTempBuff, sBuff+nstart, nend-nstart);
sTempBuff[nend-nstart] = '\0';
}
nstart = i;
//获取上一个解析
switch(index){
case 0:
//GPGSV
break;
case 1:
//总段数
break;
case 2:
//分段数
num = atoi(sTempBuff);
start_index = (num-1)*4;
break;
case 3:
//星的总数
num = atoi(sTempBuff);
nplanet = num;
if(nplanet <0)nplanet =0;
if(nplanet >MAX_PLANET) nplanet = MAX_PLANET;
break;
case 4:
if(start_index>=0 && start_index <nplanet)
{
planet[start_index].PRVNo = atoi(sTempBuff);
}
break;
case 5:
if(start_index>=0 && start_index <nplanet)
{
planet[start_index].Elevation = atof(sTempBuff);
}
break;
case 6:
if(start_index>=0 && start_index <MAX_PLANET)
{
planet[start_index].Azimuth = atof(sTempBuff);
}
break;
case 7:
if(start_index>=0 && start_index <MAX_PLANET)
{
planet[start_index].Signal = GetBeforeAsterisk(sTempBuff);
start_index++;
index-=4;
}
break;
default:
break;
}
index++;
}
i++;
}
return true;
}
//解析GPRMC
BOOL ParseGPRMC(char *sBuff)
{
if(strlen(sBuff) < 1)return false;
int nstart, nend;
int i;
int index;
int num;
int start_index;
nstart = -1;
nend = 0;
i = 1;
index = 0;
start_index=0;
//从第一个开始,这样保证但开始有'$'符号时,前面可以解析成一个字符串
while(*(sBuff+i) != '\0')
{
if(*(sBuff+i) == ',')
{
nend = i;
if(nend == nstart)
{
strcpy(sTempBuff, "");
}
else
{
nstart++; //保证除掉','
memcpy(sTempBuff, sBuff+nstart, nend-nstart);
sTempBuff[nend-nstart] = '\0';
}
nstart = i;
//获取上一个解析
switch(index){
case 1:
num =atoi(sTempBuff);
nhour = num/10000;
nminute = (num-nhour*10000)/100;
nsecond = num-nhour*10000-nminute*100;
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
velocity = atof(sTempBuff);
break;
case 8:
direction = atof(sTempBuff);
break;
case 9:
num =atoi(sTempBuff);
nday = num/10000;
nmonth = (num - nday*10000)/100;
nyear = num- nday*10000 - nmonth*100;
nyear+=2000;
break;
default:
break;
}
index++;
}
i++;
}
return true;
}
//获取"???*hh"格式前面的值
double GetBeforeAsterisk(char *sBuff)
{
char str[256];
int len = strlen(sBuff);
char *pstr = strstr(sBuff,"*");
if(pstr == NULL)
return 0.;
else
{
if(pstr == sBuff)
return 0.0;
else
{
memcpy(str, sBuff, pstr-sBuff);
str[sBuff, pstr-sBuff] = '\0';
return atof(str);
}
}
return 0.;
}
//初始化一些参数
void InitialGPSCalue()
{
nyear = 2000;
nmonth = 2;
nday = 4;
nhour = 12;
nminute = 0;
nsecond = 0;
latitude = longitude = 0.; //纬度, 经度
latiX = longX = 0;
dire_latitude = 'N';
dire_longitude = 'E';
available = 0; //0 未定位, 1 定位, 2 查分定位
nplanet=0;//卫星总数
nactiveplanet=0; //活动卫星总数
velocity = 0; //速度, 节
HKEY hKey = NULL;
//ULONG dwDisposition;
/*
//Obtain the Key for HKEY_LOCAL_MACHINE\SOFTWARE\Digiark\Eztrip
if(ERROR_SUCCESS != RegOpenKeyEx(myRegInfo.hRootKey, myRegInfo.szKeyName, 0, KEY_READ, &hKey)) {
//Create the Key for HKEY_LOCAL_MACHINE\SOFTWARE\Digiark\Eztrip
if(ERROR_SUCCESS != RegCreateKeyEx(myRegInfo.hRootKey, myRegInfo.szKeyName, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition))
goto CLEANUP;
RegSetValueEx(hKey, myRegInfo.szValueName, 0, REG_DWORD, (BYTE*)myRegInfo.pszValue, 0);
}
//Obtain the Data for the above key
RegQueryValueEx(hKey, myRegInfo.szValueName, NULL, REG_DWORD, (LPBYTE)myRegInfo.pszValue, &cBytes))
CLEANUP:
if(hKey)
RegCloseKey(hKey);
*/
}
//以下部分为GPS轨迹记录与回放所用到的一些函数
//将nyear,nmonth,nday,nhour,nminute,nsecond转换为一个long型整数+一个__int8保存
//14+4+5+5+6+6=5*8
void getDate() {
dateh = nyear & 16383>>6;
datel = nyear & 63;
datel = (((( datel<<6 + nmonth & 15)<<4 + nday & 31)<<5 + nhour & 31)<<5 + nminute & 63) <<6 + nsecond & 63;
}
void setDate() {
nyear = dateh << 8 + datel >> 26;
nmonth = datel >> 22 & 15;
nday = datel >> 17 & 31;
nhour = datel >> 12 & 31;
nminute = datel >> 6 & 63;
nsecond = datel & 63;
}
//保存GPS位置信息到一个文件中
void saveGPSPos(LPWSTR filename) {
FILE *stream;
__int8 b = 1;
long s;
//首先打开轨迹记录文件
if( (stream = _wfopen( filename, L"a+" )) == NULL )
return;
fwrite(&b, sizeof(__int8), 1, stream);
getDate();
fwrite(&dateh, sizeof(__int8), 1, stream);
fwrite(&datel, sizeof(long), 1, stream);
s = latiX;
fwrite(&s, sizeof(long), 1, stream);
s = longX;
fwrite(&s, sizeof(long), 1, stream);
//最后关闭轨迹记录文件
fclose( stream );
}
//保存GPS速度、方向信息到一个文件中
void saveGPSSpeed(LPWSTR filename) {
FILE *stream;
__int8 b = 2;
short s;
//首先打开轨迹记录文件
if( (stream = _wfopen( filename, L"a+" )) == NULL )
return;
fwrite(&b, sizeof(__int8), 1, stream);
getDate();
fwrite(&dateh, sizeof(__int8), 1, stream);
fwrite(&datel, sizeof(long), 1, stream);
s = (int)velocity & 255;
fwrite(&s, sizeof(short), 1, stream);
s = (int)direction & 511;
fwrite(&s, sizeof(short), 1, stream);
//最后关闭轨迹记录文件
fclose( stream );
}
//读取一条GPS跟踪数据记录
void loadGPSPos(LPWSTR filename) {
FILE *stream;
__int8 b;
long s1;
short s2;
//首先打开轨迹记录文件
if( (stream = _wfopen( filename, L"r" )) == NULL )
return;
//如果定位成功则读取信息
if (!fseek(stream, f_tracePos, SEEK_SET)) {
//读取信息标识
fread(&b, sizeof(__int8), 1, stream );
//读取时间日期
fread(&dateh, sizeof(__int8), 1, stream);
fread(&datel, sizeof(long), 1, stream);
setDate();
//GPS位置信息
if (b==1) {
fread(&s1, sizeof(long), 1, stream);
latiX = s1;
fread(&s1, sizeof(long), 1, stream);
longX = s1;
f_tracePos += 14;
} else { //GPS速度、方向信息
fread(&s2, sizeof(short), 1, stream);
velocity = s2 & 255;
fread(&s2, sizeof(short), 1, stream);
direction = s2 & 511;
f_tracePos += 10;
}
//取得读取一条记录后当前文件指针所在的位置
//f_tracePos = ftell( stream );
}
fclose( stream );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -