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

📄 gpsloggerdlg.cpp

📁 GPS导航的源代码
💻 CPP
字号:
// GPSLoggerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GPSLogger.h"
#include "GPSLoggerDlg.h"

#include "MapTypes.h"

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

#define PI 3.1415926

/////////////////////////////////////////////////////////////////////////////
// CGPSLoggerDlg dialog
#define WM_GPS WM_USER+1

struct GPSParam
{
	int code;
	int h,m,s,ms;
	double longitude;
	double latitude;
	double height;
	double valicity;
	double direction;
};

void CGPSLoggerDlg::GpsEventProc(int code,
							     int h,int m,int s,int ms,
							     double longitude, double latitude,
							     double height,
							     double valicity,
							     double direction,
							     LPVOID userptr)
{
	CGPSLoggerDlg *pThis=(CGPSLoggerDlg *)userptr;

	GPSParam *param=new GPSParam;
	param->code			= code;
	param->h			= h;
	param->m			= m;
	param->s			= s;
	param->ms			= ms;
	param->longitude	= longitude;
	param->latitude		= latitude;
	param->height		= height;
	param->valicity		= valicity;
	param->direction	= direction;

	pThis->SendMessage(WM_GPS,0,(LPARAM)param);
}



CGPSLoggerDlg::CGPSLoggerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPSLoggerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGPSLoggerDlg)
	m_fLongitude = 0.0;
	m_fLatitude = 0.0;
	m_strPort = _T("COM2:");
	m_bDatavalid = FALSE;
	m_fAltitude = 0.0;
	m_fDirection = 0.0;
	m_fVelocity = 0.0;
	m_strTime = _T("");
	m_bAdjust = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bIsGPSRunning=FALSE;
}

void CGPSLoggerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPSLoggerDlg)
	DDX_Text(pDX, IDC_EDLONGITUDE, m_fLongitude);
	DDX_Text(pDX, IDC_EDLATITUDE, m_fLatitude);
	DDX_Text(pDX, IDC_GPSPORT, m_strPort);
	DDX_Check(pDX, IDC_CKVALID, m_bDatavalid);
	DDX_Text(pDX, IDC_EDALTITUDE, m_fAltitude);
	DDX_Text(pDX, IDC_EDDIRECTION, m_fDirection);
	DDX_Text(pDX, IDC_EDVELOCITY, m_fVelocity);
	DDX_Text(pDX, IDC_EDTIME, m_strTime);
	DDX_Check(pDX, IDC_CKADJUST, m_bAdjust);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGPSLoggerDlg, CDialog)
	//{{AFX_MSG_MAP(CGPSLoggerDlg)
	ON_BN_CLICKED(IDC_BTNSCAN, OnBtnscan)
	ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopen)
	ON_WM_DESTROY()
	ON_MESSAGE(WM_GPS,OnGps)
	ON_BN_CLICKED(IDC_CKADJUST, OnCkadjust)
	ON_MESSAGE(WM_HOTKEY,OnHotkey)
	ON_BN_CLICKED(IDC_BTNNEWFILE, OnBtnnewfile)
	ON_BN_CLICKED(IDC_BTNMAKELAYER, OnBtnmakelayer)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPSLoggerDlg message handlers

BOOL CGPSLoggerDlg::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
	GetLastLogFile();
	for(int i=0xc1;i<0xcf;i++)
		RegisterHotKey(m_hWnd,i,MOD_WIN,i);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CGPSLoggerDlg::OnBtnscan() 
{
	// TODO: Add your control notification handler code here
	WCHAR strPort[5];
	BOOL bFound=FALSE;
	for(int i=1;i<10 && !bFound;i++)
	{
		wsprintf(strPort,L"COM%d:",i);
		if(m_gps.OpenDevice(strPort,4800))
		{
			SetDlgItemText(IDC_GPSPORT,strPort);
			m_gps.CloseDevice();
			bFound=TRUE;
		}
	}
	if(!bFound)
		MessageBox(L"No GPS Found",L"Error",MB_OK);
}

void CGPSLoggerDlg::OnBtnopen() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	if(!m_bIsGPSRunning)
	{
		if(!m_gps.OpenDevice((LPTSTR)(LPCTSTR)m_strPort,4800))
			MessageBox(L"Open GPS Failed",L"Error",MB_OK);
		else
		{
			m_gps.set_EventProc(GpsEventProc,this);
			m_gps.Run();
		}

		m_bIsGPSRunning=TRUE;
	}
	else
	{
		m_gps.Stop();
		m_gps.CloseDevice();

		m_bIsGPSRunning=FALSE;
	}
	
	if(m_bIsGPSRunning)
		GetDlgItem(IDC_BTNOPEN)->SetWindowText(_T("Close"));
	else
		GetDlgItem(IDC_BTNOPEN)->SetWindowText(_T("Open"));
}

void CGPSLoggerDlg::OnGps(WPARAM wParam,LPARAM lParam)
{
	GPSParam *pParam=(GPSParam *)lParam;

	m_bDatavalid	= pParam->code;
	if(m_bDatavalid)
	{
		if(m_bAdjust)
		{
			SYSTEMTIME st;
			GetSystemTime(&st);
			st.wHour=pParam->h;		st.wMinute=pParam->m;
			st.wSecond=pParam->s;	st.wMilliseconds=pParam->ms;
			SetSystemTime(&st);

			m_bAdjust=FALSE;
		}

		m_fLongitude	= pParam->longitude;
		m_fLatitude		= pParam->latitude;
		m_fAltitude		= pParam->height;
		m_fDirection	= pParam->direction;
		m_fVelocity		= pParam->valicity*1.842;
		pParam->h+=8;	if(pParam->h >= 24)	pParam->h-=24;
		m_strTime.Format(L"%d:%d:%d:%d",pParam->h,pParam->m,pParam->s,pParam->ms);

		{
			CWnd *pNeedle=GetDlgItem(IDC_NEEDLE);
			CRect rect;
			pNeedle->GetClientRect(&rect);
			double arg;
			int cx=rect.Width()/2,cy=rect.Height()/2;
			double r=cx > cy ? cy : cx;
			r-=2;
			POINT pt[5];
			arg=PI*(0.5-m_fDirection/180.0);
			if(arg < 0)	arg+=(2.0*PI);
			pt[0].x=(int)(r*cos(arg))+cx;		pt[0].y=cy-(int)(r*sin(arg));
			arg-=PI;
			pt[1].x=(int)(r*cos(arg+0.3))+cx;	pt[1].y=cy-(int)(r*sin(arg+0.3));
			pt[2].x=cx;							pt[2].y=cy;
			pt[3].x=(int)(r*cos(arg-0.3))+cx;	pt[3].y=cy-(int)(r*sin(arg-0.3));
			pt[4].x=pt[0].x;					pt[4].y=pt[0].y;

			CDC *pDC=pNeedle->GetDC();
			pDC->PatBlt(rect.left,rect.top,rect.Width(),rect.Height(),WHITENESS);
			pDC->Polyline(pt,5);
			pDC->DeleteDC();
		}

		WCHAR strFilename[20];
		GetDlgItemText(IDC_EDLOGFILE,strFilename,20);
		//FILE *fp=fopen("gpslogger.gld","a");
		FILE *fp=_wfopen(strFilename,L"a");
		fprintf(fp,"%ls %f %f %f %f\n",m_strTime,m_fLongitude,m_fLatitude,m_fDirection,m_fVelocity);
		fclose(fp);
	}
	else
	{
		m_fLongitude	= 0;
		m_fLatitude		= 0;
		m_strTime		= _T("");
	}

	UpdateData(FALSE);
}

void CGPSLoggerDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	m_gps.Stop();
	m_gps.CloseDevice();
}

void CGPSLoggerDlg::OnCkadjust() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
}

void CGPSLoggerDlg::OnPower()
{
}

void CGPSLoggerDlg::OnHotkey(WPARAM wParam,LPARAM lParam)
{
	switch((UINT) HIWORD(lParam))
	{
	case 0xc6:
		OnBtnscan();
		break;
	case 0xc1:
		OnBtnopen();
		break;
	}
}

void CGPSLoggerDlg::OnBtnnewfile() 
{
	// TODO: Add your control notification handler code here
	int id;
	WCHAR strFilename[20];
	GetDlgItemText(IDC_EDLOGFILE,strFilename,20);
	swscanf(strFilename,L"gpslog%d.gld",&id);
	wsprintf(strFilename,L"gpslog%d.gld",id+1);
	SetDlgItemText(IDC_EDLOGFILE,strFilename);
}

char *GetToken(char *buf)
{
	char *p=buf;
	while(*p != '\0' && *p != ' ' && *p != '\t')
		p++;
	*p=0;
	return buf;
}

void CGPSLoggerDlg::OnBtnmakelayer() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE,_T(".gld"),NULL,OFN_HIDEREADONLY,_T("数据文件(*.gld)|gpslog*.gld||"));
	if(dlg.DoModal() != IDOK)
		return;

	int h,m,s,ms;
	double longitude,latitude,speed,direct;

	MapFeatureIDStr id;
	MapFeatureProp prop;
	MapFeaturePart part;

	memset(&id,0,sizeof(MapFeatureIDStr));
	memset(&prop,0,sizeof(MapFeatureProp));
	memset(&part,0,sizeof(MapFeaturePart));

	FILE *fp=_wfopen(dlg.GetPathName(),L"r");
	char line[512];

	WCHAR strLayer[MAX_PATH],str[MAX_PATH];
	wcscpy(strLayer,dlg.GetFileTitle());

	FILE *fpID,*fpMap,*fpProp;
	wsprintf(str,L"\\My Documents\\map\\%s.id",strLayer);
	fpID=_wfopen(str,L"wb");
	wsprintf(str,L"\\My Documents\\map\\%s.map",strLayer);
	fpMap=_wfopen(str,L"wb");
	wsprintf(str,L"\\My Documents\\map\\%s.prop",strLayer);
	fpProp=_wfopen(str,L"wb");

	unsigned long int npt=1,gap=0;
	fwrite(&npt,1,sizeof(unsigned long int),fpMap);
	fwrite(&gap,1,sizeof(unsigned long int),fpMap);
	fwrite(&part,1,sizeof(MapFeaturePart)-sizeof(MapPosition),fpMap);

	//开始播放
	while(!feof(fp))
	{
		fgets(line,512,fp);
		/*hh:mm:ss:ms longitude latitude speed power XX err XX snr XX ber XX msl mdl*/
		char *fld;
		
		//时间
		fld=GetToken(line);
		sscanf(line,"%d:%d:%d:%d",&h,&m,&s,&ms);
		
		//经度
		fld=fld+strlen(fld)+1;
		fld=GetToken(fld);
		longitude=atof(fld);

		//纬度
		fld=fld+strlen(fld)+1;
		fld=GetToken(fld);
		latitude=atof(fld);

		//速度
		fld=fld+strlen(fld)+1;
		fld=GetToken(fld);
		speed=atof(fld);

		//方向
		fld=fld+strlen(fld)+1;
		fld=GetToken(fld);
		direct=atof(fld);

		if(longitude > 115.0 && longitude < 120.0 &&
		   latitude > 35.0 && latitude < 40.0)
		{
			part.pts[0].x=longitude;
			part.pts[0].y=latitude;
			fwrite(part.pts,1,sizeof(MapPosition),fpMap);

			part.npt++;

			part.bound.pt1.x=part.bound.pt1.x < longitude ? part.bound.pt1.x : longitude;
			part.bound.pt1.y=part.bound.pt1.y < latitude ? part.bound.pt1.y : latitude;
			part.bound.pt2.x=part.bound.pt2.x > longitude ? part.bound.pt2.x : longitude;
			part.bound.pt2.y=part.bound.pt2.y > latitude ? part.bound.pt2.y : latitude;
		}
	}
	
	fclose(fp);

	id.id=0;
	id.offmap=0;
	id.offprop=0;
	sprintf(id.name,"%d",id.id);
	id.type=1;
	id.bound=part.bound;
	fwrite(&id,1,sizeof(MapFeatureIDStr),fpID);

	prop.color=0xff0000;
	prop.width=2;
	fwrite(&prop,1,sizeof(MapFeatureProp),fpProp);

	fseek(fpMap,2*sizeof(unsigned long int),SEEK_SET);
	fwrite(&part,1,sizeof(MapFeaturePart)-sizeof(MapPosition),fpMap);

	fclose(fpID);
	fclose(fpMap);
	fclose(fpProp);
}

void CGPSLoggerDlg::GetLastLogFile()
{
	int id,maxid=1;
	WIN32_FIND_DATA findData;
	HANDLE hFind=FindFirstFile(_T("gpslog*.gld"),&findData);

	if(hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			swscanf(findData.cFileName,L"gpslog%d.gld",&id);
			if(id > maxid)	maxid=id;
		}
		while(FindNextFile(hFind,&findData));
		FindClose(hFind);
	}

	WCHAR strFilename[20];
	wsprintf(strFilename,L"gpslog%d.gld",maxid);
	SetDlgItemText(IDC_EDLOGFILE,strFilename);
}

⌨️ 快捷键说明

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