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

📄 gpsprodlg.cpp

📁 一个用VC++做的嵌入式GPS导航的程序
💻 CPP
字号:
// GPSProDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GPSPro.h"
#include "GPSProDlg.h"

#include "ConfigDlg.h"



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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPSProDlg dialog

CGPSProDlg::CGPSProDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPSProDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGPSProDlg)
	m_strReceChar = _T("");
	m_id = 0;
	m_time = 0;
	m_latitude = _T("");
	m_longlati = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGPSProDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPSProDlg)
	DDX_Text(pDX, IDC_RECECHAR, m_strReceChar);
	DDX_Text(pDX, IDC_ID, m_id);
	DDX_DateTimeCtrl(pDX,IDC_TIME,m_time);
	DDX_Text(pDX, IDC_LATITUDE, m_latitude);
	DDX_Text(pDX, IDC_LONGLATI, m_longlati);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGPSProDlg, CDialog)
	//{{AFX_MSG_MAP(CGPSProDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONFIG, OnConfig)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPSProDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here
	BaudRates[0]=1200;
	BaudRates[1]=2400;
	BaudRates[2]=4800;
	BaudRates[3]=9600;
	BaudRates[4]=19200;
	BaudRates[5]=38400;
	BaudRates[6]=57600;
	BaudRates[7]=115200;

	PortIDs[0]="COM1:";
	PortIDs[1]="COM2:";
	PortIDs[2]="COM3:";
	PortIDs[3]="COM4:";
	PortIDs[4]="COM5:";
	PortIDs[5]="COM6:";
	PortIDs[6]="COM7:";
	PortIDs[7]="COM8:";
	PortIDs[8]="COM9:";

	PortNo=6;
	BaudRate=2;
	DataBits=3;
	StopBits=ONESTOPBIT;
	Parity=NOPARITY;

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGPSProDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CGPSProDlg::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 CGPSProDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGPSProDlg::OnConfig() 
{
	// TODO: Add your control notification handler code here
	CConfigDlg* Dialog1;
	Dialog1=new CConfigDlg;
	
	Dialog1->BaudRate=BaudRate;
	Dialog1->PortNo=PortNo-1;
	Dialog1->DataBits=DataBits-1;
	Dialog1->StopBits=StopBits;
	Dialog1->Parity=Parity;
	if(Dialog1->DoModal()==IDOK)
	{
		BaudRate = Dialog1->BaudRate;
		PortNo = Dialog1->PortNo;
		DataBits= Dialog1->DataBits;
		StopBits = Dialog1->StopBits;
		Parity = Dialog1->Parity;


		
		if(!m_ceSeries.OpenPort(this,Dialog1->PortNo,Dialog1->BaudRate,Dialog1->Parity,Dialog1->DataBits,Dialog1->StopBits))
		{
			AfxMessageBox("打开串口失败");
			//SetDlgItemText(IDC_BTN_OPEN,CString("关闭端口"));
		}
		else
		{
			
			m_ceSeries.ClosePort();
			//SetDlgItemText(IDC_BTN_OPEN,CString("打开端口"));
		}
		
		
	}
	delete Dialog1;
	
}

void CGPSProDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	m_ceSeries.ClosePort();
	KillTimer(1);
}

void CGPSProDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	m_ceSeries.m_OnSeriesRead = OnSeriesRead;

	m_nTimer = SetTimer(1, 3000, 0);
	
}

void CGPSProDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	CString FileName(_T(""));
	CFileDialog FileDlg(FALSE,NULL,NULL,OFN_HIDEREADONLY,TEXT("Text Files(*.txt)|*.txt||"));
	if(FileDlg.DoModal()==IDOK)
	{
		FileName=FileDlg.GetPathName();
	}
	if(FileName!=_T(""))
	{
		SaveReceivedToFile(FileName);
	}
}

BOOL CGPSProDlg::SaveReceivedToFile(LPCTSTR FileName)
{
	int i;
	CFile cf;
	DWORD dwBytesToSave=m_strReceChar.GetLength();
	
	if(!cf.Open(FileName,CFile::modeWrite|CFile::modeCreate))
	{
		AfxMessageBox(_T("无法打开Text文件!"));
	}

	cf.Seek(0,CFile::begin);
	unsigned char * buf=new unsigned char[dwBytesToSave];
	for(i=0;i<dwBytesToSave;i++)
	{
		buf[i]=m_strReceChar.GetAt(i);
	}	

	cf.Write(buf,dwBytesToSave);
	
	delete[] buf;
	cf.Close();

	return TRUE;

}

void CGPSProDlg::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("$GPRMC")!=0)
			continue;
		msg.Delete(0,msg.Find(',')+1);
	
		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,2);
		hour=atoi(temp);
		min=atoi(temp+3);
		sec=atoi(temp+6);
		msg.ReleaseBuffer();
		msg.Delete(0,msg.Find(',')+1);

		if(msg.GetAt(0)!='A')
			continue;
		msg.Delete(0,msg.Find(',')+1);
//采集到的纬度显示在edit中
		CString longitude=msg.Left(2);
		longitude+="度";
		longitude+=msg.Mid(2,6);
		longitude+="分";
		msg.Delete(0,msg.Find(',')+1);
		if(msg.GetAt(0)=='N')
			longitude.Insert(0,"北纬");
		if(msg.GetAt(0)=='S')
			longitude.Insert(0,"南纬");
		msg.Delete(0,msg.Find(',')+1);
		SetDlgItemText(IDC_LONGLATI,longitude);
		UpdateData(FALSE);
	
		
//采集到经度显示到edit中
		CString latitude=msg.Left(3);
		latitude+="度";
		latitude+=msg.Mid(3,6);
		latitude+="分";
		msg.Delete(0,msg.Find(',')+1);
		if(msg.GetAt(0)=='E')
			latitude.Insert(0,"东经");
		if(msg.GetAt(0)=='W')
			latitude.Insert(0,"西经");
		msg.Delete(0,msg.Find(',')+1);
		SetDlgItemText(IDC_LATITUDE,latitude);
		UpdateData(FALSE);

		

//采集到的显示时间
		char* pDate=msg.GetBuffer(msg.GetLength()+1);
		int day,mon,year;
		memset(temp,0,9);
		memcpy(temp,pDate,2);
		memcpy(temp+3,pDate+2,2);
		memcpy(temp+6,pDate+4,2);
		day=atoi(temp);
		mon=atoi(temp+3);
		year=atoi(temp+6);
		if((year>40)&&(year<97))
			continue;
		if(year>40)
			year+=1900;
		else
			year+=2000;
		CTime t(year,mon,day,hour,min,sec);
		CString t2;
		
		msg.ReleaseBuffer();
		msg.Delete(0,msg.Find(',')+1);

		t = CTime::GetCurrentTime();
		t2 = t.Format("%Y:%M:%D:%H:%M:%S");
		//SetDlgItemText(IDC_TIME,t2);
		::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_TIME,t2);
		UpdateData(FALSE);



		

	}
	
	CDialog::OnTimer(nIDEvent);
}

void CGPSProDlg::OnSeriesRead(CWnd *pWnd, BYTE *buf, int bufLen)
{
	CGPSProDlg *pDlg = (CGPSProDlg*)pWnd;
	CEdit * pEdit;
	pEdit = (CEdit*)pDlg->GetDlgItem(IDC_RECECHAR);

	CString strRecv = (char*)buf;
	CString strOld;
	pEdit->GetWindowText(strOld);
	pEdit->SetWindowText(strOld + strRecv);

	delete[] buf;

}

⌨️ 快捷键说明

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