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

📄 mainfrm.cpp

📁 接受GPGGA语句的VC程序
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "GPS.h"
#include <windows.h>
//#include <winioctl.h>
#include "k32call.h"
#include "phys32.h"
#include "MainFrm.h"
#include "GPSSet.h"
#include "GPSView.h"
#include "math.h"
#include "Winio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
	ON_WM_TIMER()
	ON_COMMAND(ID_RECEIVE_START, OnReceiveStart)
	ON_COMMAND(ID_RECEIVE_STOP, OnReceiveStop)
	ON_COMMAND(ID_CONFIG, OnConfig)
	ON_COMMAND(ID_CLEAR, OnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{

}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	m_Port.InitPort(this,1,9600,'N',8,1);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

LONG CMainFrame::OnCommunication(WPARAM ch, LPARAM port)
{
	m_strReceived+=(char)ch;

	return 0;
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	if(nIDEvent!=1)
		return ;

	while((m_strReceived.Find(0x0d)!=-1)&&(m_strReceived.Find(0x0a)!=-1))
	{
		int startLF=m_strReceived.Find(0x0a);
		int endCR=m_strReceived.Find(0x0d);
		if(startLF>endCR)
			endCR=m_strReceived.Find(0x0d,startLF);
		CString msg= m_strReceived.Mid(startLF+1,endCR-startLF-1);
		m_strReceived.Delete(0,endCR+1);

		if(msg.Left(6).Compare("$GPGGA")!=0)
			continue;
		msg.Delete(0,msg.Find(',')+1);
	    CString time1=msg.Left(2);
		time1 += ":";
		CString time2 = msg.Mid(2,2);
		time2 += ":";
		CString time3 = msg.Mid(4,2);
		CString time=time1+time2+time3;

	/*	char* pTime=msg.GetBuffer(msg.GetLength()+1);
		int hour,min,sec;
		char temp[9];
		memset(temp,0,9);
		memcpy(temp,pTime,2);
		memcpy(temp+3,pTime+2,2);
		memcpy(temp+6,pTime+4,5);
		hour=atoi(temp);
		min=atoi(temp+3);
		sec=atoi(temp+6);
		msg.ReleaseBuffer();
		msg.Delete(0,msg.Find(',')+1);
		CTime t(hour,min,sec);*/

		/*if(msg.GetAt(0)!='V')
			continue;*/
		msg.Delete(0,msg.Find(',')+1);

		CString latitude1=msg.Left(2);
		latitude1 += "度";
		CString latitude2 = msg.Mid(2,11);
		latitude2 += "分";
        CString latitude=latitude1+latitude2;
	
		msg.Delete(0,msg.Find(',')+1);
		if(msg.GetAt(0)=='N')
			latitude.Insert(0,"北纬");
		if(msg.GetAt(0)=='S')
			latitude.Insert(0,"南纬");
		msg.Delete(0,msg.Find(',')+1);
         
		CString longitude1=msg.Left(3);
		longitude1 +="度";
		CString longitude2=msg.Mid(3,11);
		longitude2 += "分";
		CString longitude=longitude1+longitude2;
        
		msg.Delete(0,msg.Find(',')+1);
		if(msg.GetAt(0)=='E')
			longitude.Insert(0,"东经");
		if(msg.GetAt(0)=='W')
			longitude.Insert(0,"西经");
		msg.Delete(0,msg.Find(',')+1);
		
		CString PositionFix=msg.Left(1);//定位信息
		
		msg.Delete(0,msg.Find(',')+1);	

		CString SatellitesUsed=msg.Left(2);//有效卫星
		
		msg.Delete(0,msg.Find(',')+1);	
		
		CString hdop=msg.Left(3);//对地高度
		
		msg.Delete(0,msg.Find(',')+1);		

		

		CString Altitude=msg.Left(6);		// WGS-84坐标下的海拔高度
		
		msg.Delete(0,msg.Find(',')+1);
		if(msg.GetAt(0)=='M')
			Altitude.Insert(6,"米");

		msg.Delete(0,msg.GetLength());	//定位系统模式指示忽略
		//后续字段忽略
        //坐标转换
		    double	TX;
	        double	TY; 
            double N,T,l,b,g,m0,X0,A0,B0,C0,D0,E0,G0,e,f,z,PI,P0,L0,a,B,L;
			a=6378245.0;
			f=1/298.3;
			e=sqrt(0.00669342162297);
			z=sqrt(0.00673852541468);
			PI=3.1415926535898;
			P0=PI/180;
			L0=117;
			L=atof(longitude1)+atof(longitude2)/60;
			B=atof(latitude1)+atof(latitude2)/60;
			b=B*P0;
            l=(L-L0)*P0;
			g=z*cos(b);
			T=tan(b);
			m0=l*cos(b);
			double M;
			M=1-e*e*sin(b)*sin(b);
			N=a/sqrt(M);
			A0=1+(3.0/4.0)*e*e+(45.0/64.0)*e*e*e*e+(175.0/256.0)*e*e*e*e*e*e+(11025.0/16384.0)*pow(e,8)
					+(43659.0/65536.0)*pow(e,10);
			B0=(3.0/4.0)*pow(e,2)+(15.0/16.0)*pow(e,4)+(525.0/512.0)*pow(e,6)+(2205.0/2048.0)*pow(e,8)
					+(72765.0/65536.0)*pow(e,10);
			C0=(15.0/64.0)*pow(e,4)+(105.0/256.0)*pow(e,6)+(2205.0/4096.0)*pow(e,8)+(10395.0/16384.0)*pow(e,10);
			D0=(35.0/512.0)*pow(e,6)+(315.0/2048.0)*pow(e,8)+(31185.0/131072.0)*pow(e,10);
			E0=(315.0/16384.0)*pow(e,8)+(3465.0/65536.0)*pow(e,10);
			G0=(693.0/13107.0)*pow(e,10);
			X0=a*(1-e*e)*(A0*B*PI/180-B0*sin(2*B)/2.0+C0*sin(4*B)/4.0-D0*sin(6*B)/6.0+E0*sin(8*B)/8.0-G0*sin(10*B)/10.0);
			TX=X0+N*T*m0*m0/2.0+N*T*(5-T*T+9*g*g+4*pow(g,4))*pow(m0,4)/24.0+N*T*(61-58*pow(T,2)
					+pow(T,4)+270*g*g-330*g*g*T*T)*pow(m0,6)/720.0;
			TY=20500000+N*m0+N*(1-T*T+g*g)*pow(m0,3)/6.0+N*(5-18*T*T+T*T*T*T+14*g*g-58*g*g*T*T)*pow(m0,5)/120.0;

			UpdateData(FALSE); //更新编辑框内容	

			
//            newdata:
		CGPSView* pView=(CGPSView*)this->GetActiveView();
		CGPSSet* m_pSet=pView->m_pSet;

		m_pSet->AddNew();
		m_pSet->m_latitude=latitude;
		m_pSet->m_longitude=longitude;
		m_pSet->m_time=time;
		m_pSet->m_SatellitesUsed=SatellitesUsed;
		m_pSet->m_hdop=hdop;
		m_pSet->m_Altitude=Altitude;
		m_pSet->m_TX=TX;
        m_pSet->m_TY=TY;
		if(m_pSet->CanUpdate()) 
		{
			m_pSet->Update();
		}
		if(!m_pSet->IsEOF())
		{
			m_pSet->MoveLast();
		}
		pView->m_dbGrid.Refresh();
		pView->UpdateData(FALSE);
	}
	
	CFrameWnd::OnTimer(nIDEvent);
}

void CMainFrame::OnReceiveStart() 
{
	// TODO: Add your command handler code here
	m_Port.StartMonitoring();
	m_nTimer = SetTimer(1, 3000, 0);
     InitializeWinIo();
}

void CMainFrame::OnReceiveStop() 
{
	// TODO: Add your command handler code here
	m_Port.StopMonitoring();
	KillTimer(1);
}

void CMainFrame::OnConfig() 
{
	CConfigDlg* dlg = new CConfigDlg(this, &m_Port);
	
	if (dlg->DoModal() == IDOK)
	{
		switch(dlg->m_strComm[3])
		{
		case '1':
			m_nComm=1;
			break;
		case '2':
			m_nComm=2;
			break;
		case '3':
			m_nComm=3;
			break;
		case '4':
			m_nComm=4;
			break;
		default:
			break;
		}
		m_Port.m_nPortNr=m_nComm;
				
		m_nBandRate=	m_Port.m_dcb.BaudRate=	atoi(dlg->m_strBaudRate);
		this->m_cParity=	dlg->m_strParity[0];
		switch(m_cParity)
		{
		case 'N':
			m_Port.m_dcb.Parity=NOPARITY;
			break;
		case 'O':
			m_Port.m_dcb.Parity=ODDPARITY;
			break;
		case 'E':
			m_Port.m_dcb.Parity=EVENPARITY;
			break;
		case 'M':
			m_Port.m_dcb.Parity=MARKPARITY;
			break;
		case 'S':
			m_Port.m_dcb.Parity=SPACEPARITY;
			break;
		default:
			break;		
		}

		m_nDataBits=	m_Port.m_dcb.ByteSize=	atoi(dlg->m_strDataBits);
		m_nStopBits=	m_Port.m_dcb.StopBits=	atoi(dlg->m_strStopBits);
		if(m_Port.m_szWriteBuffer)
			delete m_Port.m_szWriteBuffer;
		m_Port.m_szWriteBuffer=new BYTE[m_Port.m_nWriteBufferSize];
		if(!(m_Port.InitPort(this,m_nComm,m_nBandRate,m_cParity,m_nDataBits,m_nStopBits)))
		{
			char error[100];
			sprintf(error,"COM%d被占用或没有该COM口,请改用其它COM口!",m_nComm);
			AfxMessageBox(error);
		}
	}
	delete dlg;
}

void CMainFrame::OnClear() 
{
	// TODO: Add your command handler code here
	CGPSView* pView=(CGPSView*)this->GetActiveView();
	CGPSSet* pSet=pView->m_pSet;

	pSet->MoveFirst();

	do
	{
		try 
		{
			pSet->Delete();
			pSet->MoveNext();
		}
		catch(CDBException* e) 
		{
			AfxMessageBox(e->m_strError);
			e->Delete();
			return;
		}
	}while(!pSet->IsEOF());

	pView->UpdateData(FALSE);
}


⌨️ 快捷键说明

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