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

📄 gps_pdlg.cpp

📁 这是个解析GPS的GGA数据的代码。通过解析GGA的数据
💻 CPP
字号:
// GPS_PDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GPS_P.h"
#include "GPS_PDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DWORD WINAPI ReadDateThread(LPVOID lpParameter);
bool SplitString(CString &szSplittedString, CStringArray &StrArray, CString strSplitter);
//void GetGPSinformation(CString &msdate,CString &datename);
HANDLE m_OPEN;
HANDLE ReadThread;
HANDLE hEvent;
#define BUFFERSIZE 256
BOOL OKTORUN;
CStringArray DateSub;
typedef enum {
	GGA_TIME=1,
	GGA_LONGI = 2,
	GGA_LONGIDIR = 3,
	GGA_LATI = 4,
	GGA_LATIDIR = 5,
	GGA_SEALEAVEL = 8,
	GGA_FIXFLAG=6,
}GGA_MESSSAGE;
//HWND hwndList;
// CHAR ch = "GPGGA";


/////////////////////////////////////////////////////////////////////////////
// CGPS_PDlg dialog

CGPS_PDlg::CGPS_PDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPS_PDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGPS_PDlg)
	m_HighLevel = _T("");
	m_Norhtor = _T("");
	m_East = _T("");
	m_Message = _T("");
	m_Lattitude = _T("");
	m_Longitude = _T("");
	m_Time = _T("");
	m_High = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGPS_PDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPS_PDlg)
//	DDX_Text(pDX, IDC_HIGHLEVEL, m_HighLevel);
	DDX_Text(pDX, IDC_NORTHOR, m_Norhtor);
	DDX_Text(pDX, EAST, m_East);
	DDX_Text(pDX, IDC_Message, m_Message);
	DDX_Text(pDX, IDC_LATITUDE, m_Lattitude);
	DDX_Text(pDX, IDC_LONGITUDE, m_Longitude);
	DDX_Text(pDX, IDC_TIME, m_Time);
	DDX_Text(pDX, IDC_HIGH, m_High);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGPS_PDlg, CDialog)
	//{{AFX_MSG_MAP(CGPS_PDlg)
	ON_BN_CLICKED(IDC_BUTTON1, open)
	ON_BN_CLICKED(IDC_BUTTON2, FIX)
	ON_BN_CLICKED(IDC_BUTTON3, End)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPS_PDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen


	
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CGPS_PDlg::open() 
{
	// TODO: Add your control notification handler code here
	DWORD err;
	TCHAR buf[256];
	DCB	dcb;
    OKTORUN=TRUE;



    m_OPEN= CreateFile (_T("VSP1:"),	// Pointer to the name of the port
		GENERIC_READ | GENERIC_WRITE,				// Access (read-write) mode
		0,            		// Share mode
		NULL,        	 	// Pointer to the security attribute
		OPEN_EXISTING,		// How to open the serial port
		0,            		// Port attributes
		NULL);        		// Handle to port with attribute to copy	

	Sleep(500);

//	GPSClearBuffer(&dcb, sizeof (dcb));
	memset(&dcb,0,sizeof(dcb));
	dcb.DCBlength = sizeof (dcb);
	if (GetCommState(m_OPEN, &dcb) == 0)
	{
		err = GetLastError();
		wsprintf(buf, _T("Serial GetCommState:  Failed for port %x err:%d"), m_OPEN, err);
		::MessageBox(NULL,buf,_T("Error"), MB_OK);
		return ;
	}

	dcb.BaudRate = 57600;
	dcb.ByteSize = 8;			  // 8 data bits
	dcb.StopBits = ONESTOPBIT;// 1 stop bit	
	dcb.Parity = NOPARITY;		 // no parity
	dcb.fBinary = 1;			 // no EOF check
	dcb.fOutX = 0;			 // no xon/xoff flow control on output
	dcb.fInX = 0;				 // no xon/xoff flow control on input
	dcb.fNull = 0;				 // no null stripping
	dcb.fDtrControl = 0;		 // no DTR flow control

	if (SetCommState(m_OPEN, &dcb) == 0)
	{
		err = GetLastError ();
		wsprintf (buf, _T("SetCommState:  Failed for port %x err:%d"), m_OPEN, err);
		::MessageBox(NULL, buf, _T("Error"), MB_OK);
		return ;
	}

	

	// Check portID
	if (m_OPEN== INVALID_HANDLE_VALUE )
	{
		err = GetLastError ();
		wsprintf (buf, _T("Serial connect:  Failed for port %x, err:%d"),m_OPEN, err);
		::MessageBox(NULL,buf,_T("Error"), MB_OK);
		return ;
	}


	
}

void CGPS_PDlg::FIX() 
{
	// TODO: Add your control notification handler code here
 ReadThread=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadDateThread, (LPVOID)this, 0, NULL );//CreatThread()
	
}

void CGPS_PDlg::End() 
{
	// TODO: Add your control notification handler code here
	//	UpdateData(TRUE);
//	close()
// m_OPEN=NULL;
 CloseHandle(m_OPEN);
 m_OPEN=NULL;
 ReadThread=NULL;
 CloseHandle(ReadThread);
 ReadThread=NULL;
 CloseHandle(hEvent);
 hEvent=NULL;
 OKTORUN=FALSE;



}
DWORD WINAPI ReadDateThread(LPVOID lpParameter)
{
	CGPS_PDlg*p =(CGPS_PDlg*)lpParameter;
	BYTE pBuffer[BUFFERSIZE];
	DWORD cbBuffer=BUFFERSIZE;
	DWORD cbRead ;
    CStringArray StrArray;
	CString STR,STR2,STR3,STR4,STR5,SZDATE,SZDATENAME;
    int iTotal;
    int iPos = -1;
 //   HWND hwndList;
	hEvent=CreateEvent(0,FALSE,FALSE,NULL);
	while (OKTORUN)
    {
	  CString mstr;
	  memset(pBuffer,0,BUFFERSIZE);
	  WaitForSingleObject(hEvent,1000);
	  BOOL fSucess=ReadFile(m_OPEN,pBuffer,cbBuffer,&cbRead,NULL);
      STR3=pBuffer;
	  STR+=STR3;
	  if(SplitString(STR, StrArray, "\n"))
	  { iTotal = StrArray.GetSize();
			for(int j=0; j<iTotal; j++)
			{
				STR4=StrArray[j];
				iPos = STR4.Find(L"$",2);
				if( iPos > -1)
				{
					continue;
				}
					if(STR4=="")
				{
					continue;
				}
				STR4=STR4.Left(STR4.GetLength()-1);
				STR5=STR4.Left(6);
				SZDATE = STR4;
				SZDATENAME = STR5;
				const TCHAR *lpctStr=(LPCTSTR)SZDATE;
				if(SZDATENAME==(_T("$GPGGA")))
				{
			  //    hwndList = GetDlgItem(, IDC_Message);
			//	  SendMessage(hwndList, LB_INSERTSTRING, 0, (LPARAM)lpctStr);	
			       // CString mstr;
               mstr=SZDATE;

   if(SZDATENAME==(_T("$GPGGA")))
   {   
	   if(SplitString(SZDATE, DateSub, ","))
	   { 
             if(DateSub[GGA_FIXFLAG]==(_T("0")))
			 {
              p->UpdateData(TRUE);
			  p->m_High = _T("notfix");
	          p->m_Norhtor = _T("no");
	          p->m_East = _T("no");
              p->m_Message =mstr ;
	          p->m_Lattitude = _T("notfix");
              p->m_Longitude = _T("notfix");
	          p->m_Time =DateSub[GGA_TIME];
			  p->UpdateData(FALSE);
			 }
			 else
			 {
			  p->UpdateData(TRUE);
			   p->m_High =DateSub[GGA_SEALEAVEL];// _T("");
	           p->m_Norhtor = DateSub[GGA_LONGIDIR];//_T("");
	           p->m_East = DateSub[GGA_LATIDIR];//_T("");
	           p->m_Message =mstr;//_T("");
	           p->m_Lattitude =DateSub[GGA_LATI] ;//_T("");
	           p->m_Longitude =DateSub[GGA_LONGI] ;//_T("");
	           p->m_Time =DateSub[GGA_TIME]; //_T("");
               p->UpdateData(FALSE);
			 }
  
			 

	   }

      
   } 
	              //p->UpdateData(TRUE);
			     //  p->m_Message=SZDATE;
                   
				  // p->GetGPSinformation(SZDATE,SZDATENAME);

                //   p->UpdateData(FALSE);
				}
				continue;
			}
			}
	
	
	

		} 
return TRUE;
}

bool  SplitString(CString &szSplittedString, CStringArray &StrArray, CString strSplitter)
{
  CString szTemp;
	int iPosOld = 0;
	int iPos = 0;
	if(szSplittedString.IsEmpty())	{	return FALSE;	}
	if( strSplitter.IsEmpty() )	{	return FALSE;	}
	StrArray.RemoveAll();
	while (iPos != -1)
	{
		iPos = szSplittedString.Find(strSplitter);
		szTemp = szSplittedString.Left(iPos);
		szSplittedString = szSplittedString.Right(szSplittedString.GetLength() - iPos -1);
		StrArray.Add(szTemp);
	}
	return TRUE;

}
 void CGPS_PDlg:: GetGPSinformation(CString &msdate,CString &datename)
 {
  /* GGA_MESSSAGE status;
   CString mstr;
   mstr=msdate;

   if(datename==(_T("$GPGGA")))
   {   
	   if(SplitString(msdate, DateSub, ","))
	   { 
             if(DateSub[GGA_FIXFLAG]==0)
			 {
              UpdateData(TRUE);
			   m_HighLevel = _T("notfix");
	           m_Norhtor = _T("no");
	           m_East = _T("no");
               m_Message =mstr ;
	           m_Lattitude = _T("notfix");
               m_Longitude = _T("notfix");
	           //m_Time = _T("");
			   UpdateData(FALSE);
			 }
			 else
			 {
			  UpdateData(TRUE);
			   m_HighLevel =DateSub[GGA_SEALEAVEL];// _T("");
	           m_Norhtor = DateSub[GGA_LONGIDIR];//_T("");
	           m_East = DateSub[GGA_LATIDIR];//_T("");
	           m_Message =mstr;//_T("");
	           m_Lattitude =DateSub[GGA_LATI] ;//_T("");
	           m_Longitude =DateSub[GGA_LONGI] ;//_T("");
	           m_Time =DateSub[GGA_TIME]; //_T("");
               UpdateData(FALSE);
			 }
    GGA_TIME=1,
	GGA_LONGI = 2,
	GGA_LONGIDIR = 3,
	GGA_LATI = 4,
	GGA_LATIDIR = 5,
	GGA_SEALEAVEL = 8,
	GGA_FIXFLAG=6
		 
			 
			 

	   }

      
   }*/
 
 }

⌨️ 快捷键说明

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