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

📄 testgpsdlg.cpp

📁   这是一个简单的GPSNMEA-0183协议解析库开发平台
💻 CPP
字号:
#include "stdafx.h"
#include "TestGPS.h"
#include "TestGPSDlg.h"
//#pragma   comment(lib,"GPS103d.lib")
#define _AFX_ENABLE_INLINES 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CTestGPSDlg::CTestGPSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestGPSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestGPSDlg)
	m_Edit = _T("");
	//}}AFX_DATA_INIT
	m_hGps = NULL;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestGPSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestGPSDlg)
	DDX_Control(pDX, IDC_STATEWAY, m_ctrlStateWay);
	DDX_Control(pDX, IDC_STATETYPE, m_ctrlStateType);
	DDX_Control(pDX, IDC_STATE, m_ctrlState);
	DDX_Control(pDX, IDC_EDIT1, m_EditCtrol);
	DDX_Control(pDX, IDC_VIEWSATELLITES, m_ctrlViewSatellites);
	DDX_Control(pDX, IDC_SPEED, m_ctrlSpeed);
	DDX_Control(pDX, IDC_TIME, m_ctrlTime);
	DDX_Control(pDX, IDC_SATELLITES, m_ctrlSatellites);
	DDX_Control(pDX, IDC_QUALITY, m_ctrlQuality);
	DDX_Control(pDX, IDC_LONGITUDE, m_ctrlLongitude);
	DDX_Control(pDX, IDC_LATITUDE, m_ctrlLatitude);
	DDX_Control(pDX, IDC_BEARING, m_ctrlBearing);
	DDX_Control(pDX, IDC_ALTITUDE, m_ctrlAltitude);
	DDX_Control(pDX, IDC_GPSOPEN, m_ctrlOpen);
	DDX_Control(pDX, IDC_GPSCLOSE, m_ctrlClose);
	DDX_Text(pDX, IDC_EDIT1, m_Edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestGPSDlg, CDialog)
	//{{AFX_MSG_MAP(CTestGPSDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SHOWSETUP, OnShowSetup)
	ON_BN_CLICKED(IDC_GPSCLOSE, OnGpsClose)
	ON_BN_CLICKED(IDC_GPSOPEN, OnGpsOpen)
	ON_WM_TIMER()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CTestGPSDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.
void CTestGPSDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestGPSDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestGPSDlg::OnShowSetup() 
{
  GpsShowControlPanel();
}

void CTestGPSDlg::OnGpsClose() 
{
    KillTimer(m_nTimerID);
	m_ctrlClose.EnableWindow(FALSE);

	EnableGpsPositionControl(FALSE);

  if ((m_hGps != NULL) && (m_hGps != INVALID_HANDLE_VALUE) && !GpsClose(m_hGps))
    AfxMessageBox(_T("Failed in Call to GpsClose"));
  m_hGps = NULL;
  m_ctrlOpen.EnableWindow(TRUE);
}

void CTestGPSDlg::OnGpsOpen() 
{
  CWnd *pStaticWnd;

  m_ctrlOpen.EnableWindow(FALSE);
  pStaticWnd = GetDlgItem(IDC_EDIT1);
//  m_EditCtrol.SetReadOnly(TRUE);
  m_hGps = GpsOpen(NULL, pStaticWnd);
  if ((m_hGps != NULL) && (m_hGps != INVALID_HANDLE_VALUE))
  {
    m_ctrlClose.EnableWindow(TRUE);	

    EnableGpsPositionControl(TRUE);
    m_nTimerID = SetTimer(1, 1000, NULL);
  }
  else
  {
    AfxMessageBox(_T("打开串口失败,请重试!"));
    m_ctrlOpen.EnableWindow(TRUE);
  }
}

void CTestGPSDlg::OnTimer(UINT /*nIDEvent*/) 
{
  GPSPOSITION gpsPos;
  GPSSTATE gpsState;
  CString sText = "";

  if ((m_hGps != NULL) && (m_hGps != INVALID_HANDLE_VALUE) && GpsGetState(m_hGps, &gpsState))
  {
	if (strcmp(gpsState.m_StateWay, "A") == 0)
		sText = "自动定位";
	else if (strcmp(gpsState.m_StateWay,"M") == 0)
		sText = "手动定位";
	else
		sText = "未知";
    m_ctrlStateWay.SetWindowText(sText);

	if (gpsState.m_StateType == 1)
		sText = "未定位";
	else if (gpsState.m_StateType == 2)
		sText = "2D定位";
	else if (gpsState.m_StateType == 3)
		sText = "3D定位";
	else
		sText = "未知";
	m_ctrlStateType.SetWindowText(sText);
  }

  if ((m_hGps != NULL) && (m_hGps != INVALID_HANDLE_VALUE) && GpsGetPosition(m_hGps, &gpsPos))
  {
    //display the latitude
    int nDegrees = gpsPos.dwLatitude / 360000;
    int nMinutes = (gpsPos.dwLatitude - nDegrees * 360000) / 6000;
    int nSeconds = (gpsPos.dwLatitude - nDegrees * 360000 - nMinutes * 6000) / 100;
    
	if (gpsPos.bNorthing)
      sText.Format(_T("%d,%d,%d N"), nDegrees, nMinutes, nSeconds);
    else
      sText.Format(_T("%d,%d,%d S"), nDegrees, nMinutes, nSeconds);
    m_ctrlLatitude.SetWindowText(sText);

    //display the longitude
    nDegrees = gpsPos.dwLongitude / 360000;
    nMinutes = (gpsPos.dwLongitude - nDegrees * 360000) / 6000;
    nSeconds = (gpsPos.dwLongitude - nDegrees * 360000 - nMinutes * 6000) / 100;
    if (gpsPos.bEasting)
      sText.Format(_T("%d,%d,%d E"), nDegrees, nMinutes, nSeconds);
    else
      sText.Format(_T("%d,%d,%d W"), nDegrees, nMinutes, nSeconds);
    m_ctrlLongitude.SetWindowText(sText);

    //display the number of satellites
    sText.Format(_T("%d"), gpsPos.nSatellites);
    m_ctrlSatellites.SetWindowText(sText);

	// display the number of view satellites
    sText.Format(_T("%d"), gpsPos.nViewSatellites);
	m_ctrlViewSatellites.SetWindowText(sText);

    //display the altitude
    sText.Format(_T("%d 厘米, %.2f 米"), gpsPos.dwAntennaAltitude, double(gpsPos.dwAntennaAltitude) / 100);
    m_ctrlAltitude.SetWindowText(sText);

    //display the number of speed
    sText.Format(_T("%d 厘米/小时, %.2f 公里/小时"), gpsPos.dwSpeed, double(gpsPos.dwSpeed) / 100000);
    m_ctrlSpeed.SetWindowText(sText);

    //display the Quality indicator
    sText.Format(_T("%d"), gpsPos.wQualityIndicator);
    m_ctrlQuality.SetWindowText(sText);

    //display the bearing
    nDegrees = gpsPos.dwBearing / 100;
    nMinutes = (gpsPos.dwBearing - nDegrees * 100) / 100;
    nSeconds = (gpsPos.dwBearing - nDegrees * 100 - nMinutes * 100 / 60);
    sText.Format(_T("%d,%d,%d East of North"), nDegrees, nMinutes, nSeconds);
    m_ctrlBearing.SetWindowText(sText);

    //display the time of fix
    SYSTEMTIME st;
    st.wYear         = gpsPos.wFixYear;
    st.wMonth        = gpsPos.wFixMonth;
    st.wDayOfWeek    = 0;
    st.wDay          = gpsPos.wFixDay;
    st.wHour         = (unsigned short)(gpsPos.wFixHour + 8);
    st.wMinute       = gpsPos.wFixMinute;
    st.wSecond       = gpsPos.wFixSecond;
    st.wMilliseconds = 0;
    //COleDateTime t(st);
	CTime t(st);
    sText = t.Format("%H:%M:%S") + _T("(北京时间)");
    m_ctrlTime.SetWindowText(sText);

    m_EditCtrol.LineScroll(m_EditCtrol.GetLineCount(), 0);
  }
}


void CTestGPSDlg::EnableGpsPositionControl(BOOL bEnable)
{
	m_ctrlSpeed.EnableWindow(bEnable);
	m_ctrlTime.EnableWindow(bEnable);
	m_ctrlSatellites.EnableWindow(bEnable);
	m_ctrlQuality.EnableWindow(bEnable);
	m_ctrlLongitude.EnableWindow(bEnable);
	m_ctrlLatitude.EnableWindow(bEnable);
	m_ctrlBearing.EnableWindow(bEnable);
	m_ctrlAltitude.EnableWindow(bEnable);
	m_ctrlViewSatellites.EnableWindow(bEnable);
    
	GetDlgItem(IDC_PROMPT_SPEED)->EnableWindow(bEnable);
	GetDlgItem(IDC_PROMPT_TIME)->EnableWindow(bEnable);
	GetDlgItem(IDC_PROMPT_SATELLITES)->EnableWindow(bEnable);
	GetDlgItem(IDC_PROMPT_QUALITY)->EnableWindow(bEnable);
	GetDlgItem(IDC_PROMPT_LONGITUDE)->EnableWindow(bEnable);
	GetDlgItem(IDC_PROMPT_LATITUDE)->EnableWindow(bEnable);
	GetDlgItem(IDC_PROMPT_BEARING)->EnableWindow(bEnable);
	GetDlgItem(IDC_PROMPT_ALTITUDE)->EnableWindow(bEnable);
	GetDlgItem(IDC_PROMPT_VIEWSATELLITES)->EnableWindow(bEnable);

	m_ctrlSpeed.SetWindowText(_T(""));
	m_ctrlTime.SetWindowText(_T(""));
	m_ctrlSatellites.SetWindowText(_T(""));
	m_ctrlQuality.SetWindowText(_T(""));
	m_ctrlLongitude.SetWindowText(_T(""));
	m_ctrlLatitude.SetWindowText(_T(""));
	m_ctrlBearing.SetWindowText(_T(""));
	m_ctrlAltitude.SetWindowText(_T(""));
	m_ctrlViewSatellites.SetWindowText(_T(""));
}


void CTestGPSDlg::OnClose() 
{
	OnGpsClose();
	CDialog::OnClose();
}

void CTestGPSDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	OnGpsClose();
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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