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

📄 gpsdevice.cpp

📁 Gpsapi 2.0(标准mobile接口)
💻 CPP
字号:
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2000-2008  YULONG Company                         */
/*                 宇龙计算机通信科技(深圳)有限公司  版权所有 2000-2008 */
/*                                                                        */
/* PROPRIETARY RIGHTS of YULONG Company are involved in the         */
/* subject matter of this material.  All manufacturing, reproduction, use,*/
/* and sales rights pertaining to this subject matter are governed by the */
/* license agreement.  The recipient of this software implicitly accepts  */ 
/* the terms of the license.                                              */
/* 本软件文档资料是宇龙公司的资产,任何人士阅读和使用本资料必须获得        */
/* 相应的书面授权,承担保密责任和接受相应的法律约束.                       */
/*                                                                        */
/**************************************************************************/

#include "stdafx.h"
#include "GpsDevice.h"
#include <math.h>
#include <pnp.h>
#include <pm.h>
#include "msgqueue.h"
//#include "Pwinuser.h"

#ifndef _DISABLE_8360_GPS_
#ifdef _VER_8360_
#include "gpsmain.h"
#endif
#endif

#define PI    3.1415926535897932

static long Distance(long nX1, long nY1, long nX2, long nY2)
{   // 两点之间的距离 
	long nReturn = 0;
	double ndX = nX1 - nX2;
	double ndY = nY1 - nY2;
	nReturn = (long)sqrt(ndX * ndX + ndY * ndY);
	return nReturn;
}

CGpsDevice::CGpsDevice():
	m_hWnd(NULL)
{
	m_hComm = NULL;
	m_bGpsOpened = FALSE;
	m_bPause = FALSE;
	m_hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
	m_nComID    =  1;
	m_wBaudRate =  CBR_9600;
	m_nByteSize =  8;

	m_pszReceive = NULL;

	m_hNewLocationData = NULL;

	m_bLogGpsData = 0;
	m_bReadFormFile = 0;
	m_delayTime = 1.0;
//	InitializeCriticalSection(&m_csDataAccsee);
#ifndef _DISABLE_8360_GPS_
#ifdef _VER_8360_
	m_hGPS=INVALID_HANDLE_VALUE;
#endif
#endif
}

CGpsDevice::~CGpsDevice()
{
	
	m_bGpsOpened = FALSE;
	CutConnect();
	CloseHandle(m_hEvent );
	
	//	DeleteCriticalSection(&m_csDataAccsee);
}
#ifndef _DISABLE_8360_GPS_
#ifdef _VER_8360_
BOOL CGpsDevice::GPSOpen()
{		
		BOOL ret;
		
		if(INVALID_HANDLE_VALUE != m_hGPS)
		{
			GPSClose();
		}
		
		m_hGPS = CreateFile (TEXT("GPS1:"), // Pointer to the name of the module
			GENERIC_READ | GENERIC_WRITE,	// Access (read-write) mode
			0,            // Share mode
			NULL,         // Pointer to the security attribute
			OPEN_EXISTING,
			0,            // Module attributes
			NULL);        // Handle to Module with attribute
		
		if(!m_hGPS || (INVALID_HANDLE_VALUE == m_hGPS))
		{
			goto InitializeFail;
		}
		
		ret=DeviceIoControl(m_hGPS,IOCTL_GPS_POWER_ON,NULL,0,NULL,0,NULL,NULL);
		
		Sleep(500);
		GPSPowerReset();
		
		return TRUE;
		
InitializeFail:
		
		CloseHandle(m_hGPS);
		return FALSE;
}

void CGpsDevice::GPSClose()
{
		if(m_hGPS==INVALID_HANDLE_VALUE)
			return ;
		
		DeviceIoControl(m_hGPS,IOCTL_GPS_POWER_OFF,NULL,0,NULL,0,NULL,NULL);
		
		CloseHandle(m_hGPS);
		m_hGPS=INVALID_HANDLE_VALUE;
}

BOOL CGpsDevice::GPSPowerReset()
{
		return DeviceIoControl(m_hGPS,IOCTL_GPS_RESET,NULL,0,NULL,0,NULL,NULL);	
}
#endif
#endif

// 设置消息的窗口句柄
void CGpsDevice::SetMessageWnd( HWND hWnd )
{
	m_hWnd = hWnd;
}

void CGpsDevice::SetNewHandle(HANDLE hNewHandle)
{
	m_hNewLocationData = hNewHandle;
}
BOOL CGpsDevice::Connect()
{	
		BOOL fSuccess=FALSE;	
		COMMTIMEOUTS CommTimeOuts;
		DCB dcb;
		TCHAR strCom[12];
		
		if(m_bGpsOpened)
			return TRUE;

#ifndef _DISABLE_8360_GPS_	
#ifdef _VER_8360_
		GPSOpen();
#endif
#endif
		
		wsprintf(strCom, _T("COM%d:"), m_nComID);	
		m_hComm = CreateFile (strCom, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);	
		if( m_hComm == INVALID_HANDLE_VALUE ) 
		{
			DWORD nErr = GetLastError();
			return FALSE;
		}
		
		memset(&dcb,0,sizeof(DCB));
		dcb.DCBlength = sizeof(DCB);
		
		fSuccess=GetCommState( m_hComm, &dcb );
		if (!fSuccess ) 
		{
			int err = GetLastError();
			//DEBUGMSGA(ZONE_ERROR,("COM%d GetCommState Error:%d\r\n",dev,err));
			CloseHandle(m_hComm);
			m_hComm = INVALID_HANDLE_VALUE;
			return FALSE;
		}
		
		dcb.BaudRate  = m_wBaudRate;/*CBR_4800 CBR_9600*/
		dcb.ByteSize  = m_nByteSize;
		dcb.Parity    = NOPARITY;
		dcb.StopBits  = ONESTOPBIT;
		//dcb.fOutxCtsFlow = TRUE;
		//dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
		//dcb.fAbortOnError=TRUE;
		
		SetCommState( m_hComm, &dcb );
		if (!fSuccess ) 
		{
			int err = GetLastError();
			//DEBUGMSGA(ZONE_ERROR,("COM%d GetCommState Error:%d\r\n",dev,err));
			CloseHandle(m_hComm);
			m_hComm = INVALID_HANDLE_VALUE;
			return FALSE;
		}
		
		memset(&CommTimeOuts,0,sizeof(COMMTIMEOUTS));
		GetCommTimeouts(m_hComm,&CommTimeOuts);
		CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
		CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
		CommTimeOuts.ReadTotalTimeoutConstant = 0;
		CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
		CommTimeOuts.WriteTotalTimeoutConstant = 5000;
		SetCommTimeouts( m_hComm, &CommTimeOuts );
		
		m_bGpsOpened = TRUE;
		m_bPause  = FALSE;
		
		m_pszReceive = (char*)malloc(MAX_READBUFFER);
		if(!m_pszReceive)
		{
			CloseHandle(m_hComm);
			m_hComm = INVALID_HANDLE_VALUE;
			return FALSE;
		}
		memset(m_pszReceive,0,MAX_READBUFFER);	
		
		return TRUE;
}

void CGpsDevice::CutConnect()
{
	SetEvent(m_hEvent);	
		if(m_bGpsOpened)
		{	
			m_bGpsOpened = FALSE;
			CloseHandle(m_hComm);
			m_hComm=INVALID_HANDLE_VALUE;
		}	
		
		if(m_pszReceive)
		{
			free(m_pszReceive);
			m_pszReceive=NULL;
		}	
#ifndef _DISABLE_8360_GPS_		
#ifdef _VER_8360_
		GPSClose();
#endif
#endif

		m_nmeaString.FreeBuf();
}


GPS_POSITION* CGpsDevice::GetData()
{
	return &m_GpsDataInfo;
}

GPSData* CGpsDevice::GetUnfiltedData()
{
	return m_nmeaString.GetGPSData();
}

BOOL CGpsDevice::ReadLine()
{	
		unsigned long nReadNumber;
		int m_nReceivedLength = 0;
		
		COMSTAT comstat;
		DWORD    dwError;
		BOOL    bError = FALSE;
		BOOL	bEnd = FALSE;
		
		if(!m_bGpsOpened || m_pszReceive==NULL)
			return FALSE;
		
		ClearCommError( m_hComm, &dwError, &comstat );
		
		int nByteToRead = comstat.cbInQue;
		if(nByteToRead>MAX_READBUFFER)
			nByteToRead=MAX_READBUFFER-1;
		
		memset(m_pszReceive,0,nByteToRead+1);
		if(!nByteToRead || !ReadFile( m_hComm, m_pszReceive, nByteToRead, &nReadNumber,NULL))
			return FALSE;
		
		return TRUE;
}

BOOL CGpsDevice::Config(int nComID, int nBaudRate, int nByteSize)
{
	m_nComID = nComID;
	m_wBaudRate = nBaudRate;
	m_nByteSize = nByteSize;

	return TRUE;
}


UINT CGpsDevice::GetStatus()
{	
	//return m_GpsDataInfo.FixStatus;
	return 1;
}

BOOL CGpsDevice::GetSeting(int &nComID, int &nBaudRate, int &nByteSize)
{
	nComID = m_nComID;
	nBaudRate = m_wBaudRate;
	nByteSize = m_nByteSize;
	return TRUE;
}
 
BOOL CGpsDevice::Receive()
{   
	GPSData *pData=NULL,*pLastData=NULL;
	DWORD dwWaitCode = 1; // 
	
	//////////////////////////
#ifndef _DISABLE_8360_GPS_
#ifdef _VER_8360_
	//add by zhangfubin 20071013 for suspending process
	BYTE pbMsgBuf[sizeof(POWER_BROADCAST) + sizeof(POWER_BROADCAST_POWER_INFO)];
	MSGQUEUEOPTIONS msgopts;    
	HANDLE rghWaits[2] = { NULL };
	HANDLE hReq = NULL;
	int    cnt=0;
	

	// Create our message queue
	memset(&msgopts, 0, sizeof(msgopts));
    msgopts.dwSize = sizeof(msgopts);
    msgopts.dwFlags = 0;
    msgopts.dwMaxMessages = 0;
    msgopts.cbMaxMessage = sizeof(pbMsgBuf);
    msgopts.bReadAccess = TRUE;

	rghWaits[0]=m_hEvent;
	rghWaits[1] = CreateMsgQueue(NULL, &msgopts);
	if (!rghWaits[1]) 
	{		
		return FALSE;
	}

	// Request notifications
	hReq = RequestPowerNotifications(rghWaits[1], PBT_RESUME|PBT_TRANSITION);
	if (!hReq)
	{		
		CloseHandle(rghWaits[1]);
		return FALSE;
	}	
#endif
#endif
	//end by zhangfubin
	
	//循环接收GPS信号数据
	//while(WaitForSingleObject(m_hEvent,GPS_GET_SPAN) == WAIT_TIMEOUT)
	while(1)
	{
#ifndef _DISABLE_8360_GPS_
#ifdef _VER_8360_
		dwWaitCode = WaitForMultipleObjects(2, rghWaits, FALSE, GPS_GET_SPAN);
#else
		dwWaitCode = WaitForSingleObject(m_hEvent,GPS_GET_SPAN);
#endif		
#else
		dwWaitCode = WaitForSingleObject(m_hEvent,GPS_GET_SPAN);
#endif
		if(!m_bGpsOpened)
			return FALSE;
 		if (m_bPause==TRUE)
 			return FALSE;
		
		switch(dwWaitCode)
		{
		case WAIT_TIMEOUT:	
		case WAIT_OBJECT_0:	
			if(ReadLine())
			{			
				memset(&m_GpsDataInfo,0,sizeof(GPS_POSITION));		
				m_nmeaString.AppendBuffer(m_pszReceive, strlen(m_pszReceive));			
				pData = m_nmeaString.GetGPSData();
			//	pData=m_nmeaString.GetFiltedData();
				if(pData)
				{
					m_GpsDataInfo.dblLatitude = pData->dLat;
					m_GpsDataInfo.dblLongitude = pData->dLong;
					m_GpsDataInfo.flAltitudeWRTSeaLevel  = (float)m_nmeaString.m_dHigh;

					//UTC  TIME
					m_GpsDataInfo.stUTCTime.wYear = pData->gTime.wYear;
					m_GpsDataInfo.stUTCTime.wMonth = pData->gTime.wMonth;
					m_GpsDataInfo.stUTCTime.wDay = pData->gTime.wDay;
					m_GpsDataInfo.stUTCTime.wHour = pData->gTime.wHour;
					m_GpsDataInfo.stUTCTime.wMinute = pData->gTime.wMinute;
					m_GpsDataInfo.stUTCTime.wSecond = pData->gTime.wSecond;

					m_GpsDataInfo.flSpeed = (float)(pData->dSpeed*1.852);
					m_GpsDataInfo.flHeading = (float)pData->dAngle;

					m_GpsDataInfo.FixQuality =(pData->iQuality > 0)?GPS_FIX_QUALITY_GPS:GPS_FIX_QUALITY_UNKNOWN;
					m_GpsDataInfo.FixType = (m_nmeaString.m_sGSAData.iType>0)?((m_nmeaString.m_sGSAData.iType>2)?GPS_FIX_3D:GPS_FIX_2D):GPS_FIX_UNKNOWN;
					if (m_nmeaString.m_sGSAData.iMode == 1)
						m_GpsDataInfo.SelectionType = GPS_FIX_SELECTION_AUTO;
					else if (m_nmeaString.m_sGSAData.iMode == 2)
						m_GpsDataInfo.SelectionType = GPS_FIX_SELECTION_MANUAL;
					else
					   m_GpsDataInfo.SelectionType = GPS_FIX_SELECTION_UNKNOWN;

					//卫星信息
					m_GpsDataInfo.flPositionDilutionOfPrecision = (float)m_nmeaString.m_sGSAData.dPdop;
					m_GpsDataInfo.flHorizontalDilutionOfPrecision = (float)m_nmeaString.m_sGSAData.dHdop;
					m_GpsDataInfo.flVerticalDilutionOfPrecision = (float)m_nmeaString.m_sGSAData.dVdop;

					m_GpsDataInfo.dwSatelliteCount = pData->iStarNum;                // 定位卫星数
					m_GpsDataInfo.dwSatellitesInView = m_nmeaString.m_iStarNumCon;   // 可见卫星数

					for(int i = 0; i<GPS_MAX_SATELLITES; i++)
					{
						m_GpsDataInfo.rgdwSatellitesInViewPRNs[i] = m_nmeaString.m_sStarData[i].id;
						m_GpsDataInfo.rgdwSatellitesInViewElevation[i] = m_nmeaString.m_sStarData[i].angleV;
						m_GpsDataInfo.rgdwSatellitesInViewAzimuth[i] = m_nmeaString.m_sStarData[i].angleH;
						m_GpsDataInfo.rgdwSatellitesInViewSignalToNoiseRatio[i] = m_nmeaString.m_sStarData[i].sar;
					}

				//	memcpy(m_GpsDataInfo.StarData,m_nmeaString.m_sStarData,GPS_MAX_SATELLITES*sizeof(GPS_SATELLITE_INFO));



#ifndef _DISABLE_8360_GPS_
#ifdef _VER_8360_
					//判断是否需要复位,修正GPS模块bug,暂时这么使用
                    pData = m_nmeaString.GetGPSData();
					pLastData=m_nmeaString.GetLastGPSData();
					if(pLastData && pData->dLat!=0 && pData->dLong!=0 && (pData->dLat==pLastData->dLat) && (pData->dLong==pLastData->dLong))
					{
						cnt++;
						if(cnt>=5)
						{
							DeviceIoControl(m_hGPS,IOCTL_GPS_RESET,NULL,0,NULL,0,NULL,NULL);
							cnt=0;
						}
					}
					else 
					{
						cnt=0;
					}
#endif				
#endif
				}
				if (m_hNewLocationData != NULL)
				{
					SetEvent(m_hNewLocationData);
				}
			//	SendMessage(m_hWnd, USE_MSG_RECEIVEGPS, (WPARAM)&m_GpsDataInfo,NULL);
			}	
			else
			{
				//::MessageBox(NULL,L"完成",L"提示",MB_OKCANCEL);
			}
			break;
#ifndef _DISABLE_8360_GPS_
#ifdef _VER_8360_
			//待机处理
		case WAIT_OBJECT_0+1://待机启动处理
			{
				DWORD dwSize, dwFlags;		
				
				if(ReadMsgQueue(rghWaits[1], pbMsgBuf, sizeof(pbMsgBuf), &dwSize, 0, &dwFlags)) 
				{ 						
					switch(((PPOWER_BROADCAST)pbMsgBuf)->Message)
					{
					case PBT_TRANSITION:
						
						if(POWER_STATE_SUSPEND==((PPOWER_BROADCAST)pbMsgBuf)->Flags)
						{							
							DeviceIoControl(m_hGPS,IOCTL_GPS_POWER_OFF,NULL,0,NULL,0,NULL,NULL);																					
						}
						break;
					case PBT_RESUME:
						{					
							DeviceIoControl(m_hGPS,IOCTL_GPS_POWER_ON,NULL,0,NULL,0,NULL,NULL);
							DeviceIoControl(m_hGPS,IOCTL_GPS_RESET,NULL,0,NULL,0,NULL,NULL);
						}
						break;
					}
					
				}
			}				
			break;
#endif
#endif
		}		
	}


#ifndef _DISABLE_8360_GPS_
#ifdef _VER_8360_	
	if (hReq) StopPowerNotifications(hReq);
	if (rghWaits[1]) CloseHandle(rghWaits[1]);
#endif
#endif
	return TRUE;
}

⌨️ 快捷键说明

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