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

📄 main.bak

📁 TC35i收发短信的源代码
💻 BAK
📖 第 1 页 / 共 5 页
字号:
	//respne info
	RESP_BUF[0]='O';
	RESP_BUF[1]='K';
	RESP_BUF[2]=0;
	RESP_LEN = 2;

	//send cmd to moderm
	DelayCnt = 2; //delay 2s

	ucResult = send_cmd(u6.Buf,13,IGNORE);

	return (ucResult);
}

/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for setting DTU web information.
**
**  Parameters:
**	None.
**	
**  Returns:
**	None.
**	
**-----------------------------------------------------------------------------
*/
uchar sim_at_csca( void )
{
	uchar ucResult;

	if(gSMSCNUM_LEN<1) return FALSE;
	
	//AT+CPBS="SM"
	memcpy(u6.Buf,"AT+CSCA=",8);
	u6.Buf[8]='"';
	memcpy(u6.Buf+9,gSMSCNUM,gSMSCNUM_LEN);
	u6.Buf[9+gSMSCNUM_LEN]='"';
	u6.Buf[9+gSMSCNUM_LEN+1]=0x0D;
	u6.Buf[9+gSMSCNUM_LEN+2]=0;

	ucResult = send_at_cmd( u6.Buf );

	return (ucResult);
}
/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for setting DTU web information.
**
**  Parameters:
**	None.
**	
**  Returns:
**	None.
**	
**-----------------------------------------------------------------------------
*/
uchar sim_at_cpbr( uchar idx )
{
	uchar ucResult;
	
	if(idx < 1 || idx > 9) return FALSE;

	//AT+CPBS="SM"
	memcpy(u6.Buf,"AT+CPBR=",8);

	u6.Buf[8]=idx + '0'; //phone index
	u6.Buf[9]=0x0D;
	u6.Buf[10]=0;

	RESP_BUF[0]='O';
	RESP_BUF[1]='K';
	RESP_BUF[2]=0;
	RESP_LEN = 2;

	//send cmd to moderm
	DelayCnt = 4; //delay 2s

	ucResult = send_cmd(u6.Buf,10,HOLD);

	return (ucResult);
}

/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for setting DTU web information.
**
**  Parameters:
**	None.
**	
**  Returns:
**	None.
**	
**-----------------------------------------------------------------------------
*/
uchar GetSimInfo( void )
{
	uchar i;
	uchar baud,parity;
	uchar ucResult;
	int len;
	int pos1,pos2,pos3,pos4;

	if(send_at_cmd("AT+CMGF=1\r")!=TRUE) return FALSE;
	if(SetCharSet("GSM")!=TRUE) return FALSE;

	memset(gPHONENUM,0,PHONE_LEN);
	memset(gSMSCNUM,0,PHONE_LEN);

	//AT+CPBS="SM"
	for(i=0;i<20;i++)
	{
		ucResult = sim_at_cpbs();
		if(ucResult==TRUE) break;
		delay_s(1);
	}
	if(ucResult!=TRUE) return FALSE;

	//read postion 1 data
	for(i=0;i<20;i++)
	{
		ucResult = sim_at_cpbr( 1 );
		if(ucResult==TRUE) break;
		delay_s(1);
	}

	if(ucResult!=TRUE) return FALSE;

	//redraw the phone & parameter
	if(IN_LEN < 7) return FALSE;

	u6.Buf[IN_LEN]=0; //finish flag

	//search first '"'
	pos1 = strpos(u6.Buf,1,'"');

	if(pos1>0)
	{
		//search 2nd '"'
		pos2 = strpos(u6.Buf,pos1+1,'"');
		pos3 = strpos(u6.Buf,pos2+1,'"');
		pos4 = strpos(u6.Buf,pos3+1,'"');

		//get sms center number.
		gSMSCNUM_LEN = (uchar)(pos2-pos1-1);
		if(gSMSCNUM_LEN>0)
		{
			gSMSCNUM[0]='+';
			gSMSCNUM[1]='8';
			gSMSCNUM[2]='6';
			memcpy(gSMSCNUM+3,u6.Buf+pos1+1,gSMSCNUM_LEN);
			gSMSCNUM_LEN += 3;
			gSMSCNUM[gSMSCNUM_LEN]=0; //finish flag
		}

		//get parameter.
		len = pos4-pos3-1;
		if(len != SM_PARA_LEN) return FALSE;

		memcpy(tempBuf,u6.Buf+pos3+1,SM_PARA_LEN);
		tempBuf[SM_PARA_LEN]=0;
	}
	else
	{
		SendRetInfo("\r\n>error1!");
		return FALSE;
	}

//SendRetInfo("03");

	//read postion 2 data
	for(i=0;i<20;i++)
	{
		ucResult = sim_at_cpbr( 2 );
		if(ucResult==TRUE) break;
		delay_s(1);
	}

//SendRetInfo("04");

	if(ucResult!=TRUE) return FALSE;

//SendRetInfo("05");

	//redraw the phone & parameter
	if(IN_LEN < 7) return FALSE;

	u6.Buf[IN_LEN]=0; //finish flag

	pos1 = strpos(u6.Buf,1,'"');

	if(pos1>0)
	{
		//search 2nd '"'
		pos2 = strpos(u6.Buf,pos1+1,'"');

		//get sms center number.
		gPHONENUM_LEN = (uchar)(pos2-pos1-1);
		if(gPHONENUM_LEN>0)
		{
			memcpy(gPHONENUM,u6.Buf+pos1+1,gPHONENUM_LEN);
		}
//----------------------------------------------------------
/*
		memcpy(gPHONENUM,"13922834904",11);
		gPHONENUM[11]=0;
		gPHONENUM_LEN = 11;
*/
//-------------------------------------------------		
	}
	else
	{
		return FALSE;
	}

	//reconfig uart6 & uart0
	if(memcmp(tempBuf,"1200",4)==0)
	{
		baud = _1200BPS;
	}
	else if(memcmp(tempBuf,"2400",4)==0)
	{
		baud = _2400BPS;
	}
	else if(memcmp(tempBuf,"4800",4)==0)
	{
		baud = _4800BPS;
	}
	else if(memcmp(tempBuf,"9600",4)==0)
	{
		baud = _9600BPS;
	}
	else if(memcmp(tempBuf,"1920",4)==0)
	{
		baud = _1920BPS;
	}
	else if(memcmp(tempBuf,"3840",4)==0)
	{
		baud = _3840BPS;
	}
	else if(memcmp(tempBuf,"1152",4)==0)
	{
		baud = _1152BPS;
	}
	else return FALSE;

	if(tempBuf[4]=='N') parity = _NULL; //no parity
	else if(tempBuf[4]=='E') parity = _EVEN; //even parity
	else if(tempBuf[4]=='O') parity = _ODD; //odd parity
	else return FALSE;

	//config uart6 & uart0
//	UART0_Init( cate, parity );
//	UART6_Init( cate, parity );

	//sms transfer protocol
//	if(tempBuf[9]=='P') gPROTOCOL = _PDU;
//	else gPROTOCOL = _TEXT;

	//watch dog time setting
//	tempBuf[9]=0; //replace 'P' or 'T'
	tempLong = atol(tempBuf+5);
	if(tempLong < 0) return FALSE;

	//change min to sec.
	gWTDTIME = tempLong * 60 * 10;

	//update sms center number.
	//AT+CSCA="+8613800755500"
	ucResult = sim_at_csca();
	if(ucResult!=TRUE) return FALSE;

	//config uart6 & uart0
	if(set_baud(baud,parity)==FALSE) return FALSE;

	return TRUE;
}

/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for UART6 data transfering.
**
**  Parameters:
**	txbuf:	transfer buffer pointer
**	txnum:	buffer size
**
**  Returns:
**	MD_OK
**	MD_ERROR
**
**-----------------------------------------------------------------------------
*/
void wtd_listen( void )
{
	uchar ucResult;

	//it's time to reconnect
	if(gWTDTIME>0)
	{
	//	SendWtdReset();
	}

	//start timer50
	tm50cnt=0;
	tm50Flag=FALSE;
	TM50_Start();
}

/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for reading out the specified msg.
**	
**  Parameters:
**		None
**	
**
**  Returns:
**		TRUE--execute OK
**      FALSE--execute fail
**-----------------------------------------------------------------------------
*/
uchar sms_at_cmgr( uchar idx )
{
	uchar ucResult;
	int len;

	if(idx < 1) return FALSE;

	ltoa (idx, tempBuf+16 );
	len = strlen(tempBuf+16);
	
	//example: AT+CMGD
	memcpy(tempBuf,"AT+CMGR=",8);
	memcpy(tempBuf+8,tempBuf+16,len);
	tempBuf[8+len] = 0x0D;
	tempBuf[8+len+1] = 0;

	//respone data
	RESP_BUF[0]='O';
	RESP_BUF[1]='K';
	RESP_BUF[2]=0;

	RESP_LEN=2;

	DelayCnt = 2; //delay 2s

	//send at cmd to moderm.
	ucResult = send_cmd( tempBuf,8+len+1,HOLD);

	return (ucResult);
}


/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for deleting the specified msg.
**	
**  Parameters:
**		None
**	
**
**  Returns:
**		TRUE--execute OK
**      FALSE--execute fail
**-----------------------------------------------------------------------------
*/
uchar sms_at_cmgd( uchar idx )
{
	uchar ucResult;
	int len;

	if(idx < 1) return FALSE;

	ltoa (idx, tempBuf+16 );
	len = strlen(tempBuf+16);
	
	//example: AT+CMGD
	memcpy(tempBuf,"AT+CMGD=",8);
	memcpy(tempBuf+8,tempBuf+16,len);
	tempBuf[8+len] = 0x0D;
	tempBuf[8+len+1] = 0;

	ucResult = send_at_cmd( tempBuf );

	return (ucResult);
}

/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for sending : AT+CMGS="13798231117".
**	                        respone: "OK"
**  Parameters:
**		None
**	
**
**  Returns:
**		TRUE--execute OK
**      FALSE--execute fail
**-----------------------------------------------------------------------------
*/
uchar sms_at_cmgs( uchar cate,uchar *para )
{
	uchar ucResult;
	int len;

	len = strlen(para);

	if(len<1) return FALSE;

	//comm prefix
	memcpy(tempBuf,"AT+CMGS=",8);

	if(cate==_PDU)
	{
		memcpy(tempBuf+8,para,len);
		tempBuf[8+len]=0x0D;
		tempBuf[8+len+1]=0;
		len = 8+len+1;
	}
	else
	{
		if(len>PHONE_LEN) return FALSE;
	
		//example: AT+CMGS="13798231117"
		tempBuf[8] = '"';
		memcpy(tempBuf+9,para,len);
		tempBuf[9+len]='"';
		tempBuf[9+len+1]=0x0D;
		tempBuf[9+len+2]=0;
		len = 9+len+2;
	}

	//respone data
	RESP_BUF[0]=0x3E;	//'>';
	RESP_BUF[1]=0x20;
	RESP_BUF[2]=0; 	//space
	RESP_LEN=1;

	DelayCnt = 2; //delay 2s

	//send at cmd to moderm.
	ucResult = send_cmd( tempBuf,len,IGNORE);

	NOP();
	NOP();

	//execute error
	return (ucResult);
}

/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for set charset which using.
**	                        respone: "OK"
**  Parameters:
**		None
**	
**
**  Returns:
**		TRUE--execute OK
**      FALSE--execute fail
**-----------------------------------------------------------------------------
*/
uchar SetCharSet(char *charset)
{
	uchar ucResult;
	int len;
	
	len = strlen(charset);

	tempBuf[0]='A';
	tempBuf[1]='T';
	tempBuf[2]='+';
	tempBuf[3]='C';
	tempBuf[4]='S';
	tempBuf[5]='C';
	tempBuf[6]='S';
	tempBuf[7]='=';
	tempBuf[8]='"';
	memcpy(tempBuf+9,charset,len);
	tempBuf[9+len]='"';
	tempBuf[9+len+1]=0x0D;
	tempBuf[9+len+2]=0;

	ucResult = send_at_cmd(tempBuf);

	return (ucResult);
}

/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for sending Msg out.
**	
**  Parameters:
**		None
**	
**
**  Returns:
**		None
**
**-----------------------------------------------------------------------------
*/
uchar SendOneMsg( uchar *DataBuf,int DataLen )
{
	uchar ucResult;
	uchar ctrl_z,tmp;
	uchar para[4];

	//send chinese msg.
	if(gPROTOCOL==_PDU){

		if(send_at_cmd("AT+CMGF=0\r")!=TRUE) return FALSE; //pdu mode

		tmp = (uchar)(DataLen/2);
		TPDU[TPDU_LEN-2]=H2A(tmp/16);
		TPDU[TPDU_LEN-1]=H2A(tmp%16);
		TPDU[TPDU_LEN]=0;

		tmp += 15;
		ltoa (tmp, para );

		ucResult = sms_at_cmgs(_PDU,para ); //send: at+cmgs=140
		if(ucResult!=TRUE) return FALSE;

		//send out TPDU.
		UART0_SendData(TPDU,TPDU_LEN);
		u0WaitSndOver();

	}
	else if(gPROTOCOL==_TEXT){
		
		ucResult = sms_at_cmgs(_TEXT,gPHONENUM ); //send: at+cmgs="13798231117"

		//error occurs
		if(ucResult!=TRUE) return FALSE;
	}

	//-------send data to uart0----------------

	//send data to uart0(modem)
	UART0_SendData(DataBuf,DataLen);
	u0WaitSndOver();

	//respone data
/*
	RESP_BUF[0]='O';
	RESP_BUF[1]='K';
	RESP_BUF[2]=0;
	RESP_LEN=2;
*/

	memcpy(RESP_BUF,"+CMGS:",6);
/*
	RESP_BUF[0]='+';
	RESP_BUF[1]='C';
	RESP_BUF[2]='M';
	RESP_BUF[3]='G';
	RESP_BUF[4]='S';
	RESP_BUF[5]=':';
*/
	RESP_BUF[6]=0;
	RESP_LEN=6;

	ctrl_z = 0x1A; //ctrl+z

	DelayCnt = 10; //delay 10s

	//send to moderm
	ucResult = send_cmd( &ctrl_z, 1, IGNORE );

	//execute ok
	return (ucResult);
}

/*
**-----------------------------------------------------------------------------
**
**  Abstract:
**	This function is responsible for sending Msg out.
**	
**  Parameters:
**		None
**	
**
**  Returns:
**		None
**
**-----------------------------------------------------------------------------
*/
uchar SendMsg( void )
{
	uchar ucResult,err;
	uchar i,times;
	int modlen;
	int len;

	times = (uchar)(u6.Length/gMAX_RCV_LEN);
	modlen = (uchar)(u6.Length%gMAX_RCV_LEN);

	if(modlen>0) times++;
	
	for(i=0;i<times;i++)
	{
		if(i+1==times){
			if(modlen>0) len = modlen;
			else len = gMAX_RCV_LEN;
		}
		else len = gMAX_RCV_LEN;

		err = 3;
		while(err > 0)
		{
			err--;

			//send one msg.
			ucResult = SendOneMsg(u6.Buf+u6.SndPtr,len);

			//if error occurs then return
			if(ucResult==TRUE) break;

⌨️ 快捷键说明

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