gps_parse.c

来自「gps 协议解析(凯立德内部用)包含LINUX版」· C语言 代码 · 共 797 行 · 第 1/2 页

C
797
字号
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include	"carnix_osal.h"
#include	"cnv_common.h"
#include	"HMI_CTRL.h"
#include	"cnv_hmi_Common.H"
#include	<windows.h>
#include	"cnv_hmi_GPSPublic.h"

//#define NAVI_VERSION_INTERMIT_GPSPORT     //此宏是中断方式访问GPS

#ifdef		NAVI_VERSION_DMEDIA
#include	"GpsAPI.h"
#endif


//#define		NAVI_GPS_LOG    //写日志
//#define		LGM_GPSEMU    //GPS信号模拟
//#define		ZHENHAN_GPS_COLDSTART   //正翰冷启动


//2006-10-25 dongwei add 
//用日期记录GPSLOG文件名
#ifdef	NAVI_GPS_LOG
//#define	LGM_GPSEMU_DATA
#endif

		
#ifdef			ZHENHAN_GPS_COLDSTART
#define			COLDSTART_CODE	"$PSRF104,0,0,0,96000,237759,922,12,4*2E\r\n"
#define			COLDSTART_SUC	"cold start successed"
#define			COLDSTART_FAILE	"cold start failed"
#endif




#ifdef		LGM_GPSEMU
#define		GPSEMUFILENAME		"GPS_EMU.LOG"
#endif

#ifdef	NAVI_GPS_LOG
	#define	GPS_LOG_FILE_NAME		"GPS_LOG_"
	#ifdef	NAVI_OUTPUT_DEBUG_LOCAL
		extern 	  char GPS_LogFilePath[80];
	#endif
	FILE*	GPS_Log_Fpt = NULL;
#endif


#define	GPS_COMM_PORT	"COM2:"

#define	GPS_COMM_BAUDRATE	4800
HANDLE	hCOM = INVALID_HANDLE_VALUE;

static	short	StopGPSSample = 0;

long isGpsEmu = 0;//是否是GPS信号模拟,如是,在GPSPublic中每收到一个RMC就Sleep(0.8秒) ls 2007-05-24

// 尚杨陀螺数据解析
#ifdef	NAVI_VERSION_SHANGYANG_TY
// 线程同步
HANDLE	s_hThreadEvent = 0;

extern	BOOL InitGyrSpd (void);
extern	BOOL GetGyrSpdValue(LONG* Angle, LONG* Distance, DWORD* Time);
extern	BOOL CloseGyrSpd (void);

#endif

//gqs.2006.09.05 宇龙..
//在没有收到宇龙蓝牙发送的消息之前
//不打开串口3.
#ifdef NAVI_VERSION_240x292_YULONG
	extern short g_iNavi_BT_OPENED;
	extern  void win_hmi_SendMessage_Com3_State(int iState);
#endif

#ifdef	LGM_GPSEMU_DATA
extern	void	win_hmi_GetSystemTimeString(char	*OutStringName);
#endif

#ifdef LGM_GPSEMU
HANDLE	InitGPSEMU()
{
	HANDLE hCOM;
	unsigned short tempWSTR[80];
	char	gps_emu_file[256];
    isGpsEmu = 1;
	sprintf(gps_emu_file, "%s/%s", SysENV_Ptr->MapMediaFilePath, GPSEMUFILENAME);
	hCOM = CreateFile(CXSYS_L2U_LE(gps_emu_file,tempWSTR,0),
					  GENERIC_READ,
					  0,
					  NULL,
					  OPEN_EXISTING,
					  0,
					  NULL);
	return	hCOM;
}
#endif

#ifdef NAVI_VERSION_240x292_YULONG
short	g_i_CloseGPS_YuLong = 0;
short	g_i_OpenGPS_YuLong = 1;
#endif

HANDLE Init_GPS()
{
	HANDLE hCOM;
	COMMTIMEOUTS CommTimeOuts;
	DCB dcb;
	unsigned short tempWSTR[80];
	unsigned long readed;

#ifdef	ZHENHAN_GPS_COLDSTART
	char	coldstart[100];
	short	coldstartlen = 0;
	short	iRtn = 0 ;
#endif

//gqs.2006.09.05 宇龙..
//在没有收到宇龙蓝牙发送的消息之前
//不打开串口3.
#ifdef NAVI_VERSION_240x292_YULONG
	//win_hmi_SendMessage_Com3_State(1);
	if (g_iNavi_BT_OPENED == 0)
	{
		return INVALID_HANDLE_VALUE;
	}
#endif

	memset(&dcb,0,sizeof(dcb));
	if (0 == OSAL_Runtime_Config.GPS_Port[0] )
		hCOM = CreateFile(CXSYS_L2U_LE(GPS_COMM_PORT,tempWSTR,0),
						  GENERIC_READ | GENERIC_WRITE,
						  0,
						  NULL,
						  OPEN_EXISTING,
						  0,
						  NULL);
	else
		hCOM = CreateFile(CXSYS_L2U_LE(OSAL_Runtime_Config.GPS_Port,tempWSTR,0), 
						  GENERIC_READ | GENERIC_WRITE,
						  0,
						  NULL,
						  OPEN_EXISTING,
						  0,
						  NULL);
	if (INVALID_HANDLE_VALUE == hCOM )
	{
		return hCOM;
	}

// 20060526 DMedia要求使用中断方式访问他们的GPS端口,否则会丢失数据
	//GetCommTimeouts(hCOM,&CommTimeOuts);
//#ifdef		NAVI_VERSION_DMEDIA
#if (defined NAVI_VERSION_DMEDIA) || (defined NAVI_VERSION_INTERMIT_GPSPORT)
	CommTimeOuts.ReadIntervalTimeout = 0;
   	CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
	CommTimeOuts.ReadTotalTimeoutConstant = 0;
   	CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
   	CommTimeOuts.WriteTotalTimeoutConstant = 600;
#else
	CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
   	CommTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD;
	CommTimeOuts.ReadTotalTimeoutConstant = 1000;
   	CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
   	CommTimeOuts.WriteTotalTimeoutConstant = 600;
#endif

	dcb.DCBlength = sizeof(dcb);

   	readed = GetCommState(hCOM, &dcb );
	if (0 == OSAL_Runtime_Config.GPS_BaudRate )
		dcb.BaudRate = GPS_COMM_BAUDRATE;
	else
		dcb.BaudRate = OSAL_Runtime_Config.GPS_BaudRate;
	dcb.StopBits = ONESTOPBIT;
	dcb.ByteSize = 8;
//#ifdef		NAVI_VERSION_DMEDIA
#if (defined NAVI_VERSION_DMEDIA) || (defined NAVI_VERSION_INTERMIT_GPSPORT)
	dcb.Parity = NOPARITY;
#else
	dcb.fParity = NOPARITY;
	dcb.fBinary = 1;
#endif
   	readed = SetCommState(hCOM, &dcb );
	readed = SetCommTimeouts(hCOM,&CommTimeOuts);


// 20060526 DMedia要求使用中断方式访问他们的GPS端口,否则会丢失数据
#ifdef		NAVI_VERSION_DMEDIA
{
	NMEAParam NmeaParam;
	if(StartGpsApi())
	{
		GetNMEAType(&NmeaParam);
		NmeaParam.gga = 1;
		NmeaParam.gsa = 1;
		NmeaParam.gsv = 1;
		NmeaParam.rmc = 1;
		SetNMEAType(&NmeaParam);
		StopGpsApi();
	}
}
#else
  	readed = SetupComm(hCOM,2048,2048);
	readed = SetCommMask(hCOM,0); 	
	PurgeComm( hCOM, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR| PURGE_RXCLEAR );
#endif

#ifdef	ZHENHAN_GPS_COLDSTART
	sprintf(coldstart,"%s",COLDSTART_CODE);
	coldstartlen = strlen(coldstart);
	WriteFile(hCOM,coldstart,coldstartlen,(unsigned long *)&iRtn,NULL);
#endif

//gqs.2006.09.04 宇龙..
//打开串口之前向MtapiServerWindow窗口发送一个串口3打开消息
#ifdef NAVI_VERSION_240x292_YULONG
	//1打开..
	if(g_i_OpenGPS_YuLong)
	{
		win_hmi_SendMessage_Com3_State(1);
		g_i_OpenGPS_YuLong = 0;
	}
#endif


	return hCOM;
}

#define		LENGTH_GPS_READ_BUFFER			1024
static char GPS_Read_Buffer[LENGTH_GPS_READ_BUFFER+1];
HANDLE	GPS_Thread = 0;

// 读GPS数据之前
short	g_i_ReadingGPSData = 0;
// 读取一行数据
// bBuff的长度要大于等于lLen+1
long	cnv_GPS_Read_Line(HANDLE hCOM,char* bBuff,long lLen)
{
	char	strTmp[2];
	long	lRetCount = 0;
	long	lNumOfTotal = 0;
	static	short	s_i_FailedCount = 0;
	memset(strTmp,0,2);
	if(hCOM == INVALID_HANDLE_VALUE)	return	-1;
	while(1)
	{
		ReadFile( hCOM,strTmp,1,&lRetCount,NULL);
		if(lRetCount <= 0)	// 设备已经关闭或者没有收到数据
		{
			if(s_i_FailedCount >= 1)	// 等待1秒后还没有读到数据,则认为设备出现问题
			{
				s_i_FailedCount = 0;
				return	-1;
			}
			s_i_FailedCount++;
			CXSYS_Sleep(1000);
			continue;
		}
		g_i_ReadingGPSData = 0;
		bBuff[lNumOfTotal] = strTmp[0];
		lNumOfTotal++;
		if(lNumOfTotal >= lLen)	// 缓冲区满
			break;
		if(strTmp[0] == '\n')	// 读到换行符
		{
			break;
		}
	}
	bBuff[lNumOfTotal] = 0;
	return	lNumOfTotal;
}

// 20060526 DMedia要求使用中断方式访问他们的GPS端口,否则会丢失数据
#if (defined NAVI_VERSION_DMEDIA) || (defined NAVI_VERSION_INTERMIT_GPSPORT)
//#ifdef		NAVI_VERSION_DMEDIA	
short	DMedia_GPS_FindNewLine(char *pstrLine)
{
	char*	p = pstrLine;
	while(*p != 0)
	{
		if(*p == '\n')
			return	1;
		p++;
	}
	return	0;
}

long	cnv_GPS_Read_LineEx(HANDLE hCOM,char* bBuff,long lLen)
{
	ULONG	bytesRead;
	ULONG	ulTotalLen = 0;
	char	strBuffTmp[LENGTH_GPS_READ_BUFFER - 2];
	DWORD	EventMask = EV_RXCHAR;
	char	*pstrTmp = bBuff;
	memset(bBuff,0,lLen);
	// Setting the CommMask to listen for this event (4a)
	SetCommMask(hCOM, EV_RXCHAR);

	// Wait on the event
	while(WaitCommEvent(hCOM, &EventMask, NULL))
	{
		// Read from (4b)
		if(ReadFile(hCOM,strBuffTmp,LENGTH_GPS_READ_BUFFER - 2, &bytesRead,NULL) == 0)
		{
			continue;
		}
		if(bytesRead == 0)
			continue;

		// 是否超过缓冲区长度
		if(ulTotalLen + bytesRead >= (unsigned long) (lLen - 1))
		{
			memcpy(pstrTmp,strBuffTmp,lLen - ulTotalLen - 1);
			ulTotalLen = lLen - 1;
			break;
		}
		memcpy(pstrTmp,strBuffTmp,bytesRead);
		pstrTmp += bytesRead;
		ulTotalLen += bytesRead;
		// 查找看是否有换行
		if(!DMedia_GPS_FindNewLine(bBuff))
		{
			continue;
		}
		g_i_ReadingGPSData = 0;
		break;
	} // end of WaitCommEvent

	return	(long)ulTotalLen;
}

/*============================================
功能:
	从Buff中检索一行数据
参数:
	pstrBuffer:
		[in]输入的字符串数据
	pstrLine:
		[out]检索出来的一行数据
	iLineLen:
		[in]pstrLine的内存长度
	piOutLineLen:
		[out]输出的行字符串长度
返回值:
	返回剩下的字符串
============================================*/
char*	GetLineFromBuffer(char* pstrBuffer,char* pstrLine,short iLineLen,short *piOutLineLen)
{
	short	iCount = 0;
	char*	pstr = pstrBuffer;
	*piOutLineLen = 0;
	while(*pstr != 0)
	{
		pstrLine[iCount] = *pstr;
		if(*pstr == '\n')	// 读到换行符
		{
			*piOutLineLen = iCount;
			pstrLine[iCount + 1] = 0;
			pstr++;
			return	pstr;
		}
		iCount++;
		if(iCount >= iLineLen)	// 行缓冲区满
		{
			pstrLine[iLineLen - 1] = 0;
			*piOutLineLen = iLineLen - 1;
			break;
		}
		pstr++;
	}
	return	pstr;
}

// 一行GPS数据的最大长度
#define		MAX_LENGTHOFLINE		256
#endif
extern long GYR_OpenLogFile();

//2007-01-22 加入 g_iGPS_AlertInorge ls

//g_iGPS_AlertType == 1 使用 NDControl 检测, g_iGPS_AlertType == 0 使用 cnv_timer 检测
extern  short   g_iGPS_AlertType;

DWORD  WINAPI GPS_Sample_Thread_Func_LGM(void *in_Param)
{
	int	lLen ;
	short	iStatus = 1;
  
//#ifdef		NAVI_VERSION_DMEDIA
#if (defined NAVI_VERSION_DMEDIA) || (defined NAVI_VERSION_INTERMIT_GPSPORT)
	char	strLine[MAX_LENGTHOFLINE];
	short	iLineLen ;
	char*	pLineBuff;

⌨️ 快捷键说明

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