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

📄 gpstestdlg.cpp

📁 有关GPS的编程源码。很有参考价值!值得一看
💻 CPP
字号:
// GPSTESTDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GPSTEST.h"
#include "GPSTESTDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGPSTESTDlg dialog
HANDLE hcom=INVALID_HANDLE_VALUE;
bool m_connect=false;
HWND m_dlgwnd=NULL;
HANDLE hreadthread=INVALID_HANDLE_VALUE;
//HANDLE hprocessthread=INVALID_HANDLE_VALUE;
char g_buf[512]={'\0'};



CGPSTESTDlg::CGPSTESTDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPSTESTDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGPSTESTDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGPSTESTDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPSTESTDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGPSTESTDlg, CDialog)
	//{{AFX_MSG_MAP(CGPSTESTDlg)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
	ON_BN_CLICKED(IDC_EXIT, OnCancle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPSTESTDlg message handlers

BOOL CGPSTESTDlg::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
}

DWORD WINAPI readthread(LPVOID lpara)
{
   	int i=0,lenth=0,j=0;
	DWORD dwstate,dwread,dwerror;
	COMSTAT comstat;
	HANDLE hprocessthread=INVALID_HANDLE_VALUE;
	SetCommMask(hcom,EV_RXCHAR);
	WCHAR * point=NULL;
	WCHAR tcharbuf[512];
	WCHAR latitude[15];
	WCHAR north_south,west_east;
	WCHAR longtitude[15];
    WCHAR flag_gprmc[]=TEXT("GPRMC");
    WCHAR totaltude[100];
    char recvbuf[512];
    memset(recvbuf,0,512*sizeof(char));
	memset(tcharbuf,0,sizeof(TCHAR)*512);
    HWND hwnd=(HWND)lpara;
	HWND hstatus=::GetDlgItem(hwnd,IDC_STATUS);
	HWND htext=::GetDlgItem(hwnd,IDC_TEXT);
    
	while(m_connect)
	{
	i=0;j=0;
	WaitCommEvent(hcom,&dwstate,NULL);
	SetCommMask(hcom,EV_RXCHAR);
	if(dwstate&EV_RXCHAR)
	{
		memset(recvbuf,0,512*sizeof(char));
	    memset(latitude,0,sizeof(WCHAR)*10);
        memset(longtitude,0,sizeof(WCHAR)*10);
		ClearCommError(hcom,&dwerror,&comstat);
		lenth=comstat.cbInQue;
		if(lenth<512)
		continue;

		ReadFile(hcom,recvbuf,512,&dwread,NULL);
		
		mbstowcs(tcharbuf,recvbuf,512);
		point=wcsstr(tcharbuf,flag_gprmc);
        if(*point==NULL||*(point+80)==NULL)
		{
		continue;
		}
		else
		{
          	memset(latitude,0,sizeof(WCHAR)*15);
            memset(longtitude,0,sizeof(WCHAR)*15);

          while(*point++!=',');
          while(*point++!=',');
		  while(*point++!=',');

			while( *point!=',')
			{
		   latitude[i++]=*point++;
			}
		  point++;
		  north_south=*point++;
		  point++;
		  while(*point!=',')
		  {
		  longtitude[j++]=*point++; 
		  }
          point++;
		  west_east=*point++;
          if(*point!=',')
		  {
		  continue;
		  }
		  else
		  {
		   	memset(totaltude,0,sizeof(WCHAR)*100);
		    wcscpy(totaltude,latitude);
		    wcscat(totaltude,&north_south);
			wcscat(totaltude,TEXT("\r\n"));
            wcscat(totaltude,longtitude);
			wcscat(totaltude,&west_east);
            wcscat(totaltude,TEXT("\r\n"));
            ::SetWindowTextW(htext,totaltude);

		  }
			  ::SetWindowTextW(hstatus,tcharbuf);
    
	}
	   Sleep(100);

	}
	}
return 1;

}
void CGPSTESTDlg::OnConnect() 
{
	m_dlgwnd=this->GetSafeHwnd();
    DCB dcb;
	COMMTIMEOUTS time;
	DWORD readid,processid;
    
	if(hcom!=INVALID_HANDLE_VALUE)
		MessageBox(TEXT("hcom already open"),NULL,MB_OK);
	hcom=CreateFile(TEXT("COM1:"),GENERIC_READ | GENERIC_WRITE,
		0, NULL, OPEN_EXISTING,0, NULL);
	if(!hcom)
	{
	
		MessageBox(TEXT("hcom open error"),NULL,MB_OK);
		return;
	}
	else
   
   	GetCommState(hcom,&dcb);
	dcb.DCBlength=sizeof(dcb);
	dcb.BaudRate=4800;
	dcb.ByteSize=8;
	dcb.Parity=NOPARITY;
	dcb.StopBits=ONESTOPBIT;
	
	if(!SetCommState(hcom,&dcb))
	{MessageBox(TEXT("setdcb error"),NULL,MB_OK);
	 return ;
	
	}
    GetCommTimeouts(hcom,&time);
	time.ReadIntervalTimeout=MAXDWORD;
	time.ReadTotalTimeoutConstant=0;
	time.ReadTotalTimeoutMultiplier=0;
	time.WriteTotalTimeoutConstant=1000;
	time.WriteTotalTimeoutMultiplier=10;
    SetCommTimeouts(hcom,&time);
    SetCommMask(hcom,EV_RXCHAR);
    PurgeComm(hcom,PURGE_TXCLEAR|PURGE_RXCLEAR);
    	//thread
	hreadthread=CreateThread(NULL,0,readthread,(LPVOID)this->GetSafeHwnd(),0,&readid);
	
	if(!hreadthread)
		MessageBox(TEXT("read thread error"),NULL,MB_OK);
	
  	m_connect=true;
	
}

void CGPSTESTDlg::OnDisconnect() 
{
	DWORD dwexit;
	::GetExitCodeThread(hreadthread,&dwexit);	
	::TerminateThread(hreadthread,dwexit);
	::CloseHandle(hcom);
	CDialog::OnDestroy();
}

void CGPSTESTDlg::OnCancle() 
{
	CDialog::OnCancel();	
}

⌨️ 快捷键说明

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