#include<STC12C5A.h>
#include <GPRS.h>
#include <intrins.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<string.h>
#define uint unsigned int
#define uchar unsigned char
uchar GPRSBuff[30]={0};
char code AT_SK[]=">";
char code AT_Code[]="OK";
char RS_Sets_Cont=0; //发送 数组指针
char RsPoint=0; //发送 数组指针
char READRsPoint=0; //读取 数组指针
char RsBuf[LenRxBuf]; //接收缓存
//---定时用-----------
char timercount=0; //50MS定时器,
char timer_S_cnt=0;
void Delay2(uint ms)// 延时子程序
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/***********************************************************
函数名称:Print_Char
函数功能:发送单个字符
入口参数:ch 出口参数:无
***********************************************************/
void Print_Char(uchar ch)//发送单个字符
{
S2BUF=ch; //送入缓冲区
while(!(S2CON&S2TI)); //等待发送完毕
S2CON&=~S2TI; //软件清零
}
/***********************************************************
函数名称:Print_Str
函数功能:发送字符串
入口参数:*str 出口参数:无
***********************************************************/
void Print_Str(uchar *str)//发送字符串
{
while(*str!='\0')
{
Print_Char(*str);
str++;
}
}
/*void ClrRsBuf(char *p,int tt) //清空
{
//int i;
while(tt--)
{
*p='\0';
p++;
}
} */
/***********************************************************
函数名称:Ini_UART
函数功能:串口初始化、定时器初始化
入口参数:无 出口参数:无
***********************************************************/
void Ini_UART(void)//串口初始化、定时器初始化
{
AUXR &= 0xf7; //波特率不倍速
S2CON = 0x50; //8位数据,可变波特率
BRT = 0xFD; //设定独立波特率发生器重装值
AUXR |= 0x00; //独立波特率发生器时钟为Fosc,即1T
AUXR |= 0x10; //启动独立波特率发生器
IE2 = 0x01; //使能串口2中断
EA = 1; //开总中断
TH0 = 0xB8;
TL0 = 0x00; //20ms的时钟基准
TR0=1;
}
//////////////////////////////////////
/* void Send_AT(void)
{ uchar code mode[]="AT\r";
unsigned char *p;
timercount=50;
while(1)
{
// ClrRsBuf(RsBuf,sizeof(RsBuf));
Print_Str(mode);
ES=1; //
Delay2(500);
// p=strstr(RsBuf,AT_Code);
if(p!=NULL) break;
if(timercount==0) break;
}
}
*/
///////////////////////////////////////////////////
//////////////////////////////////////////////////////
uchar data system_server ; //系统状态
/*********************************************************************
* C51中字符串函数的扩充 *
** 函数名称 : strsearch ()
** 函数功能 : 在指定的数组里连续找到相同的内容
** 入口参数 : ptr2=要找的内容, ptr1 当前数组
**** 出口参数 : 0-没有 找到 >1 查找到
*********************************************************************/
/*uchar strsearch(uchar *ptr2,uchar *ptr1_at,uchar num)//查字符串*ptr2在*ptr1中的位置
//本函数是用来检查字符串*ptr2是否完全包含在*ptr1中
//返回: 0 没有找到
// 1-255 从第N个字符开始相同
{
//、 uchar max_length;
uchar i,j,k;
if(ptr2[0]==0) return(0);
for(i=0,j=0;i<num;i++)
{
if(ptr1_at[i]==ptr2[j])
{//第一个字符相同
for(k=i;k<num;k++,j++)
{
if(ptr2[j]==0)//比较正确
return(i+1); //返回值是整数,不含0
if(ptr1_at[k]!=ptr2[j]) break;
}
j=0;
}
}
return(0);
}
*/
/*********************************************************************
* C51中字符串函数的扩充 *
** 函数名称 : strsearch1()
** 函数功能 : 在指定的数组里连续找到相同的内容
** 入口参数 : ptr2=要找的内容, ptr1 当前数组, id 数组开始的第一个位置
**** 出口参数 : 0-没有 找到 >1 查找到
*********************************************************************/
//------在指定移态的数组里连续找到相同的内容---------------------------------------
//i=传来的指令, *ptr2=要找的内容, uchar *ptr1 当前数组
/*uchar strsearch1(uchar i,uchar *ptr2,uchar *ptr1)
{ uchar j=0,k;
k=i;
do{
if(ptr2[j]==0) return(k);
if(ptr1[k]==ptr2[j])
{j++;k++;}
else return(0);
}while(j<=60);
return(0xff);
}
*/
/*uchar Mygetch(char *ch)//从串口缓冲区读1字节数据
{
if(RsPoint==READRsPoint) {return 0;} //RxBuf Empty
*ch=RsBuf[READRsPoint];; READRsPoint++; //数据被读走,读指针加1
if(READRsPoint>=LenRxBuf) READRsPoint=0; //如果读指针到缓冲区尾部,则返回到头部
return 1;
}
*/
/*uchar MygetSets(char *Sets)//从串口缓冲区读取一个命令
{
char a[1]={0};
if(RS_Sets_Cont)
{
while(1)
{
if(Mygetch(a)==0)
{ RS_Sets_Cont=0; return 1; }
if(a[0]==0x0A)
{
RS_Sets_Cont--;
*Sets=0;
return 1;
}
if(a[0]==0x0D)
{
;
}
else
{
*Sets++=a[0];
}
}
}
else
{
return 0;
}
} */
//----------------------------------------------
/*uchar ReadBsbuffStrstr(uchar *buff,uchar *str)
{
timercount=10;
while(timercount>0)
{
MygetSets(buff);
if(strstr(buff,str)!=NULL)
{
while(RS_Sets_Cont>0)
{
MygetSets(buff);
}
return TRUE;
}
else if(RS_Sets_Cont==0) return FALSE;
}
}
*/
uchar Hand(uchar *a)
{
if(strstr((char *)RsBuf,(char *)a)!=NULL)
return 1;
else
return 0;
}
void CLR_RX(void)
{
char k;
for(k=0;k<50;k++) //将缓存内容清零
{
RsBuf[k] = 0;
}
READRsPoint = 0; //接收字符串的起始存储位置
}
void connect ()
{
uchar i ;
reconnect:
while(!Hand("OK")) //等待设置成功
{
Print_Str("AT%IOMODE=0,1,1\r\n");//配置apn
Delay2(10000);
}
CLR_RX();
Delay2(100);
while(!Hand("OK")) //等待设置成功
{
Print_Str("AT+CGDCONT=1,\042IP\042,\042CMNET\042\r\n");//配置apn
Delay2(10000);
}
CLR_RX();
Delay2(100);
while(!Hand("OK")) //等待设置成功
{
Print_Str("AT%ETCPIP=\042USER\042,\042GPRS\042\r\n");//进入tcpip功能
Delay2(10000);
}
CLR_RX();
Delay2(100);
while(!Hand("OK")) //等待设置成功
{
Print_Str("AT%ETCPIP?\r\n");//进入tcpip功能
Delay2(10000);
}
CLR_RX();
Delay2(100);
while(!Hand("CONNECT")) //打开一个连接
{
Print_Str("AT%IPOPEN=\042UDP\042,\04259.212.166.186\042,1234\r\n");//打开一个连接 改这儿ip 端口号1234就可
Delay2(10000);
i++;
if(i==5)
{
while(!Hand("OK")) //等待设置成功
{
Print_Str("AT%IPCLOSE=5\r\n");//进入tcpip功能
Delay2(10000);
}
CLR_RX();
Delay2(100);
i=0;
goto reconnect;
}
}
CLR_RX();
Delay2(100);
}