?? gps.cpp
字號(hào):
}
//逐一減少設(shè)備數(shù)
GpsSetNumDevices(dwDevices-1);
return bFound;
}
BOOL GpsRenameEntry(LPCTSTR lpszOldEntry, LPCTSTR lpszNewEntry)
{
if (GpsDeviceNameAlreadyExists(lpszNewEntry))
{
TRACE(_T("GpsRename, Cannot rename as entry already exists with new name %s\n"), lpszNewEntry);
return FALSE;
}
//通過通信入口名尋找設(shè)備
DWORD dwDevices = GpsGetNumDevices();
GPSDEVINFO* pGpsDevInfo = new GPSDEVINFO[dwDevices];
dwDevices = GpsEnumDevices(pGpsDevInfo, dwDevices);
BOOL bFound = FALSE;
for (DWORD i=0; i<dwDevices && !bFound; i++)
{
if (_tcsicmp(lpszOldEntry, pGpsDevInfo[i].szDeviceName) == 0)
bFound = TRUE;
}
delete [] pGpsDevInfo;
if (!bFound)
{
TRACE(_T("GpsRenameEntry, Failed to find GpsEntry for %s\n"), lpszOldEntry);
return FALSE;
}
GPSDEVINFO devInfo;
GetGpsDevice(i-1, &devInfo);
_tcscpy(devInfo.szDeviceName, lpszNewEntry);
SetGpsDevice(i-1, &devInfo);
return bFound;
}
BOOL GpsPropertiesDlg(HWND hWnd, LPCTSTR lpszEntry)
{
//通過通信入口名尋找設(shè)備
GPSDEVINFO devInfo;
if (!GpsGetDevice(lpszEntry, &devInfo))
{
TRACE(_T("GpsPropertiesDlg, Failed to find device\n"));
return FALSE;
}
CWnd* pParent = CWnd::FromHandle(hWnd);
CString sCaption;
AfxFormatString1(sCaption, IDS_PROPERTIES_CAPTION, lpszEntry);
CPropertiesPropertySheet propSheet(sCaption, pParent);
propSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
propSheet.m_Page1.m_bDefault = devInfo.bDefaultReceiver;
propSheet.m_Page1.m_dwStopBits = devInfo.wCommStopBits;
propSheet.m_Page1.m_dwPort = devInfo.wCommPort;
propSheet.m_Page1.m_dwParity = devInfo.wCommParity;
propSheet.m_Page1.m_dwBaudRate = devInfo.dwCommBaudRate;
propSheet.m_Page1.m_dwDataBits = devInfo.wCommDataBits;
int nResponse = propSheet.DoModal();
if (nResponse == IDOK)
{
devInfo.bDefaultReceiver = propSheet.m_Page1.m_bDefault;
devInfo.wCommStopBits = (WORD) propSheet.m_Page1.m_dwStopBits;
devInfo.wCommPort = (WORD) propSheet.m_Page1.m_dwPort;
devInfo.wCommParity = (WORD) propSheet.m_Page1.m_dwParity;
devInfo.dwCommBaudRate = propSheet.m_Page1.m_dwBaudRate;
devInfo.wCommDataBits = (WORD) propSheet.m_Page1.m_dwDataBits;
if (!GpsSetDevice(lpszEntry, &devInfo))
{
TRACE(_T("GpsPropertiesDlg, Failed to set device paramters\n"));
return FALSE;
}
return TRUE;
}
else
return FALSE;
}
HGPS GpsOpen(LPCTSTR lpszEntry)
{
//檢查并確認(rèn)至少有一個(gè)設(shè)備工作
GPSDEVINFO devInfo;
if (!GpsGetDevice(lpszEntry, &devInfo))
{
TRACE(_T("GpsOpen, Failed to find device\n"));
return (HGPS) INVALID_HANDLE_VALUE;
}
//創(chuàng)建新的實(shí)例
GPSHandle* pGpsHandle = new GPSHandle;
CopyMemory(&pGpsHandle->m_DevInfo, &devInfo, sizeof(devInfo));
pGpsHandle->m_bRunning = TRUE;
pGpsHandle->m_pThread = AfxBeginThread(GpsMonitorThead, pGpsHandle, THREAD_PRIORITY_IDLE, 0, CREATE_SUSPENDED);
if (!pGpsHandle->m_pThread)
{
TRACE(_T("GpsOpen, Failed to create background thread\n"));
delete pGpsHandle;
pGpsHandle = NULL;
return (HGPS) INVALID_HANDLE_VALUE;
}
pGpsHandle->m_pThread->m_bAutoDelete = FALSE;
pGpsHandle->m_pThread->ResumeThread();
//等待直到線程正確初始化
WaitForSingleObject(*pGpsHandle->m_pStartEvent, INFINITE);
if (!pGpsHandle->m_bRunning)
{
//等待直到線程退出
WaitForSingleObject(pGpsHandle->m_pThread->m_hThread, INFINITE);
delete pGpsHandle->m_pThread;
pGpsHandle->m_pThread = NULL;
delete pGpsHandle;
pGpsHandle = NULL;
TRACE(_T("GpsOpen, Failed to start background thread\n"));
return (HGPS) INVALID_HANDLE_VALUE;
}
return (HGPS) pGpsHandle;
}
BOOL GpsClose(HGPS hEntry)
{
if (hEntry == INVALID_HANDLE_VALUE)
{
TRACE(_T("GpsClose, invalid HGPS handle value\n"));
return FALSE;
}
GPSHandle* pGpsHandle = (GPSHandle*) hEntry;
if (pGpsHandle == NULL)
{
TRACE(_T("GpsClose, invalid HGPS handle value\n"));
return FALSE;
}
pGpsHandle->m_pKillEvent->SetEvent();
WaitForSingleObject(pGpsHandle->m_pThread->m_hThread, INFINITE);
delete pGpsHandle->m_pThread;
pGpsHandle->m_pThread = NULL;
delete pGpsHandle;
pGpsHandle = NULL;
return TRUE;
}
BOOL GpsGetPosition(HGPS hEntry, LPGPSPOSITION lpPosition)
{
if (hEntry == INVALID_HANDLE_VALUE)
{
TRACE(_T("GpsGetPostion, invalid HGPS handle value\n"));
return FALSE;
}
GPSHandle* pGpsHandle = (GPSHandle*) hEntry;
if (pGpsHandle == NULL)
{
TRACE(_T("GpsGetPostion, invalid HGPS handle value\n"));
return FALSE;
}
CopyMemory(lpPosition, &pGpsHandle->m_Position, sizeof(GPSPOSITION));
return TRUE;
}
DWORD GpsGetVersion()
{
return 0x0102; //版本
}
BOOL GpsGetDevice(LPCTSTR lpszEntry, LPGPSDEVINFO lpGpsDevInfo)
{
BOOL bFound = FALSE;
{
DWORD dwDevices = GpsGetNumDevices();
GPSDEVINFO* pGpsDevInfo = new GPSDEVINFO[dwDevices];
dwDevices = GpsEnumDevices(pGpsDevInfo, dwDevices);
for (DWORD i=0; i<dwDevices && !bFound; i++)
{
if (pGpsDevInfo[i].bDefaultReceiver)
{
CopyMemory(lpGpsDevInfo, &pGpsDevInfo[i], sizeof(pGpsDevInfo[i]));
bFound = TRUE;
}
}
delete [] pGpsDevInfo;
if (!bFound)
TRACE(_T("GpsGetDevice, Failed to find default device\n"));
}
else
{
DWORD dwDevices = GpsGetNumDevices();
GPSDEVINFO* pGpsDevInfo = new GPSDEVINFO[dwDevices];
dwDevices = GpsEnumDevices(pGpsDevInfo, dwDevices);
for (DWORD i=0; i<dwDevices && !bFound; i++)
{
if (_tcsicmp(lpszEntry, pGpsDevInfo[i].szDeviceName) == 0)
{
bFound = TRUE;
CopyMemory(lpGpsDevInfo, &pGpsDevInfo[i], sizeof(GPSDEVINFO));
}
}
delete [] pGpsDevInfo;
if (!bFound)
TRACE(_T("GpsGetDevice, Failed to find device %s\n"), lpszEntry);
}
return bFound;
}
BOOL GpsDeviceNameAlreadyExists(LPCTSTR lpszEntry)
{
GPSDEVINFO devinfo;
return GpsGetDevice(lpszEntry, &devinfo);
}
void GpsShowAboutBox(HWND hWnd)
{
CWnd* pParent = CWnd::FromHandle(hWnd);
CDialog dlg(IDD_ABOUT, pParent);
dlg.DoModal();
}
UINT GpsMonitorThead(LPVOID pParam)
{
CString sSentence;
CString sSerialString;
GPSHandle* pGpsHandle = (GPSHandle*) pParam;
BYTE szBuffer[100];
CGpsSerialPort SerialPort;
BOOL bOpen = SerialPort.Open(&pGpsHandle->m_DevInfo);
if (!bOpen)
{
TRACE(_T("GpsMonitorThread, Failed to open comms port\n"));
pGpsHandle->m_bRunning = FALSE;
}
//線程正確初始化,釋放GPSOpen并繼續(xù)
pGpsHandle->m_pStartEvent->SetEvent();
while (pGpsHandle->m_bRunning)
{
//如果終端事件激活,那么中斷監(jiān)視線程
if (::WaitForSingleObject(pGpsHandle->m_pKillEvent->m_hObject, 0) == WAIT_OBJECT_0)
{
pGpsHandle->m_bRunning = FALSE;
continue;
}
int nBytesRead = SerialPort.Read(szBuffer, sizeof(szBuffer));
if (nBytesRead > 0)
{
int nIndex = 0;
while (nIndex < nBytesRead)
{
sSerialString += ((TCHAR) szBuffer[nIndex]);
nIndex++;
}
// 是否取得了一個(gè)sentence
int nLocationLineFeed = sSerialString.Find(0x0A);
while (nLocationLineFeed != -1)
{
// 是的
sSentence = sSerialString.Left(nLocationLineFeed + 1);
// 修剪從串口取得的sentence
sSerialString = sSerialString.Right(sSerialString.GetLength() - (nLocationLineFeed+1));
nLocationLineFeed = sSerialString.Find(0x0A);
// 決定調(diào)用哪個(gè)列
if (sSentence.Left(6).Compare(_T("$GPRMC")) == 0)
{
CNmeaSentence sentence(sSentence);
CRMCResponse response;
if (response.Parse(sSentence) && response.m_IsDataValid)
{
pGpsHandle->m_Position.dwLongitude = CNmeaSentence::NmeaAngleToHundrethsOfSeconds(response.m_Longitude.Value);
pGpsHandle->m_Position.bEasting = (response.m_Longitude.Easting == East);
pGpsHandle->m_Position.dwLatitude = CNmeaSentence::NmeaAngleToHundrethsOfSeconds(response.m_Latitude.Value);
pGpsHandle->m_Position.bNorthing = (response.m_Latitude.Northing == North);
pGpsHandle->m_Position.dwBearing = CNmeaSentence::NmeaBearingToHundrethsOfDegrees(response.m_Bearing);
pGpsHandle->m_Position.dwSpeed = (DWORD) (response.m_SpeedOverGroundKnots * 185200); //There's 1.8519998 Km/s in 1 Knot
pGpsHandle->m_Position.wFixYear = response.m_Date.wYear;
pGpsHandle->m_Position.wFixMonth = response.m_Date.wMonth;
pGpsHandle->m_Position.wFixDay = response.m_Date.wDay;
pGpsHandle->m_Position.wFixHour = response.m_Time.wHour;
pGpsHandle->m_Position.wFixMinute = response.m_Time.wMinute;
pGpsHandle->m_Position.wFixSecond = response.m_Time.wSecond;
}
}
else if (sSentence.Left(6).Compare(_T("$GPGGA")) == 0)
{
CNmeaSentence sentence(sSentence);
CGGAResponse response;
if (response.Parse(sSentence))
{
pGpsHandle->m_Position.nSatellites = (WORD) response.m_nSatellites;
pGpsHandle->m_Position.dwAntennaAltitude = (DWORD) (response.m_AntennaAltitudeMeters * 100);
pGpsHandle->m_Position.wQualityIndicator = (WORD) response.m_GPSQuality;
}
}
}
}
}
return 1;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -