📄 sim300-gprs.txt
字号:
#define olen 16 //send size
unsigned char idata outbuf[olen]; //send buff
unsigned char idata *outlast=outbuf; //last ouput buff datalocation
unsigned char idata *putlast=outbuf; //last input buff datalocation except serial
#define ilen 13 //reception size
unsigned char idata inbuf[ilen]; //reception buff
unsigned char idata *ilast=inbuf; //last input buff datalocation
unsigned char idata AT_BUFF[20]; //at buff
unsigned char idata *inline=AT_BUFF;
bit bdata obufsign ; //发送标志
bit bdata ibufsign ; //接收标志
bit bdata data_number ; //数据数目
bit bdata inbufful ; //inbuf溢出
bit bdata data_over;
bit bdata T0_10ms_count ; //时间等待溢出
void getDatas(void); //接收数据AT_BUFF
void SendData(unsigned c); //发送一个字节数据
void SendDatas(unsigned *outline); //发送一串数据
void putstr(unsigned char *outplace,unsigned char j); //转移数据串到发送区
void String2Bytes(const unsigned char* pSrc, char* pDst,int nSrcLength); //AB->0xAB
void Bytes2String(const unsigned char* pSrc, char* pDst,int nSrcLength); //0xAB->AB
/***************************串口中断**********************************/
void serale(void) interrupt 4 using 2
{
if(TI)
{
TI=0;
if(obufsign) //当数据准备好了obufsign=1,TI=1时启动发送
{
SBUF=*outlast;
outlast++;
if(outlast==outbuf+olen)
{
outlast=outbuf;
obufsign=0;
}
}
}
if(RI)
{
RI=0;
if(ibufsign)
{
data_number++;
if(data_number>=1&&data_number<=3)
{
*inlast=SBUF;
inlast++;
}
if(inlast==inbuff+ilen)
{
inlast=inbuf;
inbufful=1 ;
}
if(data_number==3)
{
inlast=inbuf;
data_over=1;
}
}
}
}
/**************假如是T0记时 大概是0.01s**************************************/
void timer0_rup(void) interrupt 1
{
T0_10ms_count++;
TL0=0x00;
TH0=0xB8;
}
/*****************************数据接收函数***********************************/
void getDatas(void)
{
unsigned char data_over=0;
T0_10ms_count=0;
RI=0;
do
{
while(!RI)
{
if(T0_10ms_count==3000) //是30秒 连接超时
{
data_voer=1;
break;
}
};
RI=0;
*inline=SBUF;
if(*inline==0x0A&&*(inline-1)==0x0D)
{
if(*(inline-2)=='K'||*(inline-2)=='R'||*(inline-2)=='T')
{
if(*(inline-3)=='O'||*(inline-3)=='C'||*(inline-3)=='E')
data_voer=1;
}
}
inline++;
}
while(data_over!=1);
inline=AT_BUFF;
DelayUs(0x09c4); //(大概是120ms)延时10ms 115200byte/s 70us/byte,可延时150个字节
}
/*****************************DelayUs**************************************/
void DelayUs(uint delay_Nus) //延时N us
{
while(delay_Nus)
delay_Nus--;
}
/***************************向串口发一个字节数据********************************/
void SendData(unsigned c)
{
unsigned i;
SBUF=c;
while(!TI);
TI=0;
}
/****************************向串口发一串数据*************************/
void SendDatas(unsigned *outline)
{
int i;
DelayUs(0x04E2); //延时 5ms
for(i=0;*outline!=0;i++)
{
SendData(*outline);
outline++;
}
}
/*****************************放一块数据到发送缓冲区****************************/
void putstr(unsigned char *outplace,unsigned char j)
{
int i;
for(i=0;i<j;i++)
{
*putlast=*outplace;
putlast++;
outplace++;
if(putlast==outbuf+olen) putlast=outbuf;
}
if(!outbufsign)
{
outbufsign=1;
TI=1;
}
}
/*****************************字符转换*********************************
// 十六进制字符串转化为十六进制
// 如:"C8329BFD0E01" --> {0xC8,0x32,0x9B,0xFD,0x0E,0x01}
// pSrc: 源数据指针
// pDst: 目标字符串指针
// nSrcLength: 源数据长度
// 返回: 目标字符串长度
*************************string2bytes*********************************/
void String2bytes(const char* pSrc,unsigned char* pDst,unsigned char nSrcLength)
{
unsigned int i; //输出高4位
for(i=0;i<nSrcLength;i+=2)
{
if((*pSrc>='0') && (*pSrc<='9'))
{
*pDst=(*pSrc-'0')<<4;
}
else
{
*pDst=(*pSrc-'A'+10)<<4;
}
pSrc++; //输出底4位
if((*pSrc>='0') && (*pSrc<='9'))
{
*pDst |=*pSrc-'0';
}
else
{
*pDst |=*pSrc-'A'+10;
}
pSrc++;
pDst++;
}
nSrcLength/=2;
return(nSrcLength);
}
/********************************************************
// 字节数据转换为可字符串
// 如:{0xC8,0x32,0x9B,0xFD,0x0E,0x01} -->"C8329BFD0E01"
// pSrc: 源数据指针
// pDst: 目标字符串指针
// nSrcLength: 源数据长度
// 返回: 目标字符串长度
******************************************************************/
void Bytes2String(const unsigned char* pSrc, char* pDst,int nSrcLength)
{
const char tab[]="0123456789ABCDEF"; // 0x0-0xf的字符查找表
for(int i=0;i<nSrcLength;i++)
{
*pDst++ = tab[*pSrc>>4]; //输出低4位
*pDst++ = tab[*pSrc&0x0f]; //输出高4位
pSrc++;
}
// *pDst = '\0'; //输出字符串加个结束符
return(SrcLength*2);
}
/*****************初始化***************************************/
void init()
{
outbufsign=0;
inbufful=0;
data_number=0;
data_over=0;
T0_10ms_count=0;
TMOD=0x01; //T0作为定时器
T2CON=0x30; //T2作为波特率发生器
SCON=0x50;
TR0=0; //关定时器T0
TR2=0; //关定时器T2
EA=0; //关中断
ES=0;
TH0=0xB8; //定时10毫秒,装初值
TL0=0x00; //
TR0=1; //开定时器T0
TR2=1; //开定时器T2
EA=1; //开中断
ET0=1; //开定时器T0中断
TI=0; //清发送置位
RI=0; //清接收置位
}
/*********************************MAIN函数********************************/
void main()
{
unsigned i;
for(i=0;i<=2;i++)
{
DelayUs(0x7A12);
}
init();
/*************关中断防止数据错误*****************/
ES=0;
/******&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&********/
for(i=0;i<=2;i++)
{
SendDatas("AT\r\n");
getdatas();
if(AT_BUFF[2]=="O" && AT_BUFF[3]=="K")
break;
}
for(i=0;i<20;i++)
{
DelayUs(0x30D4); //延时
}
/***************************************************************/
for(i=0;i<=2;i++)
{
SendDatas("AT+CLPORT="TCP","1214"\r\n");
getDatas();
if(AT_BUFF[2]=='O' && AT_BUFF[3]=='K')
break;
}
for(i=0;i<20;i++)
{
DelayUs(0x30D4); //延时
}
/*****************************************************/
for(i=0;i<=2;i++)
{
SendDatas("AT+CIPSTART="TCP",");
SendData('"');
SendDatas("60.176.84.10");
SendData('"');
SendData(',');
SendData('"');
SendDatas("8080");
SendData('"');
SendDatas("\r\n");
getDatas();
if(AT_BUFF[2]=='O' && AT_BUFF[3]=='K')
break;
}
for(i=0;i<20;i++)
{
DelayUs(0x30D4); //延时
}
/*******************打算发数据AT+CIPSEND**********************************/
SendDatas("AT+CIPSEND\r\n");
for(i=0;i<20;i++)
{
DelayUs(0x30D4); //延时
}
/*********************开中断*********************************/
ES=1;
putstr("Command Ready!",14); //upload call pc download data
for(i=0;i<20;i++) DelayUs(0x30D4);
while(data_over==0); //download over
for(i=0;i<20;i++) DelayUs(0x30D4);
data_number=0;
data_over=0;
putsr("DATA OK!\r\n",10); //answer pc
for(i=0;i<20;i++) DelayUs(0x30D4);
/*****************************************************/
for(i=0;i<=2;i++)
{
SendDatas("AT+CIPHEAD=1\r\n");
getDatas();
if(AT_BUFF[2]=='O' && AT_BUFF[3]=='K')
break;
}
for(i=0;i<20;i++)
{
DelayUs(0x30D4); //延时
}
while(1)
{
/*************************start*******************************************/
while(data_over==0); //over pc data
for(i=0;i<20;i++) DelayUs(0x30D4);
data_number=0;
data_over=0;
String2byte(inbuf,??,14);
putstr(inbuf,7); //return to send data to CAN
for(i=0;i<20;i++) DelayUs(0x30D4);
/*********************************************************************/
while(data_over==0); //recive CAN data, and ready upload pc
for(i=0;i<20;i++) DelayUs(0x30D4);
data_number=0;
data_over=0;
Byte2string(inbuf,??,14);
putstr(inbuf,7); //upload data to PC
for(i=0;i<20;i++) DelayUs(0x30D4);
}
} //最后的括号
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -