📄 debug.c
字号:
if(ListBuf[ii]=='L')
{
cmgl_state=1;
list_index_begin=ii;
}
}
else if(cmgl_state==1)
{
if(ListBuf[ii]==LF)
{
delay();
list_index_end=strpos(ListBuf+list_index_begin,',');
list_index_end=list_index_begin+list_index_end;
memcpy(list_indexlist+listcmdindex,ListBuf+list_index_begin+2,list_index_end-list_index_begin-2);
listcmdindex+=list_index_end-list_index_begin-1;
list_indexlist[listcmdindex-1]=0;
listcmd_count++; //短信条数
list_PDU_begin=ii+1; //pdu的起始地址即LF后一字节
if(ListBuf[list_index_end+1]=='0') //判断短信是否为已读状态
cmgl_state=2;
else
cmgl_state=0;
}
}
else if(cmgl_state==2) //处理未读的短信
{
if(ListBuf[ii]==LF)
{
list_PDU_end=ii-2;
memcpy(PDUBuf,ListBuf+list_PDU_begin,list_PDU_end-list_PDU_begin+1);
PDUBuf[list_PDU_end-list_PDU_begin+1]=0;
PDU2SMS(PDUBuf);
Data_processing();
cmgl_state=0;
wait_5s();
}
}
}
//delete message
listcmdindex=0;
for(j=0;j<listcmd_count;j++)
{
strcpy(CMDBuf,"AT+CMGD=");
strcpy(CMDBuf+strlen(CMDBuf),list_indexlist+listcmdindex);
listcmdindex+=strlen(list_indexlist+listcmdindex)+1;
CMDBuf[strlen(CMDBuf)]='\r';
CMDBuf[strlen(CMDBuf)]=0;
send_string(CMDBuf);
wait_5s();
}
// state variable reset
listcmd_count=0;
bListEnd=0;
listchar_count=0;
list_index_begin=0;
list_index_end=0;
listcmdindex=0;
memset(ListBuf,0,sizeof(ListBuf));
memset(PDUBuf,0,sizeof(PDUBuf));
memset(list_indexlist,0,sizeof(list_indexlist));
}
}
/////////////function: 把两ascii字节合并成一char 例如"35"变为0x35;//////////////
uchar hex2int(uchar * str)
{
uchar highbyte;
uchar lowbyte;
highbyte = toint(str[0]); //asii to 0x00~0x09 OR 0x0a~0xf
lowbyte = toint(str[1]);
return highbyte*16+lowbyte;
}
/////////function: 把char变为字符串 例如0x36 "36"/////////////////////////////
void int2hex(uchar num,uchar *dest)
{
uchar highbyte;
uchar lowbyte;
highbyte = num/16;
lowbyte = num-highbyte*16;
if(highbyte>9)
dest[0]=highbyte+ALPHABASE;
else
dest[0]=highbyte+NUMBASE;
if(lowbyte>9)
dest[1]=lowbyte+ALPHABASE;
else
dest[1]=lowbyte+NUMBASE;
dest[2]=0;
}
///function: PDU 7位编码程序,将源字符串数组str转换为手机可以识别的dest字符串 SMS->PDU///
uchar PDU7BitEncode(char * str,char * dest)
{
uchar nSrc; // 源字符串的计数值
uchar nChar; // 当前正在处理的组内字符字节的序号,范围是0-7
uchar nLeft; // 上一字节残余的数据
uchar nSrcLength = strlen(str);
char * pSrc = str;
char * pDst = dest;
// 计数值初始化
nSrc = 0;
// 将源串每8个字节分为一组,压缩成7个字节
// 循环该处理过程,直至源串被处理完
// 如果分组不到8字节,也能正确处理
while(nSrc<=nSrcLength)
{
// 取源字符串的计数值的最低3位
nChar = nSrc&7;
// 处理源串的每个字节
if(nChar == 0)
{
// 组内第一个字节,只是保存起来,待处理下一个字节时使用
nLeft = *pSrc;
}
else
{
// 组内其它字节,将其右边部分与残余数据相加,得到一个目标编码字节
uchar temp;
temp = (*pSrc<<(8-nChar))|nLeft;
int2hex(temp,pDst);
// 将该字节剩下的左边部分,作为残余数据保存起来
nLeft = *pSrc >> nChar;
// 修改目标串的指针和计数值
pDst=pDst+2;
}
// 修改源串的指针和计数值
pSrc++;
nSrc++;
}
*pDst=0;
return (pDst-dest)/2;
}
///////////////function: decode PDU to SMS//////////////////////////////////////
void PDU7BitDecode(char * str,char * dest)
{
int length=strlen(str)/2;
uchar remainder=0;
uchar j=0;
uchar i=0;
uchar tt=0;
uchar original;
uchar index;
uchar tmp;
for(i=0;i<length;i++)
{
original=hex2int(str+2*i);
index=i%7;
tt=(i+1)%7;
tmp=original;
tmp=tmp<<index;
if(tt==0)
{
dest[j]=(tmp&0x7f)+remainder;
j++;
dest[j]=(original>>1)&0x7f;
remainder=0;
}
else
{
dest[j]=(tmp&0x7f)+remainder;
remainder=original>>(8-index-1);
}
j=j+1;
}
dest[sms_deliver.PDUDataLen]=0;
}
//////////////function:发送AT+CMGS命令,然后发送数据信息///////////////////////
void send_response()
{
uchar tmp=0;
uchar lenoflen,quot;
uchar bitcodedlen = 0;
tmp=strlen(sms_deliver.PDUData);
quot=tmp/8;
bitcodedlen=quot*7+tmp%8; //the length of the PDUData after compact(7->8)
///发送前先检验网络注册情况
send_string("AT+COPS=0\r"); //GSM network operator is to be selected automatically
delay_1s();
delay_1s();
put_string(0x80,RcvBuf); //显示出来返回的结果
send_string("AT+COPS?\r"); //read the status to GSM network operator
delay_160ms();
put_string(0x80,RcvBuf); //显示出来返回的结果
strcpy(CMDBuf,"AT+CMGS="); //AT+CMGS=<数据长度>命令
lenoflen=itoa(bitcodedlen+PhoneNumLen+2,CMDBuf+8); //PhoneNumLen+2位手机号码 8:"AT+CMGS="八个字符
CMDBuf[8+lenoflen]=CR;
CMDBuf[8+lenoflen+1]=0;
send_string(CMDBuf); //send command
delay();
encode_message();
delay_160ms();
send_string(SendBuf); //send SMS
delay_1s();
delay();
}
////////////////////function:convert the PDU data to SMS////////////////////////
void PDU2SMS(char * Buf)
{
uchar index=0;
delay();
sms_deliver.SMSCLen=hex2int(Buf);
sms_deliver.AddressType[0]=Buf[2];
sms_deliver.AddressType[1]=Buf[3];
sms_deliver.AddressType[2]=0;
PDUHalf8bitDecode(Buf+4,(sms_deliver.SMSCLen-1)*2,sms_deliver.ServiceCenter);
sms_deliver.ServiceCenter[16]=0;
index=4+(sms_deliver.SMSCLen-1)*2;
sms_deliver.SMSDeliver1[0]=Buf[index];
sms_deliver.SMSDeliver1[1]=Buf[index+1];
sms_deliver.SMSDeliver1[2]=0;
index=index+2;
sms_deliver.PhoneNumLen=hex2int(Buf+index);
sms_deliver.PhoneNumType[0]=Buf[index+2];
sms_deliver.PhoneNumType[1]=Buf[index+3];
sms_deliver.PhoneNumType[2]=0;
memset(sms_deliver.PhoneNumber,'\0',strlen(sms_deliver.PhoneNumber));
PDUHalf8bitDecode(Buf+index+4,sms_deliver.PhoneNumLen,sms_deliver.PhoneNumber);
sms_deliver.PhoneNumber[sms_deliver.PhoneNumLen]=0;
index=index+4+sms_deliver.PhoneNumLen+1;
sms_deliver.ProtocolFlag[0]=Buf[index];
sms_deliver.ProtocolFlag[1]=Buf[index+1];
sms_deliver.ProtocolFlag[2]=0;
sms_deliver.EncodeType[0]=Buf[index+2];
sms_deliver.EncodeType[1]=Buf[index+3];
sms_deliver.EncodeType[2]=0;
PDUHalf8bitDecode(Buf+index+4,14,sms_deliver.TimePost);
sms_deliver.TimePost[14]=0;
sms_deliver.PDUDataLen=hex2int(Buf+index+18);
if(strcmp(sms_deliver.EncodeType,"00")==0)
PDU7BitDecode(Buf+index+20,sms_deliver.PDUData);
if(sms_deliver.PhoneNumLen)
PhoneNumLen=sms_deliver.PhoneNumLen;
delay();
}
///////////////////function: decode phone///////////////////////////////////////
void PDUHalf8bitDecode(char * str,uchar len,char * dest)
{
char i=0;
for(i=0;i<len;i=i+2)
{
dest[i]=str[i+1];
dest[i+1]=str[i];
}
if(dest[len-1]=='F')
dest[len-1]=0;
}
///function:process the new SMS,check if the center phone is changed////////////
void Data_processing()
{
delay();
if(sms_deliver.EncodeType[1]=='0')
{
if(strncmp(sms_deliver.PDUData,"ABC123",6)==0)
//正确的数据格式:密码(ABC123)+手记号码(8613810736479)+校验和
{
if(ascii_verify(sms_deliver.PDUData))
{
memset(center_phone,0,sizeof(center_phone));
strncpy(center_phone,sms_deliver.PDUData+6,sms_deliver.PDUDataLen-8);
// sequential_write(0x8000,PhoneNumLen+1,center_phone); //保存上位机号码
//(同时要保存PhoneNumLen?)
}
memset(sms_deliver.PDUData,0,sizeof(sms_deliver.PDUData));
}
}
}
///////////////////////function:string verify///////////////////////////////////
char ascii_verify(uchar * dest)
{
char i = 0;
uchar result = 0;
uchar tmp[3];
uchar count = strlen(dest)-2;
for(;i<count;i++)
{
result+=dest[i];
}
tmp[0] = dest[count];
tmp[1] = dest[count+1];
tmp[2] = 0;
if(result==hex2int(tmp))
return 1;
else
return 0; //////应该是return 0
}
//////////////function:编码形成短信,送到SendBuf中//////////////////////////////
void encode_message()
{
char phone[15];
memset(SendBuf, '\0', sizeof (SendBuf));
memset(phone, '\0', sizeof (phone));
encode_phone(phone); //将center_phone编码为phone
strcpy(SendBuf,"0891683108100005F0");
strcpy(SendBuf+strlen(SendBuf),"11000D91");
strcpy(SendBuf+strlen(SendBuf),phone);
strcpy(SendBuf+strlen(SendBuf),"0000A7");
int2hex(strlen(sms_deliver.PDUData),SendBuf+strlen(SendBuf));
PDU7BitEncode(sms_deliver.PDUData,SendBuf+strlen(SendBuf));
SendBuf[strlen(SendBuf)+1]=0;
SendBuf[strlen(SendBuf)]=0x1A;
}
///////////////function:对手机号码编码//////////////////////////////////////////
unsigned char encode_phone(char *dest)
{
uchar i,j;
j=0;
for(i=0;i<PhoneNumLen+1;i++)
{
if(j==0)
{
dest[i]=center_phone[i+1];
j=1;
}
else
{
dest[i]=center_phone[i-1];
j=0;
}
}
if(PhoneNumLen%2!=0) ///如果上位机号码位数是奇数个
dest[PhoneNumLen-1]='F';
dest[PhoneNumLen+1]=0;
return PhoneNumLen+1;
}
/////////function://功能类似于int2hex,增加到三位,并且各位都在0~9之间////////
char itoa(char num , char *str)
{
uchar hundred=num/100;
uchar ten=(num-hundred*100)/10;
uchar base=(num-hundred*100-ten*10);
uchar index=0;
if(hundred!=0)
{
str[index]=hundred+NUMBASE;
index=index+1;
}
if(ten!=0)
{
str[index]=ten+NUMBASE;
index=index+1;
}
str[index]=base+NUMBASE;
str[index+1]=0;
return index+1;
}
/////////////////////////function:delay 5s//////////////////////////////////////
void wait_5s()
{
uint idata i;
char idata j;
for(i=0;i<5000;i++)
{
delay();
for(j=0;j<14;j++)
_nop_();
delay();
if(bResponsed) //如果收到短信,跳出延时程序
break;
}
bResponsed=0;
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////SFR和各全局变量、开机界面等的初发始化///////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
void startup_init()
{
char i;
PMR = PMR | 0x01; //W77E58允许使用片内的1K SRAM
// AUXR=0x01; //EXTRAM=0:访问物理上在内部,逻辑上在外部的RAM;
//ALEOFF=1:禁止ALE输出,以降低对外界的EMI干扰
// WDT_CONTR=0x30; //看门狗定时71.1ms,具体设置见PDF文档(喂狗语句应为WDT_CONTR=0x30;)
TH1=0xfd;
TL1=0xfd; //串口波特率设置9600
TMOD=0x20; //GATE1=0; GATE0=0,C/T=0,M1=1,M0=0, 定时器工作模式选择
IP=0x10; // PS=0, 串口中断为高优先级中断
SCON=0x50; // 串口工作方式选择,异步8位,mode=10 ren1
PCON=0x00; // 波特率不倍增
TR1=1; //启动定时器
IE=0x90; //EA=1;ES=1;ET1=0;EX1=1;ET0=0;EX0=0;
P1=0x0f; //键盘八条线四高四低
display_type=0; //0:品种界面1:价格界面2:已存信息内容
vege_page_index=0; //蔬菜品种页面索引为 0
info_page_index=0; //已存蔬菜信息页面索引置初值0
info_count=0; //已存信息条数置初值0
vege_type[0]='\0'; //蔬菜品种置初值'\0'
vege_type[1]='\0'; //同上
vege_type[2]='\0';
for(i=0;i<=4;i++)
vege_price[i]='\0'; //蔬菜价格置初值' '
memset(ListBuf,0,sizeof(ListBuf));
memset(PDUBuf,0,sizeof(PDUBuf));
memset(RcvBuf, '\0', sizeof (RcvBuf));
memset(SendBuf, '\0', sizeof (SendBuf));
memset(&sms_deliver ,0,sizeof(sms_deliver));
memset(CMDBuf,0,sizeof(CMDBuf));
memset(sms_deliver.PDUData,0,sizeof(sms_deliver.PDUData));
strcpy(center_phone,"8613752061177");
init_LCD(); //初始化LCD;
put_string(0x90," "); //显示定位,如果没有这条语句,显示经常不对
put_string(0x90,"初始化..."); //开机显示的字符串
}
////////////////////////////////////////////////////////////////////////////////////////
/////////////main()函数,做所有需要初始化的工作都在main()///////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
//全部的初始化工作
main()
{
char i ;
startup_init();
for(i=0;i<3;i++) //初始化5次,再不成功就放弃
{
if(i>0) delay_1s() ;
if(init_GSM()==0) break ; //初始化手机模块成功就返回0,失败返回1;
write_cmd(0x98); write_data(NUMBASE+i+1) ;//在第四行第一列显示第i次手机模块初始化
}
IE=0x94; //EA=1;ES=1;ET1=0;EX1=1;ET0=0;EX0=0;打开键盘中断
if(0) //上位机号已设置 if( (byte_read(0x8000)=='8')&&(byte_read(0x8001=='6') )
{
i=0; //把EEPROM中读出的号保存到center_phone
while(1)
{
// center_phone[i]= byte_read(0x8000+i);
if(center_phone[i]=='\0') break;
i++;
}
}
else
{
strcpy(center_phone,"8613141199264");
// sequential_write(0x8000,14,center_phone); //保存上位机号码
}
receive_SMS(); //接收短信
PhoneNumLen=13; //号码长度暂为13 需要改
type_screen(); //液晶显示
while(1)
{
// receive_SMS(); //接收短信
_nop_();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -