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

📄 gprs_handle.c

📁 uccos2的的源码文件
💻 C
字号:
/****************************************Copyright (c)**************************************************
********************************************************************************************************/
#define GPRS_GLOBALS

#include "config.H"

char AT_COMMAND[]=GPRS_AT_CMD;

OS_STK GPRS_task_stack[GPRS_TASK_STACK_SIZE];

extern void	GEC_GPRS_Send_String(char * strptr);

void	GEC_GPRS_HANDLE(void *pdata);
char * startchar(char * str,char endch );
uint16 StrTOUINT(char * str,uint8 DBits);
uint8	GEC_GPRS_SND_CMD(char * cmdptr);

/*********************************************************************************************************
** Function name:			GEC_GPRS_Initial
**
** Descriptions:			Initial GPRS
********************************************************************************************************/
uint8	GEC_GPRS_Initial(void)
{
	GPRS_TCB.GPRSCmdOK	=	OSSemCreate(0);//creat the command ok signal;
	GPRS_TCB.GPRSRing	=	OSSemCreate(0);
	GPRS_TCB.GPRSMsgWR	=	OSSemCreate(0);
	
	GPRS_TCB.Status = 1;//idle
	GPRS_TCB.SNDCmdLen = 0;
	GPRS_TCB.RCVCmdLen = 0;
	GPRS_TCB.GPRS_RCV_WR_PTR = 0;
	//GPRS_TCB.GPRS_SND_RD_PTR = 0;
	
	UART1_Ini(9600);//initial com1 port
	
	if( GEC_GPRS_SND_CMD("AT\r\n") )
	{return 1;}
	if( GEC_GPRS_SND_CMD("AT$NOSLEEP=1\r\n") )
	{return 1;}
	if( GEC_GPRS_SND_CMD("AT+CREG=1\r\n") )//reg
	{return 1;}
	
	/*OSTaskCreateExt(GEC_GPRS_HANDLE,
                (void *)0,
                &GPRS_task_stack[GPRS_TASK_STACK_SIZE-1],
                GPRS_TASK_PRIO,
                GPRS_TASK_ID,
                &GPRS_task_stack[0],
                GPRS_TASK_STACK_SIZE,
                (void *)0,
                OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);	*/
	//creat the GEC_GPRS_Handle task
	return 0;
}
/*********************************************************************************************************
** Function name:			GEC_GPRS_Handle
**
** Descriptions:			GPRS handle task
********************************************************************************************************/
void	GEC_GPRS_HANDLE(void *pdata)
{	
	GPRSAutoTakeTheCall();//RING
	//GOT MESSAGE
}

const char GPRS_CALL_STR[]={"ATD%s;\r\n"};
void	GEC_GPRS_CALL(char * number)
{
	char sendchar[30];
	_sprintf(sendchar,GPRS_CALL_STR,number);
	GEC_GPRS_SND_CMD(sendchar);
}

void	GEC_GPRS_PICKUP()
{
	GEC_GPRS_SND_CMD("ATA\r\n");
}

const char GPRS_MSG_STR[]={"AT+CMGS=\"%s\"\r\n"};
uint8	GEC_GPRS_SND_MSG(char * number,char * SNDMSG)
{
	char sendchar[30];
	char temp[2];
	uint8 err;
	
	OS_ENTER_CRITICAL();
	GPRS_TCB.Status = GPRS_TCB.Status | 0X0020;//set send message
	OS_EXIT_CRITICAL();
	
	GEC_GPRS_SND_CMD("AT+CMGF=1\r\n");
	
	OS_ENTER_CRITICAL();
	GPRS_TCB.Status = GPRS_TCB.Status & 0xfffe;//clear idle bit
	GPRS_TCB.Status = GPRS_TCB.Status | 0x0004;//set have been sended and wait ok
	OS_EXIT_CRITICAL();	
	_sprintf(sendchar,GPRS_MSG_STR,number);
	GEC_GPRS_Send_String(sendchar);//send msg,
	do
	{
		OSSemPend(GPRS_TCB.GPRSMsgWR,2*OS_TICKS_PER_SEC,&err);//wait for >;
		if(err == OS_TIMEOUT)
		{
			continue;
		}
		break;
	}
	while(1);
	
	GEC_GPRS_Send_String(SNDMSG);//send MSG,
	temp[0] = 0x1a;
	temp[1] = 0;
	GEC_GPRS_Send_String(temp);//send ctrl + z
	
	OS_ENTER_CRITICAL();
	GPRS_TCB.Status = GPRS_TCB.Status | 0x0001;	//set idle bit
	GPRS_TCB.Status = GPRS_TCB.Status & 0xffd9;	//clear have been sended and wait ok bit										//clera sending command bit
	OS_EXIT_CRITICAL();
	return 0;
}

const char GPRS_RCVMSG_STR[]={"AT+CMGR=%d\r\n"};
uint8	GEC_GPRS_RCV_MSG(uint8 num,char * number,char * RCVMSG)
{
	char sendchar[15];
	//char temp[2];
	GEC_GPRS_SND_CMD("AT+CMGF=1\r\n");
	_sprintf(sendchar,GPRS_RCVMSG_STR,num);
	GEC_GPRS_SND_CMD(sendchar);
	GPRS_TCB.Status = GPRS_TCB.Status | 0x0010;	//set GOT MSG bit
	//read msg
	return 0;
}

const char GPRS_SETMSGCRT_STR[]={"AT+CSCA=\"%s\"\r\n"};
uint8 GPRSSetMSGCenter(char * number)
{	
	char sendchar[30];
	_sprintf(sendchar,GPRS_SETMSGCRT_STR,number);
	GEC_GPRS_SND_CMD(sendchar);
	return 0;
}
/*********************************************************************************************************
** 函数名称: GPRSTakeTheCall
** 功能描述: 接电话
** 输 入: 无
********************************************************************************************************/
uint8 GPRSTakeTheCall()
{	
	GEC_GPRS_SND_CMD("ATA\r\n");
	return 0;
}
/*********************************************************************************************************
** 函数名称: GPRSHangUpTheCall
** 功能描述: 挂电话
** 输 入: 无
**
** 输 出: 成功 0 失败1
********************************************************************************************************/
uint8 GPRSHangUpTheCall()
{	
	GEC_GPRS_SND_CMD("AT+CHUP\r\n");
	return 0;
}
/*********************************************************************************************************
** 函数名称: GPRSDelMSG
** 功能描述: 删除短信
** 输 入: num 短信的存储号(1~20)
********************************************************************************************************/
const char GPRS_DELMSG_STR[]={"AT+CMGD=%d\r\n"};
uint8 GPRSDelMSG(uint8 num)
{	
	char sendchar[15];
	
	_sprintf(sendchar,GPRS_DELMSG_STR,num);
	GEC_GPRS_SND_CMD(sendchar);
	return 0;
}
/*********************************************************************************************************
** 函数名称: GPRSAutoTakeTheCall
** 功能描述: 接电话
********************************************************************************************************/
uint8 GPRSAutoTakeTheCall()
{	
	uint8 err;
	OSSemPend(GPRS_TCB.GPRSRing, OS_TICKS_PER_SEC*2, &err);
	if(err == OS_NO_ERR)
	{
		GEC_GPRS_SND_CMD("ATA\r\n");
		return 0;
	}
	else
		return 1;
}
/*********************************************************************************************************
** Function name:			GEC_GPRS_RVC_HANDLE
** Descriptions:			Revice data and save into GPRSDataQ
********************************************************************************************************/
void GEC_GPRS_RVC_HANDLE(uint8 i)
{
	if( GPRS_TCB.Status & 0x0010 )
	{
		if( ( i == '>' ) || ( i == 'O' ) || (GPRS_TCB.GPRS_MSG_WR_PTR > 0) )
		{
			GPRS_TCB.RCVMSG[GPRS_TCB.GPRS_MSG_WR_PTR] = i;
			if(i == '\n')
			{
				GPRS_TCB.RCVMSG[GPRS_TCB.GPRS_MSG_WR_PTR+1] = 0;
				if( (GPRS_TCB.Status & 0XFFFd) )//if SENDING COMMAND BIT
				if(strncmp(GPRS_TCB.RCVMSG,"OK\r\n",strlen("OK\r\n")) == 0)
				{
					GPRS_TCB.Status = GPRS_TCB.Status & 0XFFF8;//CLEAR COMMAND BIT
					OSSemPost(GPRS_TCB.GPRSCmdOK);//send call signal; 
				}	
				GPRS_TCB.GPRS_MSG_WR_PTR = 0;
			}
			else if(i == '>')
			{
				if(GPRS_TCB.Status & 0X0020)
				{
					GPRS_TCB.Status = GPRS_TCB.Status & 0XFFD9;//CLEAR COMMAND BIT AND MESSAGE BIT
					OSSemPost(GPRS_TCB.GPRSMsgWR);//send call signal; 
					GPRS_TCB.GPRS_MSG_WR_PTR = 0;
				}
			}
			else
			{
				GPRS_TCB.GPRS_MSG_WR_PTR ++;
				if(GPRS_TCB.GPRS_MSG_WR_PTR >= GPRS_CMDMaxSize+100)
				{
					GPRS_TCB.GPRS_MSG_WR_PTR = 0;
				}
			}
		}
		else if(GPRS_TCB.GPRS_MSG_WR_PTR == 0)
		{
			GPRS_TCB.RCVMSG[GPRS_TCB.GPRS_MSG_WR_PTR] = i;
			GPRS_TCB.GPRS_MSG_WR_PTR ++;
		}
		
	}
	else if( (GPRS_TCB.Status & 0x0001) == 0 )//BUZY
	{
		if( ( i == '>' ) || ( i == 'O' ) || (GPRS_TCB.GPRS_RCV_WR_PTR > 0) )
		{
			GPRS_TCB.RCVCommand[GPRS_TCB.GPRS_RCV_WR_PTR] = i;
			if(i == '\n')
			{
				GPRS_TCB.RCVCommand[GPRS_TCB.GPRS_RCV_WR_PTR+1] = 0;
				if( (GPRS_TCB.Status & 0XFFFd) )//if SENDING COMMAND BIT
				if(strncmp(GPRS_TCB.RCVCommand,"OK\r\n",strlen("OK\r\n")) == 0)
				{
					GPRS_TCB.Status = GPRS_TCB.Status & 0XFFF8;//CLEAR COMMAND BIT
					OSSemPost(GPRS_TCB.GPRSCmdOK);//send call signal; 
				}	
				GPRS_TCB.GPRS_RCV_WR_PTR = 0;
			}
			else if(i == '>')
			{
				if(GPRS_TCB.Status & 0X0020)
				{
					GPRS_TCB.Status = GPRS_TCB.Status & 0XFFD9;//CLEAR COMMAND BIT AND MESSAGE BIT
					OSSemPost(GPRS_TCB.GPRSMsgWR);//send call signal; 
					GPRS_TCB.GPRS_RCV_WR_PTR = 0;
				}
			}
			else
			{
				GPRS_TCB.GPRS_RCV_WR_PTR ++;
				if(GPRS_TCB.GPRS_RCV_WR_PTR >= GPRS_CMDMaxSize+100)
				{
					GPRS_TCB.GPRS_RCV_WR_PTR = 0;
				}
			}
		}
		else if(GPRS_TCB.GPRS_RCV_WR_PTR == 0)
		{
			GPRS_TCB.RCVCommand[GPRS_TCB.GPRS_RCV_WR_PTR] = i;
			GPRS_TCB.GPRS_RCV_WR_PTR ++;
		}
		
	}
	else//idle , for ring
	{
		if( (i == 'r') || ( i == 'R' ) || (GPRS_TCB.GPRS_RCV_WR_PTR > 0) )
		{
			GPRS_TCB.RCVCommand[GPRS_TCB.GPRS_RCV_WR_PTR] = i;
			if(i == '\n')
			{
				GPRS_TCB.RCVCommand[GPRS_TCB.GPRS_RCV_WR_PTR+1] = 0;
				if(strncmp(GPRS_TCB.RCVCommand,"RING\r\n",strlen("RING\r\n")) == 0)
				{
					GPRS_TCB.Status = GPRS_TCB.Status | 0X0008;//SET RING BIT
					OSSemPost(GPRS_TCB.GPRSRing);//send call signal; 
				}	
				GPRS_TCB.GPRS_RCV_WR_PTR = 0;
			}
			else
			{
				GPRS_TCB.GPRS_RCV_WR_PTR ++;
				if(GPRS_TCB.GPRS_RCV_WR_PTR >= GPRS_CMDMaxSize+100)
				{
					GPRS_TCB.GPRS_RCV_WR_PTR = 0;
				}
			}
		}
	}
}
/*********************************************************************************************************
** Function name:			startchar
**
** Descriptions:			check the string from head to end
********************************************************************************************************/
char * startchar(char * str,char endch )
{
	uint16 i=0;
	char * Ptr;
	Ptr=str;
	while(str[i])
	{
		if(str[i]==endch)
		{
			str[i]=0;
			return Ptr;
		}
		i++;
		if(i>30)
			break;
	}
	return NULL;
} 
/*********************************************************************************************************
** Function name:			StrTOUINT
**
** Descriptions:			change string to 16 bits integer
********************************************************************************************************/
uint16 StrTOUINT(char * str,uint8 DBits)
{
	uint8	i=0;
	uint16	j=0;
	if( DBits <= 5 )
	{
		do
		{
			if(str[i] != '.')
			{
				j=(str[i]-0x30)+j*10;
				DBits--;
			}
			i++;
			
		}while(DBits > 0);
	}
	return j;
}
uint8	GEC_GPRS_SND_CMD(char * cmdptr)
{
	uint8 err;
	
	OS_ENTER_CRITICAL();
	if( (GPRS_TCB.Status & 0x0001) == 0 )
	{
		OS_EXIT_CRITICAL();//
		return 2;//buzy
	}
	GPRS_TCB.Status = GPRS_TCB.Status & 0xfffe;//clear idle bit
	GPRS_TCB.Status = GPRS_TCB.Status | 0x0004;//set have been sended and wait ok
	OS_EXIT_CRITICAL();
	do
	{
		GEC_GPRS_Send_String(AT_COMMAND);//send at,
		OSSemPend(GPRS_TCB.GPRSCmdOK,2*OS_TICKS_PER_SEC,&err);//wait for ok;
		if(err == OS_TIMEOUT)
		{
			
			continue;//error
		}
		break;
	}while(1);
	OS_ENTER_CRITICAL();
	GPRS_TCB.Status = GPRS_TCB.Status & 0xfff9;	//clear have been sended and wait ok bit										//clera sending command bit
	GPRS_TCB.Status = GPRS_TCB.Status | 0x0004; //set have been sended and wait ok
	OS_EXIT_CRITICAL();
	do
	{
		GEC_GPRS_Send_String(cmdptr);//send command,
		OSSemPend(GPRS_TCB.GPRSCmdOK,2*OS_TICKS_PER_SEC,&err);//wait for ok;
		if(err == OS_TIMEOUT)
		{
			
			continue;//error
		}
		break;
	}while(1);
	OS_ENTER_CRITICAL();
	GPRS_TCB.Status = GPRS_TCB.Status | 0x0001;	//set idle bit
	GPRS_TCB.Status = GPRS_TCB.Status & 0xfff9;	//clear have been sended and wait ok bit										//clera sending command bit
	OS_EXIT_CRITICAL();
	return 0;//success
}

⌨️ 快捷键说明

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