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

📄 maintestdlg.cpp

📁 一个GPS与串口通信的程序
💻 CPP
字号:
// maintestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "maintest.h"
#include "maintestDlg.h"
#include "gps.h"


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

/////////////////////////////////////////////////////////////////////////////
// CMaintestDlg dialog

CMaintestDlg::CMaintestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMaintestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMaintestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMaintestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMaintestDlg)
	DDX_Control(pDX, IDC_MSCOMM2, m_ctrlComm2);
	DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMaintestDlg, CDialog)
	//{{AFX_MSG_MAP(CMaintestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START_BUTTON, OnStart)
	ON_BN_CLICKED(IDC_STOP_BUTTON, OnStop)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMaintestDlg message handlers

BOOL CMaintestDlg::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
	
	// TODO: Add extra initialization here
	//打开串口1接收GPS数据
	if(m_ctrlComm1.GetPortOpen())
		m_ctrlComm1.SetPortOpen(FALSE);
	m_ctrlComm1.SetCommPort(1);              //选择com1
	if( !m_ctrlComm1.GetPortOpen())
		m_ctrlComm1.SetPortOpen(TRUE);//打开串口
	else
		AfxMessageBox("cannot open serial port");
	m_ctrlComm1.SetSettings("4800,n,8,1"); //波特率4800,无校验,8个数据位,1个停止位
	m_ctrlComm1.SetInputMode(1);      //1:表示以二进制方式检取数据2:十六进制检取数据
    m_ctrlComm1.SetRThreshold(1);  
    //参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
    m_ctrlComm1.SetInputLen(0);   //设置当前接收区数据长度为0
    m_ctrlComm1.GetInput();    //先预读缓冲区以清除残留数据	


   //打开串口2 发送目标方位信息
	if(m_ctrlComm2.GetPortOpen())
		m_ctrlComm2.SetPortOpen(FALSE);
	m_ctrlComm2.SetCommPort(2);              //选择com2
	if( !m_ctrlComm2.GetPortOpen())
		m_ctrlComm2.SetPortOpen(TRUE);//打开串口
	else
		AfxMessageBox("cannot open serial port");
	m_ctrlComm2.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位

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

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

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

void CMaintestDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	SetTimer(1,1000,NULL);	
}

void CMaintestDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
}

void CMaintestDlg::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);           //加入到对应字符串中 
		}
 	}

}

void CMaintestDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
 	send();	
	CDialog::OnTimer(nIDEvent);
}

void CMaintestDlg::send()
{
	//下面提取gps数据(经纬度和高度)
  	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)
			continue;
		msg.Delete(0,msg.Find(',')+1);
/*		//获取UCT时间信息转换成时、分、秒
	    char* pTime=msg.GetBuffer(msg.GetLength()+1);
		int hour,min,sec;
		char temp[9];
		memset(temp,0,9);
		memcpy(temp,pTime,2);        //将源字符串中地址码中2个字符copy到目的串中
		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);
    //获取纬度信息,// 
		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());
	//整理数据,存储经纬度和高度
		array[0]=longitude;   //经度
		array[1]=latitude;
		array[2]=high;
	}

	int angle[2];
	gps stage,obj,stor,delta,goal; 
    stage.setgps(104.351,30.417,3.35); //接近学校的地面坐标
    stor=stage;       //存储地面的经纬度
	//目标的位置信息
 	obj.setgps(array[0],array[1],array[2]);//	obj.setgps(104.0984,30.679,545.4);
	stage.convert();
 	obj.convert();
	delta=obj-stage;               //求得差值
	goal=transform(stor,delta);    //转换成法线坐标中 天线为原点的法线坐标系中目标的坐标
    goal.compute(goal,angle);      //计算出仰角和方位角

	CByteArray hexdata;
	hexdata.RemoveAll();
	hexdata.SetSize(21);
	{
		hexdata.SetAt( 0,0x7E);             //开始码         7E7EH
	    hexdata.SetAt( 1,0x7E);    
		hexdata.SetAt( 2,0x03);             //目的地址         03H
		hexdata.SetAt( 3,0x11);             //源地址           11H
		hexdata.SetAt( 4,0x00);             //长度           000FH
		hexdata.SetAt( 5,0x0F);
		hexdata.SetAt( 6,0x04);             //命令码           04H
		{
			hexdata.SetAt( 8,angle[0]);      //方位角           LLH
		    angle[0]>>=8;
		    hexdata.SetAt( 7,angle[0]);      //方位角           HHH     

			hexdata.SetAt( 9,0x7E);      //方位角           LLH
	
		    hexdata.SetAt( 10,0x7E);      //方位角           HHH  
			hexdata.SetAt( 11,0x7E);      //方位角           LLH
		 
		    hexdata.SetAt( 12,0x7E);      //方位角           HHH  

			hexdata.SetAt(14,angle[1]);      //副  仰       LLH
		    angle[1]>>=8;
		    hexdata.SetAt(13,angle[1]);      //方位角           HHH  

			hexdata.SetAt(15,0x7E);      //方位角           LLH
		    hexdata.SetAt(16,0x7E);      //方位角           HHH  			
		    hexdata.SetAt(17,0x00);     //俯仰角          LLH
		    hexdata.SetAt(18,0x00);       //俯仰角          HHH
	        hexdata.SetAt(19,0x66);          //结束码         66BBH 
            hexdata.SetAt(20,0x66);

		}
	}	
	m_ctrlComm2.SetOutput(COleVariant(hexdata)); //发送十六进制数据
}

⌨️ 快捷键说明

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