由于要讲详细点,所以要分开来讲,不便之处敬请原谅!
既然SIM908自带GSM功能,电话功能实现了,为何不加上短信功能呢,也许到时操作发短信有点复杂。
短信这部分有点复杂,因为采用管理员管理模式,要在AT24C02中保存短信的数量,还要保存短信的性质,接着还要判断短信内容是命令还是非命令,发短信的界面也复杂,T9键盘的建立,字符的索引。。。。。。。一堆代码
接收到短信后,使能短信读取标志位,然后进入主函数
while (1)
{
GUI_Delay (20);
if(FlagNewMessage)
{
// USART_printf(USART6,"%d\r\n",Messagenum);
FlagNewMessage=0;
if(SIM_ERR_NONE == Sim908_ReadMessage(Messagenum,1))
{
USART_printf(USART6,"read cmd\r\n");
Messagebuff[Messagenum] = MESSAGE_CMD;
//I2C_EE_BufferWrite(Messagebuff, MESSAGE_FLASH_ADDR, sizeof(Messagebuff));
}
else
{
USART_printf(USART6,"read notcmd\r\n");
Messagebuff[Messagenum] = MESSAGE_NOTCMD;
}
ee_WriteBytes(Messagebuff, 1,sizeof(Messagebuff));//保存短信属性
}
}
进入主函数后就开始进入Sim908_ReadMessage函数读取短信内容了
u8 Sim908_ReadMessage(u8 addr, u8 mode)
{
u8 res = SIM_ERR_OTHER;
u8 messageaddr[20];
u8 address[3];
u8 *p1 = NULL;
u8 *p2 = NULL;
// USART_printf(USART6,"addr=%d\r\n",addr);
// USART_printf(USART6,"mode=%d\r\n",mode);
if(addr > 50 || addr < 1)
{
return 0;
}
if(addr < 10)
{
address[0] = addr + '0';
address[1] = '\0';
}
else
{
address[0] = addr/10 + '0';
address[1] = addr%10 + '0';
address[2] = '\0';
}
// USART_printf(USART6,"address=%s\r\n",address);
Sim908_SendCmd("AT+CMGF=1", "OK", 50);
Sim908_SendCmd("AT+CSCS=\"GSM\"", "OK", 50);
sprintf((char *)messageaddr, "AT+CMGR=%s", address);
if(SIM_ERR_NONE == Sim908_SendCmd(messageaddr, "+CMGR:", 100))
{
// USART_printf(USART6, "\r\n %s \r\n",GSM_RX_BUFF);
p1 = (u8 *)strstr((char *)GSM_RX_BUFF, ",");
p2 = (u8 *)strstr((char *)(p1+2), "\"");
p2[0] = '\0'; //添加结束符
strcpy((char *)MES_STR.Phonenum, (char *)(p1+2)); //保存电话号码
// USART_printf(USART6, "\r\n %s\r\n",MES_STR.Phonenum);
p1 = (u8 *)strstr((char *)(p2+1), ":");
p2 = (u8 *)strstr((char *)(p1), "+");
p2[0] = '\0';
strcpy((char *)MES_STR.Time, (char *)(p1-11)); //保存接收时间
// USART_printf(USART6, "\r\n %s\r\n",MES_STR.Time);
p1 = (u8 *)strstr((char *)(p2+1), "\r");
p2 = (u8 *)strstr((char *)(p1+1), "\r");
p2[0] = '\0';
strcpy((char *)MES_STR.Message, (char *)(p1+2));
USART_printf(USART6, "\r\n %s\r\n",MES_STR.Message);
USART_printf(USART6, "\r\n judge message \r\n");
if(SIM_ERR_NONE == Sim908_JudgeMessage( &MES_STR ))
{
res = SIM_ERR_NONE;
}
}
return res;
}
保存了短信的发送电话、时间。就对短信内容进行判断了。
/*
函数名:Sim908_JudgeMessage
描述: 判断短信内容
输入: u8 *message 保存短信内容的数组指针
输出: 无
*/
u8 Sim908_JudgeMessage(Messagestr *mes_str)
{
u8 res = SIM_ERR_OTHER;
if(NULL != strstr((char *)mes_str->Message, "DW"))
{
USART_printf(USART6, "\r\n DW \r\n");
Sim908_GPSSet(1);
GPS_IT_FLAG =1;
DW_FLAG=1;
Sim908_SendMessage(ADMINISTRATORPHONE, "Please waitting....",1);
/* if(SIM_ERR_NONE == Sim908_GetGPSInformation(&GPS_STR, NULL))
{
Sim908_GPSConvert(GPS_STR.Latitude, GPS_STR.Longtude, LOCATION);
Sim908_SendMessage(ADMINISTRATORPHONE, (char *)LOCATION, 1);
}
else
{
Sim908_SendMessage(ADMINISTRATORPHONE, "try again",1);
} */
res = SIM_ERR_NONE;
}
else if(NULL != strstr((char *)mes_str->Message, "GMM"))
{
u8 *p1 = NULL;
USART_printf(USART6, "\r\n GMM \r\n");
p1 = strstr((char *)mes_str->Phonenum, ADMINISTRATORPHONE);
if(NULL == p1)
{
res = SIM_ERR_MESCMD;
//USART_printf(USART1, "\r\n phonenum is %s \r\n", mes_str->Phonenum);
//USART_printf(USART1, "\r\n ADMINISTRATORPHONE is %s \r\n", ADMINISTRATORPHONE);
Sim908_SendMessage((char *)(mes_str->Phonenum), "you are not the administrator", 1);
}
else
{
char mm[30];
u8 *p1 = NULL;
u8 *p2 = NULL;
p1 = strstr((char *)mes_str->Message, "\"");
p2 = strstr((char *)(p1+1), "\"");
p2[0] = 0;
strcpy(PASSWORD, (char *)(p1+1));
//USART_printf(USART1, "\r\n new PASSWORD is $%s$ \r\n", PASSWORD);
//I2C_EE_BufferWrite(PASSWORD, PASSWORD_FLASH_ADDR, sizeof(PASSWORD));
// Flash_Write(PASSWORD_FLASH_ADDR, (u16 *)PASSWORD, sizeof(PASSWORD));
sprintf(mm, "new password is %s", PASSWORD);
Sim908_SendMessage((char *)ADMINISTRATORPHONE, mm, 1);
res = SIM_ERR_NONE;
}
}
else if(NULL != strstr((char *)mes_str->Message, "MM"))
{
u8 *p1 = NULL;
u8 *p2 = NULL;
p1 = strstr((char *)mes_str->Message, "\"");
p2 = strstr((char *)(p1+1), "\"");
p2[0] = 0;
//USART_printf(USART1, "\r\n GLY \r\n");
if(NULL != strstr(PASSWORD, p1+1))
{
//USART_printf(USART1, "\r\n password is right \r\n");
strcpy(ADMINISTRATORPHONE, (char *)mes_str->Phonenum);
//USART_printf(USART1, "\r\n copy phonenum \r\n");
//I2C_EE_BufferWrite(ADMINISTRATORPHONE, PHONE_FLASH_ADDR, sizeof(ADMINISTRATORPHONE));
// Flash_Write(PHONE_FLASH_ADDR, (u16 *)ADMINISTRATORPHONE, sizeof(ADMINISTRATORPHONE));
//USART_printf(USART1, "\r\n i2c write \r\n");
Sim908_SendMessage(ADMINISTRATORPHONE, "new administrator has builded", 1);
res = SIM_ERR_NONE;
}
else
{
// USART_printf(USART1, "\r\n password is error \r\n");
Sim908_SendMessage((char *)(mes_str->Phonenum), "password ERROR", 1);
res = SIM_ERR_MESCMD;
}
}
else if(NULL != strstr((char *)mes_str->Message, "GPRS"))
{
u8 *p1 = NULL;
u8 *p2 = NULL;
USART_printf(USART6, "\r\n GPRS \r\n");
GPRSFLAG=1;
GPS_IT_FLAG =1;
Sim908_GPSSet(1);
p1 = (u8 *)strstr((char *)mes_str->Message, "IP");
if(NULL == p1)
{
// USART_printf(USART1, "\r\n IP eeror \r\n");
res = SIM_ERR_MESCMD;
}
p2 = (u8 *)strstr((char *)mes_str->Message, "COM");
if(NULL == p2)
{
// USART_printf(USART1, "\r\n COM eeror \r\n");
res = SIM_ERR_MESCMD;
}
p1 = (u8 *)strstr((char *)mes_str->Message, "\"");
p2 = (u8 *)strstr((char *)(p1+1), "\"");
Sim908_CopyStr((u8 *)IPADDR, p1+1, p2);
p1 = (u8 *)strstr((char *)(p2+1), "\"");
p2 = (u8 *)strstr((char *)(p1+1), "\"");
Sim908_CopyStr((u8 *)COM, p1+1, p2);
Sim908_TCPClose();
Sim908_TCPConnect((u8 *)IPADDR, (u8 *)COM);
res = SIM_ERR_NONE;
}
else if(NULL != strstr((char *)mes_str->Message, "CLOSE"))
{
GPS_IT_FLAG=0;
GPRSFLAG=0;
Sim908_GPSSet(0);
Sim908_TCPClose();
}
return res;
}
判断短信内容是否包含以上字符串 DW 、GMM 、MM 、GPRS 、CLOSE
DW为定位的指令、GMM为改密码、MM为新建管理员、GPRS为建立TCP连接,CLOSE为关闭(以上指令均为管理员号码发送的,其他号码发送无效)
短信界面为短信查看界面,点击屏幕下方的上下键即可对短信的切换,屏幕中央显示短信的信息(命令短信不显示,作为后台应用),点击左上方的按键即可新建短信。
此为发送短信主界面,数字键与字母键切换,字母大小写切换,信息发送成功后会弹出提示框,提示发送成功,否则提示发送失败。
-------------------------------------------------------------------------------温柔华丽的分割线----------------------------------------------------------
|