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

📄 gpslib.c

📁 GPS的一个任务
💻 C
📖 第 1 页 / 共 2 页
字号:
	/*DEBUG_PRINT(("时间串=%s\n\r",temp));*/	
	ascii_2_hex(temp,htemp,12);
	GpsMsg[28] = 0x05;
	GpsMsg[29] = 0x10;
	GpsMsg[30] = 0x06;
	GpsMsg[31] = 0x00;
	GpsMsg[32] = htemp[0];
	GpsMsg[33] = htemp[1];
	GpsMsg[34] = htemp[2];
	GpsMsg[35] = htemp[3];
	GpsMsg[36] = htemp[4];
	GpsMsg[37] = htemp[5];
	/*里程字*/
	memset(htemp,0,20);
	memset(temp,0,20);	
	strncat(temp,&gpsPdu[46],6);
	
	ispeed = atoi(temp);
	GpsMsg[38] = 0x06;
	GpsMsg[39] = 0x10;
	GpsMsg[40] = 0x03;
	GpsMsg[41] = 0x00;	
	GpsMsg[44] = (ispeed & 0xFF0000)>>16;
	GpsMsg[43] = (ispeed & 0x00FF00)>>8;
	GpsMsg[42] = (ispeed & 0x0000FF);	
	ilen = Gpshandle(GpsMsg,SendGpsMsg,45);	
	pCommNode = commNodeInit();
	pCommNode->commandtype = 0;
	memcpy(pCommNode->message, SendGpsMsg,ilen);	
	pCommNode->msgLen = ilen;
	if(isConnectedApp)
		sysQSend(pConnectMsgQueue, SYSSENDMESSAGE, (void *)pCommNode, 0);
	else
	{
		gpsDataSave(pCommNode->message, ilen);
		DEBUG_PRINT(("----模块正在拨号保存一条离线数据到离线数据缓冲----\r\n"));
	}	
	
}


/*
*发送GPS信息到TCP任务
*/
void gpsMsgToTcpTask(char *gpsMsg)
{
	command_t *pCommNode;
	pCommNode = commNodeInit();
	memcpy(pCommNode->message, gpsMsg, strlen(gpsMsg));
	pCommNode->commandtype = 0x33;
	pCommNode->msgLen = strlen(pCommNode->message);

	sysQSend(pConnectMsgQueue, SYSSENDMESSAGE, (void *)pCommNode, 0);
	
	return;    
}
/*
*gpsMsgIsValid - check gps msg valid, is valid return TRUE, else return FALSE
*
*
*/
BOOL gpsMsgIsValid(char *msg)
{
	int index, i;
	UINT8 checkSum=0;
	/*if not begin with $ then return FALSE*/
	if (msg[0]!= '$')
	{
		/*DEBUG_PRINT(("MSG if not begin with $ \n"));*/
		return FALSE;
	}
	index = strlen(msg);
	if (msg[index-5] != '*')
	{
		/*DEBUG_PRINT(("MSG if not end with * \n"));*/
		return FALSE;
	}
	/*calculate check sum of char beteew $ and */
	for (i=1; i<index-5;i++)
		checkSum ^= msg[i];
	/*camp checkSum and hh */
	if (strtol (msg+index-4, NULL, 16) != checkSum)
	{
		DEBUG_PRINT(("CheckSum is 0x%02X, old checkSum is: %c%c\n", checkSum, msg[index-4], msg[index-3]));
		return FALSE;
	}
	return TRUE;
}


void overSpeedHandle(char *gpsMsg, char msgLen)
{
	double currentSpeed;
	static UINT32 overspeedTimes=0;
	char buffer[20];
	char *errorPtr;
	UINT32 maxSpeedTemp;
	UINT32 maxSpeedTimes;

	if (gpsMsg[6]!='A')
	{
		overspeedTimes = 0;
		return;
	}
	memset(buffer, 0, sizeof(buffer));
	strncpy(buffer, &gpsMsg[28], 5);
	currentSpeed = atof(buffer)*1.852;
	maxSpeedTemp = strtol(maxSpeed, &errorPtr, 10);
	maxSpeedTimes = strtol(overSpeedTime, &errorPtr, 10);
	if (maxSpeedTemp < 30) maxSpeedTemp = 120;
	if (maxSpeedTemp > 140) maxSpeedTemp = 140;

	if (currentSpeed > maxSpeedTemp)
	{
		overspeedTimes++;
		if (overspeedTimes>maxSpeedTimes)
		{
			command_t *pCommNode;
			char sendmsg[100];
			memset(sendmsg, 0, sizeof(sendmsg));
			sprintf(sendmsg, "超速行驶,当前速度:%d 公里", (int )currentSpeed);
			pCommNode = commNodeInit();
			if (overSpeedValueIsHex)
			{
				sprintf(pCommNode->message, "1_%03d", (int)currentSpeed);
			}
			else
			{
				sprintf(pCommNode->message, "1_%03X", (int)currentSpeed);
			}
			pCommNode->commandtype = 0x22;
			pCommNode->msgLen = strlen(pCommNode->message);
			sysQSend(pConnectMsgQueue, SYSSENDMESSAGE, (void *)pCommNode, 0);
			lcdmsgsend(0, sendmsg);
			notifySoundPlay();
			overspeedTimes = 0;
		}
	}
	else
		overspeedTimes = 0;


}

/*gpsTask - read gps message and send to 
*          clientMsgQueue */

void gpsTask()
{
	char buffer[240];       /*buffer for read gps message*/
	char paramBuffer[40];   /*buffer for read param */
	command_t *pCommNode;
	long i = 0;
	ULONG gpsUpdateTime=0;
        int sendLcdGpsTime=0; 
	ULONG minSpeedTime = 0;/*记录速度小于2KM/h的开始时间*/
	ULONG runTimes=0;/*记录速度连续大于2KM/h的时间*/

	double speed, angle;
	static int sysTimeUpdate = 0;

	fd_set readfds;
	struct timeval timo;

	int cs;
	ULONG lastUpdateTick=0;
	int positionV = 0;


	gpsstop = 0;

	memset(gpsNumber, 0, sizeof(gpsNumber));
	sprintf(gpsNumber, "00");

	/*init gpsData, if alarm before get gps message then gps message attach to end of alarm msg*/
	memset(gpsData, 0, 100);
	strcpy(gpsData, "061825V2432.0733N10627.2544E000.0040331309.62");
	

	strcat(gpsData, currentMainPowerState?"1":"0");
	strcat(gpsData, currentAccState?"1":"0");
	strcat(gpsData, "000000");


	gpsLibInited = TRUE;

	DEBUG_PRINT(("begin enter gps loop.\n"));

	/*stopZero = 1;*/

	while(TRUE)
	{
		
		/*如果处于省电状态*/
		/*if(isAccOffLongTime)
		{
			if(!gpsstop)
			{
				DEBUG_PRINT(("处于省电模式,关闭GPS电源...\n"));
				
				gpsstop = TRUE;
			}
			taskDelay(180);
			continue;
		}else
		{
			
			gpsstop = FALSE;
				
			
		}*/
		
		memset(buffer, 0, sizeof(buffer));
		FD_ZERO(&readfds);
		FD_SET(gpsFd, &readfds);
		timo.tv_sec = 30;
		timo.tv_usec = 0;
		cs = select(FD_SETSIZE, &readfds, (fd_set *) 0, (fd_set *) 0, &timo);

		/*如果等待GPS数据超时先关GPS模块电源然后重新启动系统*/
		if (cs <= 0)
		{			
			extern BOOL systemOk;
			gpsMileageSave();
			DEBUG_PRINT(("WAIT GPS MESSAGE TIMEOUT!\n"));
			gpsPowerDisable();
			systemOk = FALSE;
			gpsMileageSave();
			taskDelay(10*60);		
			reboot(0);
		}

		read(gpsFd, buffer, sizeof(buffer));

		memset(paramBuffer, 0, sizeof(paramBuffer));
		

		if (strncmp(buffer, GGA_MSG, strlen(GGA_MSG)) == 0)
		{
			if (!gpsMsgIsValid(buffer))
			{
				/*DEBUG_PRINT(("Gps msg is not valid: %s", buffer));*/
				continue;
			}

			if ((getParam(buffer, 7, paramBuffer) == 2) &&(strncmp(paramBuffer, gpsNumber, 2) != 0))
			{
				strncpy(gpsNumber, paramBuffer, 2);	
				/*DEBUG_PRINT(("收到卫星: %s颗\r\n", gpsNumber));*/
				continue;
			}
			else
				continue;

		}
		/*handle Recommended Minimum Specific GPS/TRANSIT Data (RMC)*/
		if (strncmp(buffer, RMC_MSG, strlen(RMC_MSG)) == 0)
		{
			/*printf("rmc_msg: %s\n",buffer);*/

			if (!gpsMsgIsValid(buffer))
			{
				continue;
			}


			if (gpsMsgPackTcp(buffer, gpsData, &angle, &speed, &positionValid) == ERROR)
				continue;
			
			gpsMsgToHandLcd(gpsData);
			
			/*检测是否超速,超速持续时间为30S*/
			if((speed*1.852)>sysInfo.maxLimitSpeed)
			{
				if(tickGet() - overSpeedtime >30*60)
				{
					/*超速报警*/
					App_SendOverSpeed(0);
					overSpeedtime = tickGet();
				}
			}else
			{
				overSpeedtime = tickGet();
			}
			/*检测是否停车超时*/
			if(((speed*1.852)<2) && (sysInfo.App_StopTime >0))
			{
				if((tickGet()-stopTime)>sysInfo.App_StopTime*60*60)
				{
					/*停车超时报警*/
					App_SendOverSpeed(1);
					stopTime = tickGet();
				}	
			}else
			{
				stopTime = tickGet();
			}
			/*如果定位无效,超速和停止时间不计*/
			if(!positionValid)
			{
				overSpeedtime = tickGet();
				stopTime = tickGet();
			}
			/*保存累加里程 2008/5/4 中国移动车管家*/
			if(tickGet() - gpsMileageTime >MILETIME*60*60)
			{
				gpsMileageSave();
				gpsMileageTime = tickGet();
			}
			
			/*速度为0*/
			if(speed < 2.5)
				currentSpeedIsZreo = TRUE;
			else
				currentSpeedIsZreo = FALSE;
			
			/*if first then set system time*/
			if (positionValid && (!sysTimeUpdate))
			{
				if (sysTimeSetByGps(buffer) == OK){
					sysTimeUpdate = 1;
					checkSysTime = 1;
					DEBUG_PRINT(("完成校时!\r\n"));
				}else
					DEBUG_PRINT(("Set system time error.\n"));
			}

			
			/*if(runTimes == 0)
				runTimes = tickGet();
			if(tickGet() - runTimes >= 10*60)
			{
				runFlag = 1;
				minSpeedTime = 0;
				stopZero = 1;
			}*/
			if(sysInfo.intervalTime == 0)
			{	
				sysInfo.intervalTime = 20;
				
			}
			/*当停止EC时,停止上传和记录位置信息*/				
			if(sysInfo.m2m_stopEC == 'Y')
				continue;
			if ((sysInfo.isSendGpsMsg ==0) && positionValid && (!isSpeedZreoLongTime) &&
			   (tickGet() - gpsUpdateTime)>sysInfo.intervalTime*60 && speed <= 150 )
			{
				
				DEBUG_PRINT(("MSG: 有效定位,每隔%d秒发送一次定位信息.\n",sysInfo.intervalTime));
				memset(buffer, '\0', strlen(buffer));
				memcpy(buffer, gpsData, strlen(gpsData));
				memset(paramBuffer, '\0', strlen(paramBuffer));
				sprintf(paramBuffer, "L%06d", gpsMileage);
				/*DEBUG_PRINT(("累加里程=%d\n\r",gpsMileage));*/
				strcat(buffer, paramBuffer);
				/*把GPS信息组织成车管家所需的GPS信息格式*/
				busPduPack(buffer);				
				/*gpsMsgToTcpTask(buffer);*/
				gpsUpdateTime = tickGet();
				areaHandle(gpsData);				
				continue;
			}
		}
	}

	close(gpsFd);
}


/*
*gpsPowerEnable - gps module power on
*
*
*/
void gpsPowerEnable()
{
	/*set GPD1 as output*/
	rPCOND &= ~(3<<2);
	rPCOND |= (1<<2);

	/*set GPD1 as HIGH*/
	rPDATD |= 1 << 1;
}

/*
*gpsPowerDisable - gps module power off
*
*
*/
void gpsPowerDisable()
{
	/*set GPD1 as output*/
	rPCOND &= ~(3<<2);
	rPCOND |= (1<<2);
	/*set GPD1 as LOW*/
	rPDATD &= ~(1 << 1);
}

⌨️ 快捷键说明

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