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

📄 gpscomdlg.cpp

📁 串口读取gps数据,然后通过华为gtm900gprs模块发送sms短信
💻 CPP
字号:
// GPSCOMDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GPSCOM.h"
#include "GPSCOMDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_MY_MESSAGE                32772
/////////////////////////////////////////////////////////////////////////////
// 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()


CGPSCOMDlg::CGPSCOMDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPSCOMDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGPSCOMDlg)
	m_strRecv = _T("");
	m_jd = _T("");
	m_wd = _T("");
	m_strSend = _T("");
	m_strServiceNo = _T("");
	m_strDestNo = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGPSCOMDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPSCOMDlg)
	DDX_Text(pDX, IDC_EDIT1, m_strRecv);
	DDX_Text(pDX, IDC_EDIT2, m_jd);
	DDX_Text(pDX, IDC_EDIT3, m_wd);
	DDX_Text(pDX, IDC_EDIT4, m_strSend);
	DDX_Text(pDX, IDC_EDIT5, m_strServiceNo);
	DDX_Text(pDX, IDC_EDIT6, m_strDestNo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGPSCOMDlg, CDialog)
	//{{AFX_MSG_MAP(CGPSCOMDlg)
	ON_BN_CLICKED(IDC_BTNCONN, OnBtnconn)
	ON_BN_CLICKED(IDC_BTNDISCONN, OnBtndisconn)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_BTNSAVE, OnBtnsave)
	ON_BN_CLICKED(IDC_BTNCONN1, OnBtnconn1)
	ON_BN_CLICKED(IDC_BTNDISCONN1, OnBtndisconn1)
	ON_BN_CLICKED(IDC_BTNOUT, OnBtnout)
	ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPSCOMDlg message handlers

BOOL CGPSCOMDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CGPSCOMDlg::OnBtnconn() 
{
	m_ceSeries.m_OnSeriesRead = OnSeriesRead;//回调函数的地址
	m_ceSeries.m_pPortOwner=this;//CCESeriese的交窗口
	if (!m_ceSeries.OpenPort())
	{
		AfxMessageBox("打开串口失败");
	}
	else  //打开成功的话,将断开连接按钮设为可用
	{
		CButton *pBtnConn = (CButton*)GetDlgItem(IDC_BTNCONN);
		CButton *pBtnDisConn = (CButton*)GetDlgItem(IDC_BTNDISCONN);
		CButton *pBtnExit = (CButton*)GetDlgItem(IDC_EXIT);
		
		ASSERT(pBtnConn != NULL);
		ASSERT(pBtnDisConn != NULL);
		ASSERT(pBtnExit != NULL);

		pBtnConn->EnableWindow(FALSE);
		pBtnDisConn->EnableWindow(TRUE);
		pBtnExit->EnableWindow(FALSE);
	}
}

void CGPSCOMDlg::OnBtndisconn() 
{	//关闭串口
	m_ceSeries.ClosePort();
	//将建立连接和退出按钮设为可用
	CButton *pBtnConn = (CButton*)GetDlgItem(IDC_BTNCONN);
	CButton *pBtnDisConn = (CButton*)GetDlgItem(IDC_BTNDISCONN);
	CButton *pBtnExit = (CButton*)GetDlgItem(IDC_EXIT);
	
	ASSERT(pBtnConn != NULL);
	ASSERT(pBtnDisConn != NULL);
	ASSERT(pBtnExit != NULL);

	pBtnConn->EnableWindow(TRUE);
	pBtnDisConn->EnableWindow(FALSE);
	pBtnExit->EnableWindow(TRUE);
}

void CGPSCOMDlg::OnExit() 
{
	//退出
	try{
		PostMessage(WM_CLOSE);
	}
	catch(CException* e){
	AfxMessageBox("ReadSection error");
	e->Delete();
	return ;
	}
}
void CGPSCOMDlg::OnSeriesRead(CWnd* pWnd,BYTE* buf,int bufLen)
{
	CGPSCOMDlg *pDlg = (CGPSCOMDlg *)pWnd;
	CEdit * pEdit,*pEdit1,*pEdit2,*pEdit3;
	pEdit = (CEdit*)pDlg->GetDlgItem(IDC_EDIT1);
	pEdit1 = (CEdit*)pDlg->GetDlgItem(IDC_EDIT2);
	pEdit2 = (CEdit*)pDlg->GetDlgItem(IDC_EDIT3);
	pEdit3 = (CEdit*)pDlg->GetDlgItem(IDC_EDIT4);
	//在CEdit中显示接收到的数据


	CString strRecv= (char*)buf;
	CString strtemp,strtemp1;
	int  lenth  =  strRecv.GetLength();
	int pos=strRecv.Find("GPRMC",1);//GPRMC
	if (pos>0 ){
		if(pos+39<lenth){
		//AfxMessageBox("");
		strtemp=strRecv.Mid(pos+15,11);
		pEdit2->SetWindowText(strtemp);
		strtemp1=strRecv.Mid(pos+27,12);
		pEdit1->SetWindowText(strtemp1);
		pEdit3->SetWindowText("经度:"+strtemp1+"  纬度:"+strtemp);
		}
	}



	CString strOld;
	pEdit->GetWindowText(strOld);  
	pEdit->SetWindowText(strOld + strRecv);
	int rowCount; 
	rowCount=pEdit->GetLineCount();//CEdit控件多行显示自动滚动
	pEdit->LineScroll(rowCount); 
   
	delete[] buf;
}

void CGPSCOMDlg::OnBtnsave() 
{
		CString str;
		str="GPS文件(*.gps)|*.GPS||";
		CFileDialog dlg(FALSE, // TRUE for FileOpen, FALSE for FileSaveAs
						".GPS",
						"GPS文件",
						OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
						str,
						this);
		dlg.m_ofn.lpstrTitle="保存GPS文件";
		if(dlg.DoModal()==IDOK)
		{
			CStdioFile file;
			CString m_fileName;
			m_fileName=dlg.GetPathName();
			if(!file.Open(m_fileName,CFile::modeCreate|CFile::modeWrite))
			{
				AfxMessageBox("不能保存");
			}

			
			CEdit *pEdit1,*pEdit2;
			pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT2);
			pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT3);

			CString strOld1;
			pEdit1->GetWindowText(strOld1);  
			CString strOld2;
			pEdit2->GetWindowText(strOld2);  
			UpdateData(true);
			file.WriteString(m_strServiceNo+"\r\n");
			file.WriteString(m_strDestNo+"\r\n");
			file.WriteString(strOld1+"\r\n");
			file.WriteString(strOld2+"\r\n");

			file.Close();
				
			return;
		}
}

void CGPSCOMDlg::OnBtnconn1() 
{	
	m_ceSeries.OpenPort1();

}

void CGPSCOMDlg::OnBtndisconn1() 
{
	BYTE * buf;
	buf=new BYTE(10);
	CString s="你好asdfasd";
	CopyMemory(buf,s.GetBuffer(s.GetLength()),10);
	delete[] buf;

}

void CGPSCOMDlg::OnBtnout() 
{
	try{
		UpdateData(TRUE);

		CString strtemp[5];

		strtemp[0]="AT+CMGF=0\r";
		strtemp[1]="AT+CSCA=\x22+8613800351500\x22\r";
		strtemp[2]="AT+CMGF=0\r";
		CString PDUlen;
		CString strPDUStr;
		PDUlen=GetPDU(m_strSend , 
							m_strDestNo ,
							&strPDUStr,
							m_strServiceNo );
		//strtemp[3]="AT+CMGS=21\r";
		strtemp[3]=CString("AT+CMGS=")+PDUlen+"\r";
		strtemp[4]=strPDUStr+"\x1a";

	//	strtemp[3]="AT+CMGS=21\r";
	//	strtemp[4]="0891683108301505F011000D91685119900693F6000800064F60597D597D\x1a";
		BYTE * buf;

		//buf = new BYTE[512];
		m_ceSeries.OpenPort1();
		Sleep(10);
		for(int i=0;i<5;i++){
			
			int bufLen = strtemp[i].GetLength()*2;
			buf = new BYTE(bufLen);
			//CopyMemory(buf,strtemp[i].GetBuffer(strtemp[i].GetLength()),bufLen);
			buf=(BYTE *)strtemp[i].GetBuffer(bufLen);
			if (!m_ceSeries.WritePort(buf,bufLen))
			{
				AfxMessageBox("写入失败");
				break;
			}
			Sleep(1000);
		}
		
	}
	catch(CException* e){
	AfxMessageBox("ReadSection error");
	e->Delete();
	return ;
	}
	m_ceSeries.ClosePort1();
}

void CGPSCOMDlg::OnBtnopen() 
{
		CString str;
		str="GPS文件(*.gps)|*.GPS||";
		CFileDialog dlg(true, // TRUE for FileOpen, FALSE for FileSaveAs
						".GPS",
						"GPS文件",
						OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
						str,
						this);
		dlg.m_ofn.lpstrTitle="打开GPS文件";
		if(dlg.DoModal()==IDOK)
		{
			CStdioFile file;
			CString m_fileName;
			m_fileName=dlg.GetPathName();
			if(!file.Open(m_fileName,CFile::modeRead))
			{
				AfxMessageBox("不能打开");
			}

			
			CEdit *pEdit1,*pEdit2,*pEdit3;

			pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT2);
			pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT3);
			pEdit3 = (CEdit*)GetDlgItem(IDC_EDIT4);
 
			CString strOld1;
			pEdit1->GetWindowText(strOld1);  
			CString strOld2;
			pEdit2->GetWindowText(strOld2);  
			
			file.ReadString(m_strServiceNo);
			file.ReadString(m_strDestNo);

			file.ReadString(strOld1);
			file.ReadString(strOld2);

			m_strServiceNo=m_strServiceNo.Left(m_strServiceNo.GetLength()-1);
			m_strDestNo=m_strDestNo.Left(m_strDestNo.GetLength()-1);
			strOld1=strOld1.Left(strOld1.GetLength()-1);
			strOld2=strOld2.Left(strOld2.GetLength()-1);
			UpdateData(false);
  
			pEdit1->SetWindowText(strOld1);  
			pEdit2->SetWindowText(strOld2); 
			pEdit3->SetWindowText("经度:"+strOld1+"  纬度:"+strOld2);
			file.Close();
				
			return;
		}

}
/*
'功能: 生成PDU串
'输入: 短信息内容、目标手机号码、[可选的短信服务中心号码]
'输出: 生成的PDU串
'返回: 整个字串的长度
'
*/
CString CGPSCOMDlg::GetPDU(CString SMSText , 
                        CString DestNo ,
                        CString * PDUString ,
                        CString ServiceNo )
{
	CString strtemp,strtemp2,strtemp3;
	strtemp="089168";
	strtemp2="";
	strtemp3="";
	if(DestNo.GetLength()==11)
	{
		DestNo = DestNo + "F";
	}
	if(ServiceNo.GetLength()==11)
	{
		ServiceNo = ServiceNo + "F";
	}
	for(int i=0;i<ServiceNo.GetLength();i=i+2)
	{
		strtemp2=ServiceNo.Mid(i,2);
		strtemp=strtemp+strtemp2.Right(1)+strtemp2.Left(1);
	}
	strtemp=strtemp+"11000D9168";
	for(int j=0;j<DestNo.GetLength();j=j+2)
	{
		strtemp2=DestNo.Mid(j,2);
		strtemp=strtemp+strtemp2.Right(1)+strtemp2.Left(1);
	}
	strtemp=strtemp+"000800";
	strtemp3=ToUnicode(SMSText);
	//int l=strtemp3.GetLength()/2;
	//strtemp2="";
	//strtemp2.Format("%x",l);

	strtemp=strtemp+strtemp3;
	*PDUString=strtemp;
	int	l=strtemp.GetLength()/2-9;
	strtemp2="";
	strtemp2.Format("%d",l);
	return strtemp2;
}

CString CGPSCOMDlg::ToUnicode(CString msg)
{
CString retstr;
   
   char aa[500]="";
   int len=msg.GetLength();
   BSTR x=msg. AllocSysString();
   long num=*((long*)x-1);   
   for(int i=0;i<num;i+=1)
   {
    if(i>=len)
     break;
     char cc[10]="";
  if((*x>>8)==0)
  {
   sprintf(aa,"%s00%x",aa,*x);
     msg=msg.Right (msg.GetLength ()-1);
   
  }
  else
  {
   sprintf(aa,"%s%x",aa,*x);
   msg=msg.Right (msg.GetLength ()-2);
   i=i+1;
  }
  SysFreeString(x); 
 
  x=msg. AllocSysString();
   }
  SysFreeString(x);
   char bb[10]="";
   int len1=strlen(aa);
   char txt[500]="";
   if(len1%4!=0)
   {
    memcpy(txt,aa,(len1-3)); 
   }
   else
   {
    memcpy(txt,aa,len1);
   }
   if(num<=0x0f)
   {
    sprintf(bb,"0%x",num);
   }
   else
   {
    sprintf(bb,"%x",num);
   }
   retstr.Format ("%s%s",bb,txt);
   retstr.MakeUpper ();
   int msglth=retstr.GetLength();  
   return retstr;


}

⌨️ 快捷键说明

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