|
目前GPS(全球定位系統(tǒng))定位應(yīng)用市場(chǎng)日趨成熟,正在進(jìn)入應(yīng)用的高速發(fā)展時(shí)期。看到論壇里不斷有人提問(wèn)關(guān)于GPS的問(wèn)題。現(xiàn)將個(gè)人對(duì)GPS的了解寫出來(lái)跟大家一塊探討。
1、 GPS應(yīng)用簡(jiǎn)介
近年來(lái)GPS系統(tǒng),已經(jīng)在大地測(cè)繪、海上漁用、車輛定位監(jiān)控、建筑、農(nóng)業(yè)等各個(gè)領(lǐng)域得到廣泛應(yīng)用。從九十年代我國(guó)引進(jìn)GPS定位技術(shù)開(kāi)始,經(jīng)過(guò)十多年的市場(chǎng)培育,GPS定位應(yīng)用進(jìn)入了發(fā)展的最好時(shí)機(jī),未來(lái)十年基于GPS的應(yīng)用將會(huì)改變我們的生活和工作方式。
目前市場(chǎng)上的大部分GPS接受模塊都是通過(guò)RS232串口與MCU進(jìn)行數(shù)據(jù)傳輸?shù)摹_@些數(shù)據(jù)包括經(jīng)度、緯度、海拔高度、時(shí)間、衛(wèi)星使用情況等基本信息。開(kāi)發(fā)人員再依據(jù)這些基本數(shù)據(jù),進(jìn)行數(shù)據(jù)處理來(lái)完成整套的定位系統(tǒng)軟件。
2、 數(shù)據(jù)格式
在進(jìn)行數(shù)據(jù)接受編程之前,先介紹一下該模塊的數(shù)據(jù)格式。它支持NMEA-0183輸出格式。信息如下:
GGA位置測(cè)定系統(tǒng)定位資料(Global Positioning System Fix Data)
GSV 導(dǎo)航衛(wèi)星資料(GNSS Satellites in View)
RMC導(dǎo)航衛(wèi)星特定精簡(jiǎn)資料(Recommended Minimum Specific GNSS Data)
VTG 方向及速度等相關(guān)資料(Course Over Ground and Ground Speed)
由于文章篇幅問(wèn)題,筆者在這里只以接收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
說(shuō)明見(jiàn)表:

上面例子中,我們可讀出位置信息:北緯24度46.5241分,西經(jīng)121度00.1536分
格林威治時(shí)間:3點(diǎn)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個(gè)超時(shí)參數(shù)
- TimeOuts.ReadTotalTimeoutMultiplier = 0;
- TimeOuts.ReadTotalTimeoutConstant = 500;
- TimeOuts.WriteTotalTimeoutMultiplier = 0;
- TimeOuts.WriteTotalTimeoutConstant = 500;
- SetCommTimeouts(m_hComm, &TimeOuts); //設(shè)置超時(shí)參數(shù)
-
- GetCommState(m_hComm, &dcb); //獲得通信狀態(tài)
- dcb.fAbortOnError = FALSE; //有錯(cuò)誤不停止
- dcb.BaudRate = CBR_4800; //波特率4800
- dcb.ByteSize = 8; //8位
- dcb.Parity = NOPARITY; //奇校驗(yàn)
- 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中,由于是定時(shí)接收,所以在這個(gè)字符串的頭和尾都可能存在
- // 不完整的NMEA輸出字符串,在處理時(shí)要特別注意
- //返回:TRUE(格式正確);FALSE(格式錯(cuò)誤)
- BOOL CGPSDlg::GetGPSParam()
- {
- int i,j;
- CString str,strNEMA;
-
- //先判斷是否接收到數(shù)據(jù)
- if (m_strRecv.IsEmpty())
- return FALSE;
- //若字符串不是以'
- [/color][i][/i]
- 開(kā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輸出語(yǔ)句(m_strRecv中可能有多條語(yǔ)句,每條間以\r\n分隔)
- for (;;)
- {
- i = m_strRecv.Find('\n', 0);
- if (i == -1)
- break; //所有的完整輸出語(yǔ)句都已經(jīng)處理完畢,退出循環(huán)
-
- //截取完整的NMEA-0183輸出語(yǔ)句
- strNEMA = m_strRecv.Left(i+1);
- m_strRecv.Delete(0, i+1);
- //下面對(duì)各種輸出語(yǔ)句進(jìn)行分別處理
- if (strNEMA.Find("$GPRMC",0) == 0)
- {
- //該輸出語(yǔ)句中的各項(xiàng)以','分隔
- for (i=j=0; strNEMA[i]!='\r'; i++) //j為逗號(hào)的計(jì)數(shù)器
- {
- if (strNEMA[i] == ',')
- {
- j++;
- str = "";
- for (i++; strNEMA[i]!=','&&strNEMA[i]!='\r'; i++)
- str += strNEMA[i]; //str為某項(xiàng)的值
- i--;
-
- //對(duì)各項(xiàng)數(shù)據(jù)分別處理
- switch (j)
- {
- case 1: //時(shí)間(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ù)無(wú)效,還未定位)
- 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; //格式錯(cuò)誤
- }
- return TRUE;
- }
復(fù)制代碼
|
|