经常需求使用 GPS 串口 NMEA 解析的功能,写了一段代码来完成引功能。
分享一下,大家一起学习。
头文件:#ifndef _GPS_MONITER_HH_
#define _GPS_MONITER_HH_
BOOL InitSerailPort(CString csSerialPort,LPVOID pParent = NULL);
DWORD WINAPI ReadNMEAThread(LPVOID lpParameter);
void SetSystemTimeFormUTC(CString csDate,CString csUTCTime);
void DeinitSerialPort(void);
#endif
源文件:#include "stdafx.h"
#include "GPSMoniter.h"
#define SATTATOLNUMBER32
// 用于在 GPS 监控界面显示 NMEA 信息
char gcBuff[4096];
CString gcsTime;
CString gcsDate;
int gdSignalNumber[SATTATOLNUMBER];
CString csLat;
CString csLatdir;
CString csLon;
CString csLondir;
CString csAltitude;
CString csSpeed;
CString csOrientation;
int nNumDisplayed;
int giGSVSatNumber;
HANDLE ghCommHandle;
HANDLE nmeathread_hand;// Global handle to the NMEA reading thread
CString gcsGPSState;//定位: A; 导航: V
CString gcsTimeOp;
CString gcsLatField;
CString gcsLonField;
int giResult;
int giSalNumber;
int giGSVCurrentPackage;
int giHourDiff;
BOOL InitSerailPort(CString csSerialPort,LPVOID pParent)
{
DCB commDCB;
COMMTIMEOUTS timeouts;
ghCommHandle = CreateFile(csSerialPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);
if(INVALID_HANDLE_VALUE == ghCommHandle)
{
RETAILMSG(1, (TEXT("Opening GPS %s failed: %d!rn"),csSerialPort,(int)GetLastError()));
return FALSE;
}
commDCB.DCBlength = sizeof (DCB);
if(!GetCommState(ghCommHandle,&commDCB))
{
CloseHandle(ghCommHandle);
ghCommHandle = INVALID_HANDLE_VALUE;
RETAILMSG(1,(L"Failed in getting GPS %s DCB settings: %d!rn",csSerialPort,(int)GetLastError()));
return FALSE;
}
commDCB.DCBlength = sizeof(DCB);
commDCB.BaudRate = 9600; // Current baud
commDCB.ByteSize = 8; // Number of bits/bytes, 4-8
commDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
commDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Setting serial port to Centrality speicifcations
if(!SetCommState(ghCommHandle,&commDCB))
{
CloseHandle(ghCommHandle);
ghCommHandle = INVALID_HANDLE_VALUE;
RETAILMSG(1,(L"Error in trying to set GPS %s DCB settings: %d!rn",csSerialPort,(int)GetLastError()));
return FALSE;
}
// Get the default timeout settings for port
if(!GetCommTimeouts(ghCommHandle, &timeouts))
{
CloseHandle(ghCommHandle);
ghCommHandle = INVALID_HANDLE_VALUE;
RETAILMSG(1,(L"Fail