热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

做一个项目,需要使用单片机控制GSM模块收发短信

#includeserial.h#includebianliang.h#include<REG51.h>#include<string.h>
#include "serial.h"
#include "bianliang.h"
#include
#include
#include "1602.H"
#define rev_FAUSE 0 
#define rev_OK 1
#define SELInt_on() ES=1      
#define SELInt_off() ES=0      
uchar temp[3];
unsigned long ss;


extern bit P0_0;
extern bit P0_1;
extern bit P0_2;
extern bit P0_3;
extern bit P0_4;
extern bit P0_5;
extern bit P0_6;
extern bit P0_7;


sbit P0_0=P0^0;
sbit P0_1=P0^1;
sbit P0_2=P0^2;
sbit P0_3=P0^3;
sbit P0_4=P0^4;
sbit P0_5=P0^5;
sbit P0_6=P0^6;
sbit P0_7=P0^7;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sbit P1_4=P1^4;
sbit P1_5=P1^5;
sbit P1_6=P1^6;
sbit P1_7=P1^7;
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_3=P2^3;


void delay1(int n)
{
  uchar i;
  while(n--)
   for(i=0;i<100;i++);
}
void Excrescent(void)
{
if(Rev_Buf[0]==0x00)
xx=1;
else
xx=0;
}


void gsm_Over(void)
{
  Ser_TxDchar(0x0d);
  Ser_TxDchar(0x0a);
}






void Cls_Rebuf(void)
{
unsigned char i;
for(i=0;i {
    Rev_Buf[i]=0;
}
Rev_count=0;  
}




Test_Mc39i(void)
{
Cls_Rebuf();
  SELInt_off();
Ser_TXDstring(TC_HEAD,2);//发送AT
gsm_Over();//回车换行
SELInt_on();
while(Rev_count<9);
Rev_count=0;
Excrescent();
for(i=5+xx;i<7+xx;i++)
{
temp[i-5-xx]=Rev_Buf[i];
}
    if(!strcmp(temp,TC_OK))
return rev_OK;
else
return rev_FAUSE;


}




MES_Mode(unsigned char mode)
{
Cls_Rebuf();
  SELInt_off();
  Ser_TXDstring(TC_HEAD,3);
  Ser_TXDstring(TC_CMGF,5);
  Ser_TxDchar(mode+0x30);
  gsm_Over();
  SELInt_on();
while(Rev_count<15);
Rev_count=0;
Excrescent();
for(i=12+xx;i<14+xx;i++)
{
temp[i-12-xx]=Rev_Buf[i];
}
    if(!strcmp(temp,TC_OK))
    return rev_OK;
else
return rev_FAUSE;

}




Set_Speed(void)
{


Cls_Rebuf();
  SELInt_off();
Ser_TXDstring(TC_HEAD,3);
Ser_TXDstring(TC_IPR,8);
gsm_Over();
SELInt_on();
while(Rev_count<18);
Rev_count=0;
Excrescent();
for(i=14+xx;i<16+xx;i++)
{
temp[i-14-xx]=Rev_Buf[i];
}
    if(!strcmp(temp,TC_OK))
return rev_OK;
else
return rev_FAUSE;

}


Sent_Mes(unsigned char *p)
{
Cls_Rebuf();
  SELInt_off();
Ser_TXDstring(TC_HEAD,3);
Ser_TXDstring(TC_CMGS,5);
Ser_TxDchar(0x22);
  Ser_TXDstring(TelNumber,11);
  Ser_TxDchar(0x22);
  gsm_Over();
  SELInt_on();
ss=0xffff;
while(Rev_Buf[Rev_count-1]!=0x3e)
    {
ss--;   
if(ss==0)
{return;}
    }
SELInt_off();
Ser_TXDstring(p,5);
Ser_TxDchar(0x1a);
SELInt_on();
}
Store_Location(void)
{


Cls_Rebuf();
  SELInt_off();
Ser_TXDstring(TC_HEAD,3);
Ser_TXDstring(TC_CPMS,5);
Ser_TxDchar(0x22);
Ser_TXDstring(TC_SM,2);
Ser_TxDchar(0x22);
Ser_TxDchar(0x2C);
Ser_TxDchar(0x22);
Ser_TXDstring(TC_SM,2);
Ser_TxDchar(0x22);
Ser_TxDchar(0x2C);
Ser_TxDchar(0x22);
Ser_TXDstring(TC_SM,2);
Ser_TxDchar(0x22);
gsm_Over();
SELInt_on();
while(Rev_count<54);
Rev_count=0;
Excrescent();
for(i=50+xx;i<52+xx;i++)
{
temp[i-50-xx]=Rev_Buf[i];
}
    if(!strcmp(temp,TC_OK))
return rev_OK;
else
return rev_FAUSE;


}




Auto_Prompt(void)
{


Cls_Rebuf();
  SELInt_off();
Ser_TXDstring(TC_HEAD,3);
Ser_TXDstring(TC_CNMI,8);
gsm_Over();
  SELInt_on();
while(Rev_count<17);
Rev_count=0;
Excrescent();
for(i=14+xx;i<16+xx;i++)
{
temp[i-14-xx]=Rev_Buf[i];
}
    if(!strcmp(temp,TC_OK))
return rev_OK;
else
return rev_FAUSE;
}




void New_Mes(void)
{
Excrescent();
for(i=3+xx;i<7+xx;i++)
{
temp1[i-3-xx]=Rev_Buf[i];
}


    if(!strcmp(temp1,TC_CMTI))
{
if(Rev_Buf[10]==0x53&&Rev_Buf[11]==0x4D)
{
NewSMS_flag=1;
MES_COUNT=Rev_Buf[13];
Cls_Rebuf();
}
}


}




void Read_Mes(uchar num)
{
Cls_Rebuf();
  SELInt_off();
Ser_TXDstring(TC_HEAD,3);
Ser_TXDstring(TC_CMGR,5);
Ser_TxDchar(num);
gsm_Over();
SELInt_on();
ss=0xffff;
while(Rev_Buf[Rev_count-1]!=0x4B)
    {
ss--;  
if(ss==0)
{return;}
    }
for(i=30;i<40;i++)
{
if(Rev_Buf[i]==0x2B&Rev_Buf[i+1]==0x38&Rev_Buf[i+2]==0x36)
{
t=i;

}
}
for(i=t+3;i {
TEL_Buf[i-t-3]=Rev_Buf[i];
}
TEL_Buf[11]=0x00;

for(i=t-20;i {
CMD_Buf[i-t+20]=Rev_Buf[i];
CMD_Buf[5]=0x00;
}


}




Del_Mes(num)
{
Cls_Rebuf();
SELInt_off();
Ser_TXDstring(TC_HEAD,3);
Ser_TXDstring(TC_CMGD,5);
Ser_TxDchar(num);
gsm_Over();
SELInt_on();
while(Rev_count<14);

Rev_count=0;
for(i=12;i<14;i++)
{
temp[i-12]=Rev_Buf[i];
}
    if(!strcmp(temp,TC_OK))
return rev_OK;
else
return rev_FAUSE;
Cls_Rebuf();
}






void CMD_SHIBIE(void)
{
if(!strcmp(TEL_Buf,TelNumber))
{
P1_2=0;
TELNUM_OK=1;  

}
else  
{ P1_3=0;
TELNUM_OK=0;
Del_Mes(MES_COUNT);
Cls_Rebuf();
NewSMS_flag=0;
}
if(!strcmp(CMD_Buf,CMD_1))
{
CMD=1;


}
else if(!strcmp(CMD_Buf,CMD_2))
{
CMD=2;


}
else
{
CMD=0;
}
}






void INIT_GSM(void)
{
if(Test_Mc39i())
{
if(MES_Mode(1))
{
if(Set_Speed())
{
if(Store_Location())
{
if(Auto_Prompt())
P1_0=0;
}


}

}
}
Cls_Rebuf();

}


我做过一点;
(1)pc机和单片机发送的串口数据是不是不一样?
答:一样,一般都为9600,n,8,1

(2)把单片机和pc上位机相连,pc的超级终端接收的命令确实正确的?
答:可以连,注意RXD和TXD应交叉对联。满足单片机发送PC接收,PC发送MCU接收的关系

(2)是不是单片机和pc机波特率不一样?
答,GSM模块一般都是自适应模特率的,只要断电一次,就可以自己适应当前波特率

注意:你通讯补上可能是RXD和TXD交叉的问题(交换TXD和RXD尝试)
,也可能是需要硬件握手的问题(RTS CTS


推荐阅读
author-avatar
阿凯宜_
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有