⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gps.cpp

📁 本书所有案例均需要单独配置
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////////////////////
//GPS.cpp
/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////  .h文件  //////////////////////////////////
#include "stdafx.h"
#include "gps.h"
#include "serport.h"
#include "resource.h"
#include "nmea.h"
#include "math.h"
#include "gpssetup.h"



//////////////////////////////////  宏定义 //////////////////////////
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif


////////////////////////////////// 本地变量 /////////////////////////////////////
class GPSHandle
{
public:
  GPSHandle();
  ~GPSHandle();

  GPSPOSITION  m_Position;    //实际数据
  CEvent*      m_pKillEvent;  //信号线程推出事件
  CEvent*      m_pStartEvent; //后台线程开始事件
  GPSDEVINFO   m_DevInfo;     //使用的通信端口设置
  BOOL         m_bRunning;    //后台线程是否运行
  CWinThread*  m_pThread;     //指向后台线程的指针
};


BOOL GetGpsDevice(DWORD dwDevice, LPGPSDEVINFO lpGpsDevInfo);
BOOL SetGpsDevice(DWORD dwDevice, LPCGPSDEVINFO lpGpsDevInfo, BOOL bCheckDefault = TRUE);
BOOL SetGpsNumDevices(DWORD dwDevices);
UINT GpsMonitorThead(LPVOID pParam);


////////////////////////////////// 执行 /////////////////////////////

GPSHandle::GPSHandle()
{
  ZeroMemory(&m_Position, sizeof(GPSPOSITION));
  m_pKillEvent = new CEvent(FALSE, TRUE);
  m_pStartEvent = new CEvent(FALSE, TRUE);
  m_pThread = NULL;
}


GPSHandle::~GPSHandle()
{
  delete m_pStartEvent;
  m_pStartEvent = NULL;

  delete m_pKillEvent;
  m_pKillEvent = NULL;
} 

BOOL GetGpsDevice(DWORD dwDevice, LPGPSDEVINFO lpGpsDevInfo)
{
  BOOL bSuccess = FALSE;
  HKEY hKey;

  CString sValueKey;
  sValueKey.Format(_T("SOFTWARE\\PJ Naughter\\GPS32\\%d"), dwDevice);

  LONG nError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sValueKey, 0, KEY_ALL_ACCESS, &hKey);
  if (nError == ERROR_SUCCESS) 
  {
    DWORD dwType;
    DWORD dwSize;

    RegQueryValueEx(hKey, _T("DeviceName"), 0, &dwType, NULL, &dwSize);
    TCHAR* pszName = new TCHAR[dwSize / sizeof(TCHAR)];
    RegQueryValueEx(hKey, _T("DeviceName"), 0, &dwType, (LPBYTE) pszName, &dwSize);
    _tcscpy(lpGpsDevInfo->szDeviceName, pszName);
    delete [] pszName;

    RegQueryValueEx(hKey, _T("DefaultReceiver"), 0, &dwType, (LPBYTE) &lpGpsDevInfo->bDefaultReceiver, &dwSize);

    RegQueryValueEx(hKey, _T("Port"), 0, &dwType, (LPBYTE) &lpGpsDevInfo->wCommPort, &dwSize);

    RegQueryValueEx(hKey, _T("BaudRate"), 0, &dwType, (LPBYTE) &lpGpsDevInfo->dwCommBaudRate, &dwSize);

    RegQueryValueEx(hKey, _T("DataBits"), 0, &dwType, (LPBYTE) &lpGpsDevInfo->wCommDataBits, &dwSize);

    RegQueryValueEx(hKey, _T("StopBits"), 0, &dwType, (LPBYTE) &lpGpsDevInfo->wCommStopBits, &dwSize);

    bSuccess = TRUE;

    RegCloseKey(hKey);
  }
	else
	{
	  TRACE(_T("GetGpsDevice, Failed to open a registry key, Error was: %d\n"), nError);
	}

  return bSuccess;
}


BOOL GpsSetNumDevices(DWORD dwDevices)
{
  BOOL bSuccess = FALSE;
  HKEY hKey;
  DWORD dwDisposition;

  LONG nError = RegCreateKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\PJ Naughter\\GPS32"), 0, _T(""),
                               REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
  if (nError == ERROR_SUCCESS) 
  {
    RegSetValueEx(hKey, _T("NumberOfDevices"), 0, REG_DWORD, (CONST BYTE*) &dwDevices, sizeof(DWORD));
    
    RegCloseKey(hKey);

    bSuccess = TRUE;
  }
	else
	{
    TRACE(_T("GpsSetNumDevices, Failed in call to create a registry key, Error was: %d\n"), nError);
	}

  return bSuccess;
}

BOOL SetGpsDevice(DWORD dwDevice, LPCGPSDEVINFO lpDevice, BOOL bCheckDefault)
{
  if (bCheckDefault && lpDevice->bDefaultReceiver)
  {
    //将所有设备设为缺省
    for (DWORD i=0; i<GpsGetNumDevices(); i++)
    {
      GPSDEVINFO devInfo;
      GetGpsDevice(i, &devInfo);
      devInfo.bDefaultReceiver = FALSE; 
      SetGpsDevice(i, &devInfo, FALSE);
    }
  }

  BOOL bSuccess = FALSE;
  HKEY hKey;
  DWORD dwDisposition;
  CString sValueKey;
  sValueKey.Format(_T("SOFTWARE\\PJ Naughter\\GPS32\\%d"), dwDevice);
	LONG nError = RegCreateKeyEx(HKEY_LOCAL_MACHINE, sValueKey, 0, _T(""), REG_OPTION_NON_VOLATILE,
                               KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
  if (nError == ERROR_SUCCESS) 
  {
    RegSetValueEx(hKey, _T("DeviceName"), 0, REG_SZ, (CONST BYTE*) lpDevice->szDeviceName, _tcslen(lpDevice->szDeviceName));

    RegSetValueEx(hKey, _T("DefaultReceiver"), 0, REG_DWORD, (CONST BYTE*) &lpDevice->bDefaultReceiver, sizeof(BOOL));

    DWORD dwData = lpDevice->wCommPort;
    RegSetValueEx(hKey, _T("Port"), 0, REG_DWORD, (CONST BYTE*) &dwData, sizeof(DWORD));

    RegSetValueEx(hKey, _T("BaudRate"), 0, REG_DWORD, (CONST BYTE*) &lpDevice->dwCommBaudRate, sizeof(DWORD));

    dwData = lpDevice->wCommDataBits;
    RegSetValueEx(hKey, _T("DataBits"), 0, REG_DWORD, (CONST BYTE*) &dwData, sizeof(DWORD));

    dwData = lpDevice->wCommStopBits;
    RegSetValueEx(hKey, _T("StopBits"), 0, REG_DWORD, (CONST BYTE*) &dwData, sizeof(DWORD));

    RegCloseKey(hKey);

    bSuccess = TRUE;
  }
	else
	{
    TRACE(_T("SetGPSDevice, Failed in call to create a registry key, Error was: %d\n"), nError);
	}

  //确认至少有一个设备是缺省的
  if (bCheckDefault && !lpDevice->bDefaultReceiver)
  {
    DWORD dwDevices = GpsGetNumDevices();
    GPSDEVINFO* pGpsDevInfo = new GPSDEVINFO[dwDevices];
    dwDevices = GpsEnumDevices(pGpsDevInfo, dwDevices);
    BOOL bFound = FALSE;
    for (DWORD i=0; i<dwDevices && !bFound; i++)
    {
      if (pGpsDevInfo[i].bDefaultReceiver)
        bFound = TRUE;
    }
    delete [] pGpsDevInfo;

    if (!bFound)
    {
      TRACE(_T("SetGpsDevice, Found no device with default receiver attribute\n"));
      TRACE(_T("  making first device the default\n"));

      GPSDEVINFO devInfo;
      GetGpsDevice(0, &devInfo);
      devInfo.bDefaultReceiver = TRUE; 
      SetGpsDevice(0, &devInfo, FALSE);
    }
  }

  return bSuccess;
}


BOOL GpsSetDevice(LPCTSTR lpszEntry, LPCGPSDEVINFO lpGpsDevInfo)
{
  //通过通信入口名寻找设备
  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(lpszEntry, pGpsDevInfo[i].szDeviceName) == 0)
      bFound = TRUE;
  }
  delete [] pGpsDevInfo;

  if (!bFound)
  {
    TRACE(_T("GpsSetDevice, Failed to find GpsEntry for %s\n"), lpszEntry);
    return FALSE;
  }

  return SetGpsDevice(i-1, lpGpsDevInfo);
}


BOOL GpsShowControlPanel()
{
#ifdef _DEBUG
  #ifdef _UNICODE
  	UINT nExec = ::WinExec("GPS100UD.EXE", SW_SHOW);
  #else
		UINT nExec = ::WinExec("GPS100D.EXE", SW_SHOW);
  #endif
#else
  #ifdef _UNICODE
  	UINT nExec = ::WinExec("GPS100U.EXE", SW_SHOW);
  #else
		UINT nExec = ::WinExec("GPS100.EXE", SW_SHOW);
	#endif
#endif
  if (nExec <= 31)
	  TRACE(_T("GpsShowControlPanel, Failed in call to WinExec GPS executable, WinExec returns: %d"), nExec);
  
	return (nExec > 31);
}


BOOL GpsCreateEntry(HWND hWnd)
{
  BOOL bSuccess = FALSE;

  CWnd* pParent = CWnd::FromHandle(hWnd);

	CInstallPropertySheet propSheet(IDS_GPS_SETUP, pParent);
  propSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
	int nResponse = propSheet.DoModal();

  if (nResponse == ID_WIZFINISH)
  {
    DWORD dwDevices = GpsGetNumDevices();

    GPSDEVINFO GpsInfo;
    _tcscpy(GpsInfo.szDeviceName, propSheet.m_Page3.m_sName); 
    GpsInfo.bDefaultReceiver = (propSheet.m_Page3.m_nMakeDefault == 0);
    GpsInfo.wCommPort = (WORD) propSheet.m_Page2.m_dwPort;
    GpsInfo.dwCommBaudRate = propSheet.m_Page2.m_dwBaudRate;
    GpsInfo.wCommDataBits = 8; 
    GpsInfo.wCommParity = GpsParityNone;
    GpsInfo.wCommStopBits = GpsStopBits1;

    bSuccess = SetGpsDevice(dwDevices, &GpsInfo);

    //逐一增加设备数
    GpsSetNumDevices(dwDevices + 1);
  }

  return bSuccess;
}


DWORD GpsGetNumDevices()
{
  DWORD dwDevices = 0;
  DWORD dwType;
  DWORD dwSize = sizeof(DWORD);
  HKEY hKey;

  LONG nError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\PJ Naughter\\GPS32"),
                             0, KEY_ALL_ACCESS, &hKey);
  if (nError == ERROR_SUCCESS) 
  {
    RegQueryValueEx(hKey, _T("NumberOfDevices"), 0, &dwType, (LPBYTE) &dwDevices, &dwSize);
    RegCloseKey(hKey);
  }
	else
	{
    TRACE(_T("GpsGetNumDevices, Failed in call to open the registry, Error was: %d\n"), nError);
	}


  return dwDevices;
}


DWORD GpsEnumDevices(LPGPSDEVINFO lpRasDevInfo, DWORD dwRequestedDevices)
{
  DWORD dwDevicesToRetreive = min(GpsGetNumDevices(), dwRequestedDevices);

  for (DWORD i=0; i<dwDevicesToRetreive; i++)
    GetGpsDevice(i, &lpRasDevInfo[i]);

  return dwDevicesToRetreive;
}


BOOL GpsDeleteEntry(LPCTSTR lpszEntry)
{
//通过通信入口名寻找设备
  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(lpszEntry, pGpsDevInfo[i].szDeviceName) == 0)
      bFound = TRUE;
  }
  delete [] pGpsDevInfo;

  if (!bFound)
  {
    TRACE(_T("GpsDeleteEntry, Failed to find GpsEntry for %s\n"), lpszEntry);
    return FALSE;
  }
  GPSDEVINFO devInfo;
  for (DWORD j=i; j<dwDevices; j++)
  {
    GetGpsDevice(j, &devInfo);
    SetGpsDevice(j-1, &devInfo);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -