#include "main.h"
extern u8 g_CdmaRcvBuf[MSGSIZE_CDMA]; /*串口接收到的GSM数据*/
extern u16 g_GsmCnt; /*串口接收到的GSM数据计数*/
/***********************************
函数功能:读信号质量
输入参数:无
返回值:无
***********************************/
u8 SignalQulity()
{
u8 *ptr=NULL;
u8 csq;
writeStrCom(COM_CDMA, TRUE, 0, "AT+CSQ");
ptr=waitAtCmd("+CSQ:",8);
if(ptr)
{
csq = getIndex(ptr+5, 5);
return csq;
}
return 0;
}
/****************************************
函数功能:设置模块的日期和时间
输入参数:Date:要设置的日期
Time:要设置的时间
返回值:成功返回1,失败返回0
****************************************/
u8 SetTime(DATE* Date,TIME* Time)
{
u8 *ptr=NULL;
u8 buf[22];
if(Date->year<0||Date->year>99)
{
return 0;
}
int2BcdAscStr(buf,Date->year,2);
strcat(buf,"/");
if(Date->month<=0||Date->month>12)
{
return 0;
}
int2BcdAscStr(buf+3,Date->month,2);
strcat(buf,"/");
if(Date->daily<=0||Date->daily>31)
{
return 0;
}
int2BcdAscStr(buf+6,Date->daily,2);
strcat(buf,",");
if(Time->hour<0||Time->hour>23)
{
return 0;
}
int2BcdAscStr(buf+9,Time->hour,2);
strcat(buf,":");
if(Time->minute<0||Time->minute>59)
{
return 0;
}
int2BcdAscStr(buf+12,Time->minute,2);
strcat(buf,":");
if(Time->sencond<0||Time->sencond>59)
{
return 0;
}
int2BcdAscStr(buf+15,Time->sencond,2);
if(Time->zone<0)
{
strcat(buf,"-");
}
else
{
strcat(buf,"+");
}
Time->zone=abs(Time->zone);
int2BcdAscStr(buf+18,Time->zone,2);
strcat(buf,"\"");
buf[21]='\0';
writeStrCom(COM_CDMA, FALSE, 0, "AT+CCLK=\"");
writeStrCom(COM_CDMA, TRUE, 0, buf);
ptr=waitAtCmd("OK",30);
if(ptr)
{
return 1;
}
return 0;
}
/***************************************
函数功能:设置报警日期和时间
输入参数:Date:报警的日期
Time:报警的时间
返回值:成功返回1,失败返回0
***************************************/
u8 SetAlarm(DATE* Date,TIME* Time)
{
u8 *ptr=NULL;
u8 buf[22];
if(Date->year<0||Date->year>99)
{
return 0;
}
int2BcdAscStr(buf,Date->year,2);
strcat(buf,"/");
if(Date->month<=0||Date->month>12)
{
return 0;
}
int2BcdAscStr(buf+3,Date->month,2);
strcat(buf,"/");
if(Date->daily<=0||Date->daily>31)
{
return 0;
}
int2BcdAscStr(buf+6,Date->daily,2);
strcat(buf,",");
if(Time->hour<0||Time->hour>23)
{
return 0;
}
int2BcdAscStr(buf+9,Time->hour,2);
strcat(buf,":");
if(Time->minute<0||Time->minute>59)
{
return 0;
}
int2BcdAscStr(buf+12,Time->minute,2);
strcat(buf,":");
if(Time->sencond<0||Time->sencond>59)
{
return 0;
}
int2BcdAscStr(buf+15,Time->sencond,2);
if(Time->zone<0)
{
strcat(buf,"-");
}
else
{
strcat(buf,"+");
}
Time->zone=abs(Time->zone);
int2BcdAscStr(buf+18,Time->zone,2);
strcat(buf,"\"");
buf[21]='\0';
writeStrCom(COM_CDMA, FALSE, 0, "AT+CALA=\"");
writeStrCom(COM_CDMA, TRUE, 0, buf);
ptr=waitAtCmd("OK",30);
if(ptr)
{
return 1;
}
return 0;
}
/*****************************************************
函数功能:发送短信息
输入参数:phonenum:要发送的手机号码
centernum:短信息中心号码
data:短信息内容
smsLen: 短信息长度
msgtype:短信息类型,'C'表示中文,'E'表示英文
返回值:无
*****************************************************/
void gsmSmsSend(u8 *phonenum,u8 *centernum, const u8 *data, u16 smsLen, u8 msgtype)
{
u8 pSmsBuf[MSGSIZE_SMS];
u8 qSmsBuf[MSGSIZE_SMS];
u8 pdata[MSGSIZE_SMS];
u8 changebuf[20];
u8 *buf=NULL;
u8 *ptr=NULL;
u8 cLength[5];
u8 len,i,pduLen=0;
memset(pSmsBuf,0,MSGSIZE_SMS);
memset(qSmsBuf,0,MSGSIZE_SMS);
memset(pdata,0,MSGSIZE_SMS);
len=gsmInvertNumbers(centernum,changebuf);
len/=2;
len++;
byte2HexAscStr(len,qSmsBuf);
strcat(qSmsBuf,"91");
strcat(qSmsBuf,changebuf);
strcpy(pSmsBuf,"11000D91");
gsmInvertNumbers(phonenum,changebuf);
strcat((char*)pSmsBuf,changebuf);
if (msgtype=='C')
{
strcat((char*)pSmsBuf,"000800");//00表示协议标识(TP-PID),是普通GSM类型,点到点方式。第二个08表示UCS2编码,00表示信息有效期
pduLen = strlen((char const*)pSmsBuf);
//smsLen/=2;
byte2HexAscStr(smsLen,pSmsBuf+pduLen);//msglen
pduLen += 2;
strcat((char*)pSmsBuf,data);//直接加unicode码
}
else
{//英文短信
strcat((char*)pSmsBuf,"000099");//00表示协议标识(TP-PID),是普通GSM类型,点到点方式。第二个00表示7-bit编码,99表示信息有效期
pduLen = strlen((char const*)pSmsBuf);
byte2HexAscStr(smsLen,pSmsBuf+pduLen);//msglen
len = atTextToPDUData((char*)data,pdata,smsLen);//在这将数据转换成7位
//len=gsmEncode7bit(data,pdata,smsLen);
pduLen += 2;
for (i = 0;i <len;i++)//短消息内容转成十六进制字符串
{
byte2HexAscStr(pdata[i],pSmsBuf+pduLen);
pduLen += 2;
}
}
if ((pduLen <= 0) || (pduLen > MSGSIZE_SMS))
{
return;
}
strcat((char*)qSmsBuf,(char*)pSmsBuf);
strcat((char*)qSmsBuf,"\x01a");//加发送符ctrl+z
len=strlen(qSmsBuf);//整个数据的长度
pduLen=strlen(pSmsBuf);
pduLen /=2;
int2BcdAscStr((char*)cLength, pduLen, 3);//计算pdu数据的长度,转成十进制字符串
cLength[3] = 0x0D;//<CR>
cLength[4] = 0;
dly20ms(5);
memset(g_CdmaRcvBuf,0x0,MSGSIZE_CDMA);
g_GsmCnt=0;
writeStrCom(COM_CDMA, FALSE, 0, "AT+CMGS=");
writeStrCom(COM_CDMA, FALSE, 0, cLength);
ptr = waitAtCmd(">",20);
if(ptr)
{
writeStrCom(COM_CDMA,TRUE, len, (u8*)qSmsBuf);
ptr = waitAtCmd("OK",50);
if(ptr)
{
writeStrCom(COM_HAND, TRUE, 0, "Send successfully");
}
}
else
{/*必须要发下面这个字节否则不能从丢失'>'时而引起的错误中恢复过来*/
writeStrCom(COM_CDMA, TRUE, 1, "\x1A");//
writeStrCom(COM_CDMA, TRUE, 1, "\x1A");//
}
dly20ms(50);
}
/*************************************************
函数功能:读短信息
输入参数:index:短信的序号
pMsg: 保存短信息的各种信息,包括信息中心号码、服务时间
发送方的电话号码、信息协议、编码方式、信息内容
返回值:返回信息字串的长度
***************************************************/
u8 ReadMessage(u8 index,SM_PARAM* pMsg)
{
u8 cLength[5];
u8 buf[256]; // 内部用的缓冲区
u8 *ptr=NULL;
u8 data;
u16 length;
int2BcdAscStr((char*)cLength, index, 3);//序号转成转成十进制字符串
cLength[3] = 0x0D;//<CR>
cLength[4] = 0;
memset(g_CdmaRcvBuf,0x0,MSGSIZE_CDMA);
g_GsmCnt=0;
writeStrCom(COM_CDMA, FALSE, 0, "AT+CMGR=");
writeStrCom(COM_CDMA, FALSE, 0, cLength);
dly20ms(150);
ptr = searchStr(g_CdmaRcvBuf, "+CMS ERROR", g_GsmCnt);
if(!ptr)
{
pMsg->index=index;
ptr=g_CdmaRcvBuf;
ptr=strstr(ptr, "+CMGR:");
ptr=strstr(ptr,"\r\n");/*查找下一行*/
ptr+=2;//跳过\r\n,指向下一行
String2Bytes(ptr,&data,2);//取长度
data=(data-1)*2;//取中心地址号码串的长度
ptr+=4; //跳到中心号码串
gsmSerializeNumbers(ptr, pMsg->SCA, data);//还原信息中心号码
ptr+=data;
// TPDU段基本参数、回复地址等
ptr+=2; //此处认为都有回复地址,若出现没有回复地址的情况需要在此判断
String2Bytes(ptr,&data,2);//取长度
if(data&1) data++; //调整奇偶性
ptr+=4; //跳到回复电话号码串处
gsmSerializeNumbers(ptr, pMsg->TPA, data);//还原电话号码
ptr+=data;
// TPDU段协议标识、编码方式、用户信息等
String2Bytes(ptr,&pMsg->TP_PID,2);// 取协议标识(TP-PID)
ptr+=2;
String2Bytes(ptr,&pMsg->TP_DCS,2); // 取编码方式(TP-DCS)
ptr+=2;
gsmSerializeNumbers(ptr, pMsg->TP_SCTS, 14); // 服务时间戳字符串(TP_SCTS)
ptr+=14;
String2Bytes(ptr,&data,2);//取信息长度
ptr+=2;
if(pMsg->TP_DCS==0) //7位解码
{
length = String2Bytes(ptr, buf, data & 7?(int)data * 7 / 4 + 2:(int)data * 7 / 4); // 格式转换
gsmDecode7bit(buf, pMsg->TP_UD, length); // 转换到TP-DU
length = data;
}
else
{ // unicode 解码,程序暂缺
strcpy(pMsg->TP_UD,ptr);
//length = String2Bytes(ptr, pMsg->TP_UD, data * 2); // 格式转换
//length = gsmDecodeUcs2(buf, pMsg->TP_UD, length); // 转换�