📄 main.bak
字号:
else SendOK();
}
return;
}
}
}
//return to sms mode.
if(u6.Length==3){
if(u6.Buf[0]=='^'&&u6.Buf[1]=='^'&&u6.Buf[2]=='^'){
ChangeModeToSMS();
SendRetInfo("\r\n>^SMSSTART\r\n");
return;
}
}
//exect at cmd
execute_AtCmd(u6.Buf,(uchar)u6.Length);
}
/**********************************************************************
* Func Name : main
* Descript : main function
* Parameters : None
* Returns : None
* Author : Nakata
* Create Date: 2008/01/22
* Modify Date: 2008/01/22
* **********************************************************************/
void main( void )
{
uchar ErrFlag1,ErrFlag2,ErrFlag3,ErrFlag4;
DI(); /*Disable interrupt*/
NOP();
WDTE = 0xAC; /*clear and start watchdog timer */
CPU_init(); /*initializtion of cpu */
InitUart6Rec(); /* initializtion of reception */
InitFLMD();
UART0_Init( _9600BPS, _NULL ); /*initializtion of UART0. */
UART6_Init( _9600BPS, _NULL ); /*initializtion of UART6. */
TM00_Init(); /*initializtion of TM00 */
TM00_Stop();
TM50_Init();
TM50_Stop();
TM51_Init();
TM51_Stop();
//initilization of ports
PortInit();
P7.1 = 0; //dtr
P7.2 = 0; //power,don't use.
EI(); /*Enable interrupt*/
WDTE=0xAC;
NOP();
NOP();
//change to command mode.
ChangeModeToCMD();
//error flag
ErrFlag1 = FALSE;
ErrFlag2 = FALSE;
ErrFlag3 = FALSE;
ErrFlag4 = FALSE;
delay_s(5);
/*
//first read the serialport parameter.
if(IsBlockValid( PARA_BLOCK_NO )==FALSE)
{
if(set_para_default()!=TRUE)
{
ErrFlag1 = TRUE;
}
}
NOP();
NOP();
//load globa parameter,such as wtd timer.
//this function must place after func "IsBlockValid()". otherwise gWTDTIME,gHBSTR,gHBTIME will be error.
if(ErrFlag1==FALSE)
{
if(load_globa_para()!=TRUE)
{
ErrFlag2 = TRUE;
}
}
NOP();
NOP();
UART0_Init( gBAUD,gPARITY ); //initializtion of UART0.
NOP();
NOP();
UART6_Init( gBAUD,gPARITY ); //initializtion of UART6.
NOP();
NOP();
//if mode is _SMSMODE
if(gMODE==_SMSMODE)
{
if(ErrFlag1==FALSE && ErrFlag2==FALSE)
{
//load SIM card information
if(GetSimInfo()==FALSE) ErrFlag3 = TRUE;
}
if(ErrFlag1==FALSE && ErrFlag2==FALSE && ErrFlag3==FALSE)
{
//init sms parameter.
if(SetSMSPara()==FALSE)
{
ErrFlag3 = TRUE;
}
else DeleteAllMsg(); //delete all msg.
}
}
if(ErrFlag1==TRUE) SendRetInfo("\r\n>Write Flash Error.\r\n");
if(ErrFlag2==TRUE) SendRetInfo("\r\n>Read Flash Error.\r\n");
if(ErrFlag3==TRUE) SendRetInfo("\r\n>Read SIM Card Error,Please Check It.\r\n");
if(ErrFlag4==TRUE) SendRetInfo("\r\n>Parameter Config Error.\r\n");
*/
//timer50/51
tm50cnt=0;
tm51cnt=0;
tm50Flag = FALSE;
tm51Flag = FALSE;
//previous Swtich state
PreSwitch = 0xFF;
if(ErrFlag1==TRUE||ErrFlag2==TRUE||ErrFlag3==TRUE||ErrFlag4==TRUE){
SendRetInfo("\r\n>^CMDSTART\r\n");
ChangeModeToCMD(); //change to command state
}
else{
SendRetInfo("\r\n>^SMSSTART\r\n");
ChangeModeToSMS(); //rest online 's parameters.
}
while(1)
{
WDTE=0xAC;
NOP();
NOP();
//need changing to command state.
//equal to 3 "@@@", no <cr>,<lf>
if(CMD_MODE_FLG==TRUE)
{
u6WaitRcvOver();
ChangeModeToCMD();
SendRetInfo("\r\n>^CMDSTART\r\n");
}
//when CD is high, must reconnect to net.
//if(gMODE==ON_LINE && gSYSTEM==TRUE)
if(gMODE==_SMSMODE)
{
//it's time to send msg or buffer is full.
if(tm51Flag==TRUE||u6.Length>=gMAX_RCV_LEN)
{
if(u6.Length>0)
{
u6WaitRcvOver(); //wait uart6 receive over.
gSTATE = SEND; //change state to "SEND"
if(SendMsg()==FALSE)
{
SendRetInfo("\r\n>Send Msg Error.\r\n");
}
InitUart6Rec();
gSTATE = IDLE; //change state to "IDLE"
}
tm51cnt=0;
tm51Flag = FALSE;
TM51_Start();
}
//when msg arrive.
if(READ_FLG == TRUE && gSTATE == IDLE){
if(u6.Length == 0){
gSTATE = READ; //change state to "READ"
ReadMsg(); //read msg.
gSTATE = IDLE; //change state to "IDLE"
}
}
//listening to wtd
if(tm50Flag==TRUE){
//listening function
wtd_listen();
}
}
if(gMODE==_CMDMODE)
{
//if write phone command arrive.
if(u6.Finish==TRUE)
{
u6WaitRcvOver(); //wait uart6 receive over.
//phrase the command string.
analzye_atcmd();
//initialization of uart6 reception.
InitUart6Rec();
}
}
}
}
/****************************************************************************
**
** Abstract:
** delay for a while.
**
** Parameters:
** None
**
** Returns:
** None
**
****************************************************************************/
void delay( unsigned int n )
{
register unsigned int i;
for(i=0;i<n;i++)
{
NOP();
}
}
/****************************************************************************
**
** Abstract:
** initializtion of CPU.
**
** Parameters:
** None
**
** Returns:
** None
**
****************************************************************************/
void CPU_init(void)
{
IMS = 0xC8; //0511--04H
IXS = 0x0C;
//clock option
MSTOP = 1; //X1 oscillator stop
OSCCTL = 0x00; //IO mode, 1<=Fhx<=10M
PCC = 0x00; //CPU clock--Main clock,Fxp ,Subsyster clock disable
MCM = 0x00;
RCM = 0x00; //internal high-speed oscillator oscillating,low-speed oscillating
while(!RSTS); //wait until internal high-speed oscillator run to stabile
OSTS = 0x01; //X1 oscillator stabilization with 204.8us at 10M
}
/****************************************************************************
**
** Abstract:
** Send at cmds to moderm.
**
** Parameters:
** DataBuf --
**
** Returns:
** None
**
****************************************************************************/
uchar send_cmd( uchar *DataBuf,int DataLen,uchar InFlag )
{
uchar i;
WDTE=0xAC;
//about timer
tCount=0;
tFlag = FALSE;
TM00_Stop();
//respone 1 setting
Rsp1Idx = 0;
// Rsp1TmpFlag = FALSE;
Rsp1MatchFlag = FALSE;
//respone 2 setting
Rsp2Idx = 0;
//s Rsp2TmpFlag = FALSE;
Rsp2MatchFlag = FALSE;
//-------------------
IN_LEN = 0;
gINFLAG = InFlag;
//-------------------
gCMDSTATE = CMD_RUN; //set to send cmd state
//send out
UART0_SendData( DataBuf, DataLen );
u0WaitSndOver();
//start timer
TM00_Start();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
while(tFlag==FALSE)
{
if(Rsp1MatchFlag==TRUE || Rsp2MatchFlag==TRUE)
{
//receive the last char.
u0WaitRcvOver();
break;
}
WDTE=0xAC;
NOP();
NOP();
}
gCMDSTATE = CMD_STOP;
//match to respone2 info
if(Rsp2MatchFlag==TRUE)
{
//SendRetInfo("\r\nRsp2Err\r\n");
return FALSE;
}
return (Rsp1MatchFlag);
}
/****************************************************************************
**
** Abstract:
** Send at cmds to moderm. 3 tiems error then return .
**
** Parameters:
** None
**
** Returns:
** None
**
****************************************************************************/
uchar execute_AtCmd( uchar *CmdBuf,uchar CmdLen )
{
uchar temp;
uchar ucResult;
int pos,pos1;
//execute the command
pos = strpos(CmdBuf,0,'|'); //find out position of "|"
if(pos>=0) //mybe "AT|OK" or "AT|OK|10"
{
memcpy(u6.Buf,CmdBuf,pos);
u6.Buf[pos]=0x0D;
u6.Buf[pos+1]=0;
u6.Length = pos+1; //cmd length
//search the second '|'
pos1 = strpos(CmdBuf,pos+1,'|');
if(pos1>=0)
{
RESP_LEN = pos1-pos-1;
//get the time count
temp = (uchar)(CmdLen-pos1-1-1);
memcpy(tempBuf,CmdBuf+pos1+1,temp);
tempBuf[temp]=0;
//get the delay time.
DelayCnt = (uint)(atol(tempBuf));
}
else
{
RESP_LEN = CmdLen-pos-1-1;
//respone length must <=10
if(RESP_LEN>10) RESP_LEN = RESPONE_LEN;
}
//get the respone string
memcpy(RESP_BUF,CmdBuf+pos+1,RESP_LEN);
RESP_BUF[RESP_LEN]=0;
}
else
{
memcpy(u6.Buf,CmdBuf,CmdLen);
u6.Buf[CmdLen]=0;
u6.Length = CmdLen;
RESP_BUF[0]=0;
RESP_LEN=0;
}
//send cmd to moderm
DelayCnt = 2; //delay 2s
ucResult = send_cmd(u6.Buf,(uchar)u6.Length,IGNORE);
return (ucResult);
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is responsible for checking DTU id.
**
** Parameters:
** None.
**
** Returns:
** None.
**
**-----------------------------------------------------------------------------
*/
void check_wtdtime( void )
{
/*
uchar i;
int len;
ltoa (gWTDTIME/10, tempBuf );
len = strlen(tempBuf);
if(len<0) return;
DI();
UART6_Send(0x0D);
UART6_Send(0x0A);
UART6_Send('T');
UART6_Send('I');
UART6_Send('M');
UART6_Send('E');
UART6_Send('=');
for(i=0;i<len;i++)
{
UART6_Send(tempBuf[i]);
}
UART6_Send(0x0D);
UART6_Send(0x0A);
EI();
*/
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is responsible for setting DTU web information.
**
** Parameters:
** None.
**
** Returns:
** None.
**
**-----------------------------------------------------------------------------
*/
uchar send_at_cmd( char *cmdstr )
{
/*
uchar ucResult;
uchar i,len;
len = (uchar)(strlen(cmdstr));
for(i=0;i<len;i++)
{
ucResult = cmdstr[i];
}
*/
//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
return(send_cmd(cmdstr,strlen(cmdstr),IGNORE));
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is responsible for setting DTU web information.
**
** Parameters:
** None.
**
** Returns:
** None.
**
**-----------------------------------------------------------------------------
*/
uchar SetSMSPara( void )
{
int len;
if(gPROTOCOL == _PDU)
{
gMAX_RCV_LEN = BYTE140 * 2; //max buffer receive length
//arrange TPDU head
memcpy(TPDU,"0011000D9168",12);
len = gsmInvertNumbers(gPHONENUM, TPDU+12, gPHONENUM_LEN);
memcpy(TPDU+12+len,"000801",6);
TPDU[12+len+6]=0;
TPDU_LEN = 12+len+6+2; //2 reserve for 2bytes data length.
if(send_at_cmd("AT+CMGF=0\r")!=TRUE) return FALSE;
//AT+CSCS="UCS2"
if(SetCharSet("UCS2")!=TRUE) return FALSE;
if(send_at_cmd("AT+CSMP=17,168,0,8\r")!=TRUE) return FALSE;
}
else
{
gMAX_RCV_LEN = BYTE140; //max buffer receive length
if(send_at_cmd("AT+CMGF=1\r")!=TRUE) return FALSE;
//AT+CSCS="GSM"
if(SetCharSet("GSM")!=TRUE) return FALSE;
if(send_at_cmd("AT+CSMP=17,168,0,0\r")!=TRUE) return FALSE;
}
/*----below-- deleted by wy. 08/10/28---------------------------
if(send_at_cmd("AT+CNMI=2,1\r")!=TRUE) return FALSE;
---------------------------------------------------------------*/
//----below-- added by wy. 08/10/28---------------------------
if(send_at_cmd("AT+CNMI=2,1,0,0,1\r")!=TRUE) return FALSE;
//----above---------------------------------------------------
if(send_at_cmd("AT+CPMS=ME,ME,SM\r")!=TRUE) return FALSE;
return TRUE;
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is responsible for setting DTU web information.
**
** Parameters:
** None.
**
** Returns:
** None.
**
**-----------------------------------------------------------------------------
*/
uchar sim_at_cpbs( void )
{
uchar ucResult;
//AT+CPBS="SM"
u6.Buf[0]='A';
u6.Buf[1]='T';
u6.Buf[2]='+';
u6.Buf[3]='C';
u6.Buf[4]='P';
u6.Buf[5]='B';
u6.Buf[6]='S';
u6.Buf[7]='=';
u6.Buf[8]='"';
if(gCHIPTYPE == _HUAWEI){
u6.Buf[9]='M';
u6.Buf[10]='T';
}
else if(gCHIPTYPE == _SIEMENS){
u6.Buf[9]='S';
u6.Buf[10]='M';
}
else
{
u6.Buf[9]='S';
u6.Buf[10]='M';
}
u6.Buf[11]='"';
u6.Buf[12]=0x0D;
u6.Buf[13]=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -