📄 gpstestview.cpp
字号:
// gpstestView.cpp : implementation of the CGpstestView class
//
#include "stdafx.h"
#include "gpstest.h"
#include "gpstestDoc.h"
#include "gpstestView.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGpstestView
IMPLEMENT_DYNCREATE(CGpstestView, CFormView)
BEGIN_MESSAGE_MAP(CGpstestView, CFormView)
//{{AFX_MSG_MAP(CGpstestView)
ON_BN_CLICKED(IDC_CLEAR_BUTTON, OnClearButton)
ON_BN_CLICKED(IDC_CLOSE_BUTTON, OnCloseButton)
ON_BN_CLICKED(IDC_START_BUTTON, OnStartButton)
ON_BN_CLICKED(IDC_STOP_BUTTON, OnStopButton)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_READ_BUTTON, OnReadButton)
ON_BN_CLICKED(IDC_SAVE_BUTTON, OnSaveButton)
ON_WM_ERASEBKGND()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CGpstestView construction/destruction
CGpstestView::CGpstestView()
: CFormView(CGpstestView::IDD)
{
//{{AFX_DATA_INIT(CGpstestView)
m_data = _T("");
m_com = _T("");
m_bote = _T("");
m_riqi = _T("");
m_shijian = _T("");
m_gaodu = _T("");
m_jingdu = _T("");
m_weidu = _T("");
m_dingwei = _T("");
m_sudu = _T("");
m_shuiping = _T("");
m_chuizhi = _T("");
m_weizhi = _T("");
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CGpstestView::~CGpstestView()
{
}
void CGpstestView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGpstestView)
DDX_Text(pDX, IDC_EDIT1, m_data);
DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm1);
DDX_CBString(pDX, IDC_COMBO1, m_com);
DDX_CBString(pDX, IDC_COMBO2, m_bote);
DDX_Text(pDX, IDC_STATIC1, m_riqi);
DDX_Text(pDX, IDC_STATIC2, m_shijian);
DDX_Text(pDX, IDC_STATIC3, m_gaodu);
DDX_Text(pDX, IDC_STATIC4, m_jingdu);
DDX_Text(pDX, IDC_STATIC5, m_weidu);
DDX_Text(pDX, IDC_STATIC6, m_dingwei);
DDX_Text(pDX, IDC_STATIC10, m_sudu);
DDX_Text(pDX, IDC_STATIC7, m_shuiping);
DDX_Text(pDX, IDC_STATIC8, m_chuizhi);
DDX_Text(pDX, IDC_STATIC9, m_weizhi);
//}}AFX_DATA_MAP
}
BOOL CGpstestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CGpstestView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CGpstestView printing
BOOL CGpstestView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGpstestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGpstestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CGpstestView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CGpstestView diagnostics
#ifdef _DEBUG
void CGpstestView::AssertValid() const
{
CFormView::AssertValid();
}
void CGpstestView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CGpstestDoc* CGpstestView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGpstestDoc)));
return (CGpstestDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGpstestView message handlers
void CGpstestView::OnClearButton()
{
// TODO: Add your control notification handler code here
Invalidate();
UpdateWindow();
m_strReceived="";
m_weidu = "";
m_jingdu = "";
m_gaodu= "";
m_shijian = "";
m_weizhi = "";
m_shuiping ="";
m_dingwei ="";
m_chuizhi ="";
m_riqi = "";
m_sudu = "";
m_data = "";
UpdateData(FALSE);
}
void CGpstestView::OnCloseButton()
{
// TODO: Add your control notification handler code here
if( !m_ctrlComm1.GetPortOpen())
m_ctrlComm1.SetPortOpen(true);
m_ctrlComm1.SetPortOpen(false);
m_strReceived="";
m_weidu = "";
m_jingdu = "";
m_gaodu= "";
m_shijian = "";
m_weizhi = "";
m_shuiping ="";
m_dingwei ="";
m_chuizhi ="";
m_riqi = "";
m_sudu = "";
m_data = "";
UpdateData(FALSE);
Invalidate();
UpdateWindow();
}
void CGpstestView::OnStartButton()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString c=m_com;
CString bote=m_bote;
CString str=bote+","+"n"+","+"8"+","+"1";
int s=atoi(c.Mid(3,1));
if(m_ctrlComm1.GetPortOpen())
m_ctrlComm1.SetPortOpen(FALSE);
m_ctrlComm1.SetCommPort(s); //选择com1
if( !m_ctrlComm1.GetPortOpen())
m_ctrlComm1.SetPortOpen(TRUE);//打开串口
else
AfxMessageBox("cannot open serial port");
m_ctrlComm1.SetSettings(str); //波特率4800,无校验,8个数据位,1个停止位
m_ctrlComm1.SetInputMode(1); //1:表示以二进制方式检取数据2:十六进制检取数据
m_ctrlComm1.SetRThreshold(1);
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_ctrlComm1.SetInputLen(0); //设置当前接收区数据长度为0
m_ctrlComm1.GetInput(); //先预读缓冲区以清除残留数据
SetTimer(1,1000,NULL);
}
void CGpstestView::OnStopButton()
{
// TODO: Add your control notification handler code here
m_ctrlComm1.SetPortOpen(FALSE);
}
void CGpstestView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
send();
CFormView::OnTimer(nIDEvent);
}
BEGIN_EVENTSINK_MAP(CGpstestView, CFormView)
//{{AFX_EVENTSINK_MAP(CGpstestView)
ON_EVENT(CGpstestView, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CGpstestView::OnComm()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integer that is not signed.
if(m_ctrlComm1.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{ ///????????????/////以下你可以根据自己的通信协议加入处理代码
variant_inp=m_ctrlComm1.GetInput(); //读缓冲区
safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k); //转换为BYTE型数组
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k); //字符型
m_strReceived+=char(bt); //加入到对应字符串中
}
}
m_data=m_strReceived;
UpdateData(false);
}
void CGpstestView::send()
{
while((m_strReceived.Find(0x0d)!=-1)&&(m_strReceived.Find(0x0a)!=-1))//循环读取GPGGA定位语句
{
//startLF 缓冲区中第一个GPS语句结束标志LF的位置 就是下一个GPS开始的标志
int startLF=m_strReceived.Find(0x0a);
int endCR=m_strReceived.Find(0x0d); //endCR 第一个GPS语句结束标志CR的位置
if(startLF>endCR) //找到GPS语句
endCR=m_strReceived.Find(0x0d,startLF);
CString msg=m_strReceived.Mid(startLF+1,endCR-startLF-1); //复制这个GPS语句,
m_strReceived.Delete(0,endCR+1); // 从缓冲区删去上个结束标志LF到本句结束标志CR间的数据。
//判断是否是$GPGGA语句,不是则跳过本次循环,是则删除$GPGGA,继续处理本句其他语句
if(msg.Left(6).Compare("$GPGGA")!=0 && msg.Left(6).Compare("$GPGSA")!=0 && msg.Left(6).Compare("$GPGSV")!=0 && msg.Left(6).Compare("$GPRMC")!=0 )
continue;
if(msg.Left(6).Compare("$GPGGA")==0)
{
msg.Delete(0,msg.Find(',')+1);
//获取时间信息
CString hour=msg.Left(2);
int hour2=atoi(hour)+8;
CString hour3;
if (hour2<24)
{
hour3.Format("%d",hour2);}
else
{
hour3.Format("%d",hour2-24);
}
CString min=msg.Mid(2,2);
CString sec=msg.Mid(4,2);
m_shijian=hour3+":"+min+":"+sec;
msg.Delete(0,msg.Find(',')+1);
//获取纬度信息,//
CString latidegree=msg.Left(2);
CString laticent=msg.Mid(2,7); // mid表示从CString中第二个字符开始copy7个
double latitude=(atof(latidegree)+atof(laticent)/60.0);
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1); //删去N,
//获取经度信息
CString longidegree=msg.Left(3);
CString longicent=msg.Mid(3,7);
double longitude=(atof(longidegree)+atof(longicent)/60.0);
msg.Delete(0,msg.Find(',')+1); //find是找到","所在的位置n, delete从位置0开始删去n+1个字符
msg.Delete(0,msg.Find(',')+1); //返回修改后的字符串长度
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
//获取高度信息
CString height=msg.Left(7);
double high=atof(height);
// 获得海平面偏差
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
CString diff=msg.Left(5);
double d=atof(diff); //海平面偏差可以为负值
high=high+d;
//以后的信息字段忽略
msg.Delete(0,msg.GetLength());
//整理数据,存储经纬度和高度
lonti.Format("%f",longitude); //经度
lati.Format("%f",latitude);
highh.Format("%f",high);
if(longitude!=0)
{
m_weidu=lonti;
m_jingdu=lati;
m_gaodu=highh;
}
else
{
m_weidu="";
m_jingdu="";
m_gaodu="";
}
sa="";
}
else
{
if(msg.Left(6).Compare("$GPGSA")==0)
{
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
CString dingwei1=msg.Left(1);
h=atoi(dingwei1);
if (h>1)
{ m_dingwei=dingwei1+"D定位";
}
else
{
m_dingwei="未定位";
}
for (int u=0;u<12;u++)
{
msg.Delete(0,msg.Find(',')+1);
sa+=msg.Mid(0,msg.Find(','))+",";
}
msg.Delete(0,msg.Find(',')+1);
CString weizhi1=msg.Mid(0,msg.Find(','));
if(h>1){
m_weizhi=weizhi1;
}
else
{
m_weizhi="";
}
msg.Delete(0,msg.Find(',')+1);
CString shuiping1=msg.Mid(0,msg.Find(','));
if(h>1){
m_shuiping=shuiping1;
}
else
{
m_shuiping="";
}
msg.Delete(0,msg.Find(',')+1);
CString chuizhi1=msg.Mid(0,msg.Find('*'));
if(h>1){
m_chuizhi=chuizhi1;
}
else
{
m_chuizhi="";
}
}
else
{
if(msg.Left(6).Compare("$GPRMC")==0)
{
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
CString sudu1=msg.Mid(0,msg.Find(','));
m_sudu=sudu1;
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
CString riqi1=msg.Left(2);
CString riqi2=msg.Mid(2,2);
CString riqi3=msg.Mid(4,2);
m_riqi="20"+riqi3+"/"+riqi2+"/"+riqi1;
}
else
{ if(msg.Left(6).Compare("$GPGSV")==0)
{
msg.Delete(0,msg.Find(',')+1);
msg.Delete(0,msg.Find(',')+1);
if(atoi(msg.Mid(0,msg.Find(',')))==1){
msg.Delete(0,msg.Find(',')+1);
CString keshu1=msg.Mid(0,msg.Find(','));
n=atoi(keshu1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -