標(biāo)題:
從串口獲得并分析GPS數(shù)據(jù)
[打印本頁]
作者:
51黑apple
時間:
2016-6-21 17:20
標(biāo)題:
從串口獲得并分析GPS數(shù)據(jù)
目前GPS(全球定位系統(tǒng))定位應(yīng)用市場日趨成熟,正在進入應(yīng)用的高速發(fā)展時期?吹秸搲锊粩嘤腥颂釂栮P(guān)于GPS的問題,F(xiàn)將個人對GPS的了解寫出來跟大家一塊探討。
1、 GPS應(yīng)用簡介
近年來GPS系統(tǒng),已經(jīng)在大地測繪、海上漁用、車輛定位監(jiān)控、建筑、農(nóng)業(yè)等各個領(lǐng)域得到廣泛應(yīng)用。從九十年代我國引進GPS定位技術(shù)開始,經(jīng)過十多年的市場培育,GPS定位應(yīng)用進入了發(fā)展的最好時機,未來十年基于GPS的應(yīng)用將會改變我們的生活和工作方式。
目前市場上的大部分GPS接受模塊都是通過RS232串口與MCU進行數(shù)據(jù)傳輸?shù)。這些數(shù)據(jù)包括經(jīng)度、緯度、海拔高度、時間、衛(wèi)星使用情況等基本信息。開發(fā)人員再依據(jù)這些基本數(shù)據(jù),進行數(shù)據(jù)處理來完成整套的定位系統(tǒng)軟件。
2、 數(shù)據(jù)格式
在進行數(shù)據(jù)接受編程之前,先介紹一下該模塊的數(shù)據(jù)格式。它支持NMEA-0183輸出格式。信息如下:
GGA位置測定系統(tǒng)定位資料(Global Positioning System Fix Data)
GSV 導(dǎo)航衛(wèi)星資料(GNSS Satellites in View)
RMC導(dǎo)航衛(wèi)星特定精簡資料(Recommended Minimum Specific GNSS Data)
VTG 方向及速度等相關(guān)資料(Course Over Ground and Ground Speed)
由于文章篇幅問題,筆者在這里只以接收GGA數(shù)據(jù)為例,格式如下:
$GPGGA,hhmmss,dddmm.mmmm,a,dddmm.mmmm,a,x,xx,x.x,x.x,M,,M,x.x,xxxx*CS
例:$GPGGA,033744,2446.5241,N,12100.1536,E,1,10,0.8,133.4,M,,,,*1F
說明見表:
上面例子中,我們可讀出位置信息:北緯24度46.5241分,西經(jīng)121度00.1536分
格林威治時間:3點37分44秒
3 部分程序代碼(c++)
//初始化串口
//入口:strComm(串口名)
//返回:TRUE(成功);FALSE(失敗)
BOOL CGPSDlg::InitComm(CString strComm)
{
int i;
DCB dcb;
COMMTIMEOUTS TimeOuts;
for (i=0; i<3; i++) //串口最多初始化3次
{
m_hComm = CreateFile(strComm, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (m_hComm != INVALID_HANDLE_VALUE)
break;
}
if (i == 3) //串口初始化失敗
{
AfxMessageBox("串口初始化失敗...");
return FALSE;
}
SetupComm(m_hComm, MAXLENGTH, MAXLENGTH); //設(shè)置發(fā)送接收緩沖區(qū)大小
TimeOuts.ReadIntervalTimeout = 0; //設(shè)定5個超時參數(shù)
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 500;
TimeOuts.WriteTotalTimeoutMultiplier = 0;
TimeOuts.WriteTotalTimeoutConstant = 500;
SetCommTimeouts(m_hComm, &TimeOuts); //設(shè)置超時參數(shù)
GetCommState(m_hComm, &dcb); //獲得通信狀態(tài)
dcb.fAbortOnError = FALSE; //有錯誤不停止
dcb.BaudRate = CBR_4800; //波特率4800
dcb.ByteSize = 8; //8位
dcb.Parity = NOPARITY; //奇校驗
dcb.StopBits = ONESTOPBIT; //1位停止位
SetCommState(m_hComm, &dcb); //設(shè)置通信狀態(tài)
PurgeComm(m_hComm, PURGE_TXCLEAR|PURGE_RXCLEAR); //清空發(fā)送和接收緩沖區(qū)
return TRUE;
}
//獲得GPS參數(shù)
//注意:從GPS接收到的字符串已經(jīng)在m_strRecv中,由于是定時接收,所以在這個字符串的頭和尾都可能存在
// 不完整的NMEA輸出字符串,在處理時要特別注意
//返回:TRUE(格式正確);FALSE(格式錯誤)
BOOL CGPSDlg::GetGPSParam()
{
int i,j;
CString str,strNEMA;
//先判斷是否接收到數(shù)據(jù)
if (m_strRecv.IsEmpty())
return FALSE;
//若字符串不是以'
[/color][i][/i]
開頭的,必須刪掉這部分不完整的
if (m_strRecv[0] != '
[/color][i][/i]
)
{
i = m_strRecv.Find('\n', 0);
if (i == -1)
return FALSE; //尾部未接收完整,必須等接收完后才能刪除
m_strRecv.Delete(0, i+1); //尾部已接收完整(尾部為\r\n結(jié)束),刪除不完整的部分
}
//截取完整的NMEA-0183輸出語句(m_strRecv中可能有多條語句,每條間以\r\n分隔)
for (;;)
{
i = m_strRecv.Find('\n', 0);
if (i == -1)
break; //所有的完整輸出語句都已經(jīng)處理完畢,退出循環(huán)
//截取完整的NMEA-0183輸出語句
strNEMA = m_strRecv.Left(i+1);
m_strRecv.Delete(0, i+1);
//下面對各種輸出語句進行分別處理
if (strNEMA.Find("$GPRMC",0) == 0)
{
//該輸出語句中的各項以','分隔
for (i=j=0; strNEMA[i]!='\r'; i++) //j為逗號的計數(shù)器
{
if (strNEMA[i] == ',')
{
j++;
str = "";
for (i++; strNEMA[i]!=','&&strNEMA[i]!='\r'; i++)
str += strNEMA[i]; //str為某項的值
i--;
//對各項數(shù)據(jù)分別處理
switch (j)
{
case 1: //時間(UTC)
m_strTime = str.Left(6);
m_strTime.Insert(2, ':');
m_strTime.Insert(5, ':');
break;
case 2: //狀態(tài)(A-數(shù)據(jù)有效;V-數(shù)據(jù)無效,還未定位)
if (str == "A")
m_strStatus = "有效數(shù)據(jù)";
else if(str == "V")
m_strStatus = "正在定位...";
else
m_strStatus = "非法數(shù)據(jù)格式";
break;
case 3: //緯度(ddmm.mmmm)
str.Insert(2, "度");
str += "分";
m_strLatitude = str;
break;
case 4: //緯度指示(N-北緯;S-南緯)
if (str == "N")
m_strLatitude.Insert(0, "北緯");
else
m_strLatitude.Insert(0, "南緯");
break;
case 5: //經(jīng)度(dddmm.mmmm)
str.Insert(3, "度");
str += "分";
m_strLongitude = str;
break;
case 6: //經(jīng)度指示(E-東經(jīng);W-西經(jīng))
if (str == "E")
m_strLongitude.Insert(0, "東經(jīng)");
else
m_strLongitude.Insert(0, "西經(jīng)");
break;
case 7: //速度(單位:節(jié))
m_strSpeed = str;
break;
case 8: //航向(單位:度)
m_strCourse = str;
break;
case 9: //日期(UTC)
m_strDate = "";
m_strDate += "20";
m_strDate += str[4];
m_strDate += str[5];
m_strDate += "-";
m_strDate += str[2];
m_strDate += str[3];
m_strDate += "-";
m_strDate += str[0];
m_strDate += str[1];
break;
default:
break;
}
}
}
}
else if (strNEMA.Find("$GPGGA",0) == 0)
{
}
else if (strNEMA.Find("$GPGSA",0) == 0)
{
}
else if (strNEMA.Find("$GPGSV",0) == 0)
{
}
else if (strNEMA.Find("$GPGLL",0) == 0)
{
}
else if (strNEMA.Find("$GPVTG",0) == 0)
{
}
else
return FALSE; //格式錯誤
}
return TRUE;
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1