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

📄 gpscommdlg.cpp

📁 这是一个PC机与GPS机之间的串口通信程序
💻 CPP
字号:
// GPSCommDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GPSComm.h"
#include "GPSCommDlg.h"
#include "stdio.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()

/////////////////////////////////////////////////////////////////////////////
// CGPSCommDlg dialog

CGPSCommDlg::CGPSCommDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPSCommDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGPSCommDlg)
	m_gpsdate = _T("");
	m_Latitude = _T("");
	m_Longitude = _T("");
	m_receive = _T("");
	m_gpstime = _T("");
	m_gpsspeed = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGPSCommDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPSCommDlg)
	DDX_Control(pDX, IDC_RECEIVE, m_conreceive);
	DDX_Control(pDX, IDC_COMSPEED, m_comspeed);
	DDX_Control(pDX, IDC_COMSELECT, m_comsel);
	DDX_Control(pDX, IDC_MSCOMM1, m_comm);
	DDX_Text(pDX, IDC_GPSDATE, m_gpsdate);
	DDX_Text(pDX, IDC_LATITUDE, m_Latitude);
	DDX_Text(pDX, IDC_LONGITUDE, m_Longitude);
	DDX_Text(pDX, IDC_RECEIVE, m_receive);
	DDX_Text(pDX, IDC_GPSTIME, m_gpstime);
	DDX_Text(pDX, IDC_GPSSPEED, m_gpsspeed);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGPSCommDlg, CDialog)
	//{{AFX_MSG_MAP(CGPSCommDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_EDITCHANGE(IDC_COMSELECT, OnComselect)
	ON_CBN_EDITCHANGE(IDC_COMSPEED, OnComspeed)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPSCommDlg message handlers

BOOL CGPSCommDlg::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

	//communcation
	m_comm.SetCommPort(1);
	if(!m_comm.GetPortOpen())
		m_comm.SetPortOpen(true);
	m_comm.SetSettings("4800,n,8,1");

	m_comm.SetInputMode(1);
	m_comm.SetInputLen(0);
	m_comm.GetInput();
	m_comm.SetRThreshold(400);//多于400的字符可正确接收到,少于的不能。
    /////显示波特率和通讯端口/////////
 	m_comsel.SetCurSel(0);
	m_comspeed.SetCurSel(1);
	////////////设置显示框字体///////
    m_font.CreateFont(15,0,0,0,700,false,false,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
	CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_SWISS,"Arial");
	CEdit *pEdit;
	pEdit=(CEdit*)GetDlgItem(IDC_RECEIVE);
	pEdit->SetFont(&m_font,false);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

BEGIN_EVENTSINK_MAP(CGPSCommDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CGPSCommDlg)
	ON_EVENT(CGPSCommDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CGPSCommDlg::OnComm() 
{  	
 ////////////////////////////////////////   
	// Scroll the edit control so that the first visible line
    // is the last 4 line of text.
   int nFirstVisible = m_conreceive.GetFirstVisibleLine();
   //TRACE(_T("There are %d Line \n"),nFirstVisible);
   int  nLineCount = m_conreceive.GetLineCount();
    TRACE(_T("There are %d Line !!!\n"),nLineCount);
   if (nLineCount>=6)
   { 
     m_conreceive.LineScroll((-nLineCount+4), 0);
	
   }
////////////////////////////////////////
/////////////////////////
	VARIANT m_input1;
   	COleSafeArray m_input2;
	long length,i;
	CString str;
     /////////接收数据/////////
	if(m_comm.GetCommEvent()==2)//接收区中有数据
	{
		m_input1=m_comm.GetInput();//读取数据
		m_input2=m_input1;//将VARIANT 型变量转换为COleSafeArray型数据
		length=m_input2.GetOneDimSize();//确定数据长度
	    CString  s[1024];
		int j=0;
		for(i=0;i<length;i++)
		{
		 m_input2.GetElement(&i,data+i);
		 BYTE a=*(char*)(data+i);
   		 str.Format("%c",a);
		 s[i]=str;
		 
		}
/////////////字节数不够59的数据接收下一行,以凑够数/////////////
	    TRACE(_T("There are %s  \n"),buffer);
         if(buffer!="")
	{
		  int bufflen=buffer.GetLength ();
	      m_receive+='\n';//换行
          for (j=0;j<59-bufflen;j++)
		{ 
		  m_receive+=s[j];
		   buffer+=s[j];
			 
		}
			/////////////////////data separate2//////
		 m_Latitude=buffer.Mid(16,2)+"  "+buffer.Mid(18,7)+"'N";
   	     m_Longitude=buffer.Mid(28,3)+"  "+buffer.Mid(31,7)+"'E";
		 m_gpsdate="20"+buffer.Mid(57,2)+"-"+buffer.Mid(55,2)+"-"+buffer.Mid(53,2);
		 //TRACE(_T("There are %s sss \n"),buffer.Mid(17,1));
		 
	   	 m_gpstime=buffer.Mid(7,2)+":"+buffer.Mid(9,2)+":"+buffer.Mid(11,2);
		 m_gpsspeed=buffer.Mid(41,5)+"mph";
		 buffer="";
		 m_receive+='\n';//换行
		 UpdateData(false);
		 
	}
    /////////////////////////////////
	/////////////////////data separate1//////
		for(i=0;i<length-6;i++)
		{ 
			if(s[i]=="$"&&s[i+1]=="G"&&s[i+2]=="P"&&s[i+3]=="R"&&s[i+4]=="M"&&s[i+5]=="C")
			{
				 //TRACE(_T("There are %d  long\n"),length-i);

			/////////改善字节数不够59的接收数据//////////
				if((length-i)>=58)
				 {
			     for (j=0;j<59;j++)
				 m_receive+=s[i+j];
		         m_Latitude=s[i+16]+s[i+17]+"  "+s[i+18]+s[i+19]+s[i+20]+s[i+21]+s[i+22]+s[i+23]+s[i+24]+"'N";
   			     m_Longitude=s[i+28]+s[i+29]+s[i+30]+"  "+s[i+31]+s[i+32]+s[i+33]+s[i+34]+s[i+35]+s[i+36]+s[i+37]+"'E";
			     m_gpsdate="20"+s[i+57]+s[i+58]+"-"+s[i+55]+s[i+56]+"-"+s[i+53]+s[i+54];
				 m_gpstime=s[i+7]+s[i+8]+":"+s[i+9]+s[i+10]+":"+s[i+11]+s[i+12];
				 m_gpsspeed=s[i+41]+s[i+42]+s[i+43]+s[i+44]+s[i+45]+"mph";
				 buffer="";
				 
				 }
				 else 
				 {
					 for (j=0;j<length-i+1;j++)
					{
						m_receive+=s[i+j];
					    buffer+=s[i+j];
					}
				 }
			
			}
			  
		}
		

   
	m_receive+='\n';//换行
	UpdateData(false);
  	m_comm.SetInputLen(0);//清空接收区的数据等待下组数据
	
}
	
}





void CGPSCommDlg::OnComselect() 
{
	// TODO: Add your control notification handler code here
	if(m_comm.GetPortOpen())
m_comm.SetPortOpen(FALSE);

m_comm.SetCommPort(m_comsel.GetCurSel()+1);
}

void CGPSCommDlg::OnComspeed() 
{
		CString temp;
	int i=m_comspeed.GetCurSel();
	switch(i)
	{
	case 0:
		i=2400;
		break;
	case 1:
		i=4800;
		break;
	case 2:
		i=9600;
		break;

	}
	temp.Format("%d,n,8,1 ",i);
	m_comm.SetSettings(temp);

	
}
//设置编辑框字体颜色
HBRUSH CGPSCommDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	if(nCtlColor==CTLCOLOR_EDIT)
		pDC->SetTextColor(RGB(0,0,255));

	//if(nCtlColor==CTLCOLOR_STATIC)
	//	pDC->SetTextColor(RGB(255,0,255));   
		return hbr;
}



⌨️ 快捷键说明

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