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

📄 gps.cpp

📁 VC++环境下的GPS全球定位系统源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      bFound = TRUE;
  }
  delete [] pGpsDevInfo;

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

  //resequence all devices above the deleted item by
  //moving each item down by one
  GPSDEVINFO devInfo;
  for (DWORD j=i; j<dwDevices; j++)
  {
    GetGpsDevice(j, &devInfo);
    SetGpsDevice(j-1, &devInfo);
  }

  //Decrement the number of devices by one
  GpsSetNumDevices(dwDevices-1);

  return bFound;
}


BOOL WINAPI 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;
  }

  //Find the device with the corresponding entry name
  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 WINAPI GpsPropertiesDlg(HWND hWnd, LPCTSTR lpszEntry)
{
  //Find the device with the corresponding entry name
  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 WINAPI GpsOpen(LPCTSTR lpszEntry)
{
  //check to make sure the device to open exists
  GPSDEVINFO devInfo;
  if (!GpsGetDevice(lpszEntry, &devInfo))
  {
    TRACE(_T("GpsOpen, Failed to find device\n"));
    return (HGPS) INVALID_HANDLE_VALUE;
  }

  //create a new handle instance
  GPSHandle* pGpsHandle = new GPSHandle;

  //In the future we will support sharing of GPS devices across process boundaries.
  //For the moment it will be 1 application at a time only.


  //this is the first client app to link to us, so spin of the
  //background thread which does the monitoring of the serial
  //port
  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; //we're in charge of cleanup
  pGpsHandle->m_pThread->ResumeThread();

  //wait until the thread is correctly initialised
  WaitForSingleObject(*pGpsHandle->m_pStartEvent, INFINITE);

  if (!pGpsHandle->m_bRunning)
  {
    //wait for the thread to exit
    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 WINAPI 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 WINAPI 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;
  }

  CSingleLock sl(&pGpsHandle->m_csPosition, TRUE); //serialize access to the position data
  CopyMemory(lpPosition, &pGpsHandle->m_Position, sizeof(GPSPOSITION));

  return TRUE;
}


DWORD WINAPI GpsGetVersion()
{
  return 0x0103;  //ie 1.03

  /* Revision History

  v1.0   29/12/1997 Original Release
  v1.01  20/01/1998 Number of problems fixed as described at top of this file
	v1.02  26/08/1998 Number of problems/updates as described at top of this file
  v1.03  11/06/1999 Now uses the WINAPI (_stdcall) calling convention

  */
}

BOOL WINAPI GpsGetDevice(LPCTSTR lpszEntry, LPGPSDEVINFO lpGpsDevInfo)
{
  BOOL bFound = FALSE;
  if (lpszEntry == NULL) //look for the default device
  {
    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 WINAPI GpsDeviceNameAlreadyExists(LPCTSTR lpszEntry)
{
  GPSDEVINFO devinfo;
  return GpsGetDevice(lpszEntry, &devinfo);
}


void WINAPI 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;
  }

  //thread is now initialised correctly, GpsOpen is free to continue
  pGpsHandle->m_pStartEvent->SetEvent();

  while (pGpsHandle->m_bRunning)
  {
    //if terminate event is active, terminate monitor thread 
    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++;
      }

      // See if we've got a sentence yet
      int nLocationLineFeed = sSerialString.Find(0x0A);

      while (nLocationLineFeed != -1) 
      {
        // Yes, there is a line feed in the string
        sSentence = sSerialString.Left(nLocationLineFeed + 1);
        // Now trim off the sentence from what came in from the serial port
        sSerialString = sSerialString.Right(sSerialString.GetLength() - (nLocationLineFeed+1));
        nLocationLineFeed = sSerialString.Find(0x0A);

        //determine which parser to call
        if (sSentence.Left(6).Compare(_T("$GPRMC")) == 0) 
        {
          CNmeaSentence sentence(sSentence);
          CRMCResponse response;
          if (response.Parse(sSentence) && response.m_IsDataValid)
          {
            CSingleLock sl(&pGpsHandle->m_csPosition, TRUE); //serialize access to the position data
            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))
          {
            CSingleLock sl(&pGpsHandle->m_csPosition, TRUE); //serialize access to the position data
            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;
          }
        }
      }
    }

    //Sleep for a time before we loop around, this avoids heavy CPU utilization
    Sleep(100);
  }

  return 1;
}

⌨️ 快捷键说明

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