#define GSM_G
// System Head File
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
// User Head File
#include "option.h"
#include "2440addr.h"
#include "def.h"
//#include "mainfile.h"
#include "serial.h"
#include "GSM.h"
void SendMessage1(uchar *buf)
{
UartSendString(1,buf);
}
uchar mess_buf_in_end = 0;
// 发送AT指令
void ATCommand(uchar *mess, uchar flag)
{
mess_buf_in_end = 0;
Uart1.pIn = 0;
SendMessage1(mess); // 串口发送数据
if(flag) SendMessage1("\r\n");
}
// 等待GSM数据
char WaitMessageBack(uchar *mess, uchar n)
{
for(;;)
{
if(mess_buf_in_end == 1)
{
mess_buf_in_end = 0;
if(!strncmp(Uart1.buf, mess, n))
{
return TRUE;
}
}
}
}
// 接收AT指令数据
void Receive_Mess(uchar ch)
{
switch (ch)
{
case 0x0d: //行结束符
Uart1.buf[Uart1.pIn] = '\0';
Uart1.pIn = 0;
mess_buf_in_end = 1;
break;
case 0x0a: //不予理睬
break;
default:
Uart1.buf[Uart1.pIn++] = ch ;
if(Uart1.pIn >= 0xff) Uart1.pIn = 0xff;
break;
}
}
// 初始化短信息
void SMSInit(void)
{
ATCommand("ATE0",1); // 关闭回显
WaitMessageBack("OK",2);
ATCommand("AT+CCID",1); // 查看SIM 卡
Delay(3000) ;
ATCommand("AT+CSCA=\"+8613800200500\"",1); // 设置在广州地区的服务中心号码
WaitMessageBack("OK",2);
ATCommand("AT+CMGF=1",1); // 设置为文本方式
WaitMessageBack("OK",2);
ATCommand("AT+CNMI=2,1,0,0,0",1);
WaitMessageBack("OK",2);
}
// 发英文短信
// number 为手机号码
// mess 为短信内容
void SendSMS(uchar *number, uchar *mess)
{
uchar buf[2]={0x1A, 0x00};
ATCommand("AT+CMGS=\"", 0);
ATCommand(number, 0);
ATCommand("\"", 1);
Delay(3000) ;
ATCommand(mess, 0);
ATCommand(buf, 0);
WaitMessageBack("+CMGS",5);
}