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

📄 gpsview.cpp

📁 通过串口通信
💻 CPP
字号:
// GPSView.cpp : implementation of the CGPSView class
//

#include "stdafx.h"
#include "GPS.h"

#include "GPSDoc.h"
#include "GPSView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGPSView

IMPLEMENT_DYNCREATE(CGPSView, CFormView)

BEGIN_MESSAGE_MAP(CGPSView, CFormView)
	//{{AFX_MSG_MAP(CGPSView)
	ON_COMMAND(ID_COMMCTRL, OnCommctrl)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPSView construction/destruction

CGPSView::CGPSView()
	: CFormView(CGPSView::IDD)
{
	//{{AFX_DATA_INIT(CGPSView)
	m_strReceive = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CGPSView::~CGPSView()
{
}

void CGPSView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPSView)
	DDX_Control(pDX, IDC_MSCOMM1, m_Com);
	DDX_Text(pDX, IDC_EDIT, m_strReceive);
	DDV_MaxChars(pDX, m_strReceive, 4096);
	//}}AFX_DATA_MAP
}

BOOL CGPSView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CGPSView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CGPSView printing

BOOL CGPSView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGPSView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGPSView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CGPSView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CGPSView diagnostics

#ifdef _DEBUG
void CGPSView::AssertValid() const
{
	CFormView::AssertValid();
}

void CGPSView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CGPSDoc* CGPSView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGPSDoc)));
	return (CGPSDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGPSView message handlers

BEGIN_EVENTSINK_MAP(CGPSView, CFormView)
    //{{AFX_EVENTSINK_MAP(CGPSView)
	ON_EVENT(CGPSView, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CGPSView::OnOnCommMscomm1() 
{
	// TODO: Add your control notification handler code here
	VARIANT vResponse;
	//CString str[1024];
	int k,i=0;
	char *str1,*str2;
	if(m_Com.GetCommEvent()==2)
	{
	k=m_Com.GetInBufferCount();//接收到的字符数目
	}
	if(k>0)
	{
	vResponse=m_Com.GetInput();//读串口
	str1=(char*)(unsigned char*)vResponse.parray->pvData;}
	str2=str1;
	while(i<k)
	{i++;str2++;}
	*str2='\0';
	m_strReceive=(const char*)str1;//待定$GPRMC为59个字符
	UpdateData(FALSE);
	m_Com.SetPortOpen(FALSE);
}

void CGPSView::OnCommctrl() 
{
	// TODO: Add your command handler code here
	m_Com.SetCommPort(1);//选择COM1
	m_Com.SetInBufferSize(1024);//设置输入缓冲区的大小
	if(!m_Com.GetPortOpen())//打开串口
		m_Com.SetPortOpen(TRUE);
	m_Com.SetInputMode(1);//设置输入方式为二进制方式
	m_Com.SetSettings("4800,n,8,1");//设置数据传输速率等参数
	m_Com.SetRThreshold(1);//为1表示一个字符引发一个事件
	m_Com.SetInputLen(0);
}

⌨️ 快捷键说明

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