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

📄 gps.cpp

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

  //逐一减少设备数
  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;
  }

  //通过通信入口名寻找设备
  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)
{
  //通过通信入口名寻找设备
  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)
{
  //检查并确认至少有一个设备工作
  GPSDEVINFO devInfo;
  if (!GpsGetDevice(lpszEntry, &devInfo))
  {
    TRACE(_T("GpsOpen, Failed to find device\n"));
    return (HGPS) INVALID_HANDLE_VALUE;
  }

  //创建新的实例
  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;
  if (lpszEntry == NULL) //寻找缺省设备
  {
    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并继续
  pGpsHandle->m_pStartEvent->SetEvent();

  while (pGpsHandle->m_bRunning)
  {
    //如果终端事件激活,那么中断监视线程 
    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++;
      }

      // 是否取得了一个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);

        // 决定调用哪个列
        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;
}

⌨️ 快捷键说明

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