【TI低功耗设计大赛】智能手表之SIM908通信处理
[复制链接]
用到sim908进行拨话,与短信、GPS、GPRS服务,需要对模块返回来的信息进行处理,
源程序如下:
#include "sim908.h"
#include "stdio.h"
#include "string.h"
#include "usart.h"
#include "ff.h"
#include "includes.h"
#include "rtc.h"
#include "i2c_ee.h"
#include "includes.h"
u8 Sim_Battery[4]; //电池
u8 Sim_RSSI[3]; //信号强度
u8 Sim_Operator = 0; //运营商 1为中国移动
u8 GSM_Response[10];
u8 RingNumber[20]; //来电号码
u8 Messagenum; //保存短信数目
u8 Messagebuff[50]; //保存短信状态 0 空 1 未读 2 命令短信 3非命令短信
char ADMINISTRATORPHONE[20] = "13729058953";
char IPADDR[20] = "220.170.79.210";
char COM[6] = "40487";
char PASSWORD[10] = "0000000000";
u8 LOCATION[30];
Messagestr MES_STR;
GPSStruct GPS_STR;
/*
函数名:Sim908_IOConfig
描述: 复位
输入: 无
输出: 无
*/
void Sim908_IOConfig( void )
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Sim908_Reset( void )
{
OS_ERR err;
GPIO_SetBits(GPIOA, GPIO_Pin_1);
OSTimeDlyHMSM( 0, 0, 2, 0, OS_OPT_TIME_HMSM_STRICT, &err);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
/*
函数名:Sim908_SendCmd
描述: 发送at命令,并检验是否收到想要的响应
输入: u8 *cmd 要发送的命令
u8 *ack 期待的响应
u16 waittime 等待时间 单位10ms
u8 *pBuff 接收响应的缓存区地址 为NULL则不保存响应数据
输出:
*/
u8 Sim908_SendCmd(u8 *cmd, u8 *ack, u16 waittime, u8 *pBuff)
{
u8 res = SIM_ERR_OTHER;
u8 *pdata = NULL;
CPU_TS ts;
OS_ERR err;
OS_MSG_SIZE msg_size;
OSMutexPend(&Mutex_USART2, 30000, OS_OPT_PEND_BLOCKING, &ts, &err);
if( (u32)cmd <= 0xff ) //用于发送结束标志 0x1A
{
USART2->DR = (u32)cmd;
}
else
{
printf("%s\r\n", ( char *)cmd);
}
if(ack != NULL)
{
strcpy((char *)&GSM_Response[0], (char *)ack);
while(waittime--)
{
//OSSemPend(&Sem_GSMResponse, 10, OS_OPT_PEND_BLOCKING, &ts, &err); //等待信号量
pdata = OSQPend(&Q_GSMResponse, 10, OS_OPT_PEND_BLOCKING, &msg_size, &ts, &err);
if(err == OS_ERR_NONE)
{
if((NULL != pBuff)&&(NULL != pdata))
{
strcpy((char *)pBuff, (char *)pdata);
}
OSMemPut(&MyPartition, pdata, &err);
res = SIM_ERR_NONE;
break;
}
}
if(err != OS_ERR_NONE)
{
res = SIM_ERR_TIMEOUT;
}
}
OSMutexPost(&Mutex_USART2, OS_OPT_POST_NONE, &err);
return res;
}
/*
函数名:Sim908_ReadMessage
描述: 读短信
输入: u8 addr 短信位置
u8 mode 1: 英文短信 0:中文短信
输出: 0 发送失败 1 发送成功
*/
u8 Sim908_ReadMessage(u8 addr, u8 mode)
{
u8 res = SIM_ERR_OTHER;
u8 messageaddr[20];
u8 address[3];
u8 *p1 = NULL;
u8 *p2 = NULL;
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';
}
if( mode ) //英文短信
{
u8 databuff[350];
Sim908_SendCmd("AT+CMGF=1", "OK", 100, NULL);
Sim908_SendCmd("AT+CSCS=\"GSM\"", "OK", 100, NULL);
sprintf((char *)messageaddr, "AT+CMGR=%s", address);
if(SIM_ERR_NONE == Sim908_SendCmd(messageaddr, "+CMGR:", 250, &databuff[0]))
{
p1 = (u8 *)strstr((char *)&databuff[0], ",");
p2 = (u8 *)strstr((char *)(p1+2), "\"");
p2[0] = '\0'; //添加结束符
strcpy((char *)MES_STR.Phonenum, (char *)(p1+2)); //保存电话号码
p1 = (u8 *)strstr((char *)(p2+1), "/");
p2 = (u8 *)strstr((char *)(p1), "+");
p2[0] = '\0';
strcpy((char *)MES_STR.Time, (char *)(p1-2)); //保存接收时间
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));
if(SIM_ERR_NONE == Sim908_JudgeMessage( &MES_STR ))
{
res = SIM_ERR_NONE;
}
}
}
else //中文短信
{
u8 databuff[350];
u8 number[80];
u8 messtr[256];
Sim908_SendCmd("AT+CMGF=1", "OK", 100, NULL);
Sim908_SendCmd("AT+CSCS=\"UCS2\"", "OK", 100, NULL);
sprintf((char *)messageaddr, "AT+CMGR=%s", address);
if(SIM_ERR_NONE == Sim908_SendCmd(messageaddr, "+CMGR:", 300, &databuff[0]))
{
p1 = (u8 *)strstr((char *)databuff, ",");
p2 = (u8 *)strstr((char *)(p1+2), "\"");
p2[0] = '\0'; //添加结束符
Sim908_UCS2GBK((char *)number, (char *)(p1+2));
strcpy((char *)MES_STR.Phonenum, (char *)number); //保存电话号码
//LCD_ShowChEnStr(10, 10, MES_STR.Phonenum, RED, 16, 1);
p1 = (u8 *)strstr((char *)(p2+1), "/");
p2 = (u8 *)strstr((char *)(p1), "+");
p2[0] = '\0';
strcpy((char *)MES_STR.Time, (char *)(p1-2)); //保存接收时间
p1 = (u8 *)strstr((char *)(p2+1), "\r");
p2 = (u8 *)strstr((char *)(p1+1), "\r");
p2[0] = '\0';
Sim908_UCS2GBK((char *)messtr, (char *)(p1+2));
strcpy((char *)MES_STR.Message, (char *)messtr);
//LCD_ShowChEnStr(10, 40, MES_STR.Message, RED, 16, 1);
res = SIM_ERR_NONE;
}
}
return res;
}
/*
函数名:Sim908_SendMessage
描述: 发送短信
输入: char *number 要发送的电话号码
char *message 要发送的短信内容
u8 mode 1为英文短信
0为中文短信
输出: 1:发送成功
0:发送失败
*/
u8 Sim908_SendMessage(char *number, char *message, u8 mode)
{
char phonenumber[100];
u8 res = SIM_ERR_OTHER;
if( mode ) //英文短信
{
Sim908_SendCmd("AT+CMGF=1", "OK", 100, NULL);
Sim908_SendCmd("AT+CSCS=\"GSM\"", "OK", 100, NULL);
sprintf(phonenumber, "AT+CMGS=\"%s\"", number); //合并命令
if(SIM_ERR_NONE == Sim908_SendCmd((u8 *)phonenumber, ">", 300, NULL))
{
USART_printf(USART2, "%s", message); //发送内容
if(SIM_ERR_NONE == Sim908_SendCmd((u8 *)0x1A, "+CMGS:", 1000, NULL))
{
res = SIM_ERR_NONE;
}
}
}
else //中文短信
{
char ucs2buff[256];
Sim908_SendCmd("AT+CMGF=1", "OK", 100, NULL);
Sim908_SendCmd("AT+CSCS=\"UCS2\"", "OK", 100, NULL);
Sim908_SendCmd("AT+CSMP=17,0,2,25", "OK", 100, NULL);
Sim908_GBK2UCS(ucs2buff, number);
sprintf(phonenumber, "AT+CMGS=\"%s\"", ucs2buff); //合并命令
if(SIM_ERR_NONE == Sim908_SendCmd((u8 *)phonenumber, ">", 300, NULL))
{
Sim908_GBK2UCS(ucs2buff, message);
USART_printf(USART2, "%s", ucs2buff); //发送内容
if(SIM_ERR_NONE == Sim908_SendCmd((u8 *)0x1A, "+CMGS:", 1000, NULL))
{
res = SIM_ERR_NONE;
}
}
}
return res;
}
/*
函数名:Sim908_Call
描述: 打电话
输入: u8 * number //电话号码
输出: 0 拨打失败 1 拨打成功
*/
u8 Sim908_Call( u8 *number )
{
char phonenum[30];
u8 res = SIM_ERR_OTHER;
sprintf(phonenum, "ATD%s;", number);
if(SIM_ERR_NONE == Sim908_SendCmd(( u8 *)phonenum, NULL, 500, NULL))
{
res = SIM_ERR_NONE;
}
return res;
}
/*
函数名:Sim908_Hangup
描述: 挂电话
输入: 无
输出: 0 操作失败 1 操作成功
*/
u8 Sim908_Hangup( void )
{
u8 res = SIM_ERR_OTHER;
if(SIM_ERR_NONE == Sim908_SendCmd("ATH", "OK", 100, NULL))
{
res = SIM_ERR_NONE;
}
return res;
}
/*
函数名:Sim908_AnswerPhone
描述: 接电话
输入: 无
输出:
*/
u8 Sim908_AnswerPhone( void )
{
u8 res = SIM_ERR_OTHER;
if(SIM_ERR_NONE == Sim908_SendCmd("ATA", "OK", 100, NULL))
{
res = SIM_ERR_OTHER;
}
return res;
}
/*
函数名:Sim908_DeleteMes
描述: 删除短信
输入: u8 addr 要删除的短信编号 1~50 (实际最大为16)
输出: 0 删除失败 1 删除成功
*/
u8 Sim908_DeleteMes(u8 addr)
{
u8 address[3];
u8 deletecmd[15];
u8 res = SIM_ERR_OTHER;
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';
}
sprintf((char *)deletecmd, "AT+CMGD=%s", (char *)address);
if(SIM_ERR_NONE == Sim908_SendCmd(deletecmd, "OK", 100, NULL))
{
res = SIM_ERR_NONE;
}
return res;
}
/*
函数名:Sim908_GPSSet
描述: 开关GPS
输入: u8 status 1:开GPS 0:关GPS
输出: 1:操作成功 0:操作失败
*/
u8 Sim908_GPSSet( u8 status )
{
u8 res = SIM_ERR_OTHER;
if( status )
{
if(SIM_ERR_NONE == Sim908_SendCmd("AT+CGPSPWR=1", "OK", 100, NULL))
{
if(SIM_ERR_NONE == Sim908_SendCmd("AT+CGPSRST=1", "OK", 100, NULL))
{
res = SIM_ERR_NONE;
}
}
}
else
{
if(SIM_ERR_NONE == Sim908_SendCmd("AT+CGPSPWR=0", "OK", 100, NULL))
{
res = SIM_ERR_NONE;
}
}
return res;
}
/*
函数名:Sim908_TCPConnect
描述: 建立tcp连接
输入: u8 *ipaddr 要连接的ip地址
u8 *com 要连接的端口
输出: 1 连接成功
0 连接失败
*/
u8 Sim908_TCPConnect(u8 *ipaddr, u8 *com)
{
u8 cmd[50];
OS_ERR err;
u8 res = SIM_ERR_OTHER;
sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"", ipaddr, com);
if(SIM_ERR_NONE != Sim908_SendCmd("AT+CGCLASS=\"B\"", "OK", 200, NULL))
{
return SIM_ERR_CGCLASS;
}
if(SIM_ERR_NONE != Sim908_SendCmd("AT+CGDCONT=1,\"IP\",\"CMNET\"", "OK", 200, NULL))
{
return SIM_ERR_CGDCONT;
}
if(SIM_ERR_NONE != Sim908_SendCmd("AT+CGATT=1", "OK", 200, NULL))
{
return SIM_ERR_CGATT;
}
if(SIM_ERR_NONE != Sim908_SendCmd("AT+CIPCSGP=1,\"CMNET\"", "OK", 200, NULL))
{
return SIM_ERR_CIPCSGP;
}
if(SIM_ERR_NONE != Sim908_SendCmd("AT+CLPORT=\"TCP\",\"2000\"", "OK", 200, NULL))
{
return SIM_ERR_CLPORT;
}
if(SIM_ERR_NONE != Sim908_SendCmd(cmd, "CONNECT OK", 1000, NULL))
{
return SIM_ERR_CIPSTART;
}
else
{
OSFlagPost(&GPRSFlagGrp, FlagGPRSConnect, OS_OPT_POST_FLAG_SET, &err);
res = SIM_ERR_NONE;
}
return res;
}
/*
函数名:Sim908_TCPSendData
描述: 在TCP建立连接成功后 调用此函数发送数据
输入: u8 *data 要发送的数据
输出: 1 发送成功
0 发送失败
*/
u8 Sim908_TCPSendData( u8 * data)
{
u8 res = SIM_ERR_NONE;
if(NULL == data)
{
return 0;
}
if(SIM_ERR_NONE == Sim908_SendCmd("AT+CIPSEND", ">", 1000, NULL))
{
//Sim908_SendCmd(data, NULL, 1);
USART_printf(USART2, "%s", data);
if(SIM_ERR_NONE == Sim908_SendCmd((u8 *)0x1A, "SEND OK", 1000, NULL))
{
res = SIM_ERR_NONE;
}
}
else
{
res = SIM_ERR_CGATT;
}
return res;
}
/*
函数名:Sim908_TCPClose
描述: 关闭TCP连接
输入: 无
输出: 1 操作成功
0 操作失败
*/
u8 Sim908_TCPClose( void )
{
OS_ERR err;
u8 res = SIM_ERR_OTHER;
Sim908_SendCmd("AT+CIPCLOSE=1", "CLOSE OK", 100, NULL);
if(SIM_ERR_NONE == Sim908_SendCmd("AT+CIPSHUT", "SHUT OK", 100, NULL))
{
//OSFlagPost(&GPRSFlagGrp, FlagGPRSConnect, OS_OPT_POST_FLAG_CLR, &err);
res = SIM_ERR_NONE;
}
return res;
}
/*
函数名:Sim908_char2hex
描述: 把字符转换成16进制字符
输入: char ch 字符
输出: 16进制数值
*/
u8 Sim908_char2hex(char ch)
{
if(ch>='0'&&ch<='9')
{
return ch-'0';
}
else if(ch>='A'&&ch<='F')
{
return (ch-'A'+10);
}
else if(ch>='a'&&ch<='f')
{
return (ch-'a'+10);
}
return 0;
}
/*
函数名:Sim908_hex2char
描述: 将一个16进制值转换为对应的字符
输入: u8 hex 16进制值 0~F
输出: 对应的字符
*/
u8 Sim908_hex2char(u8 hex)
{
if(hex <= 9)
{
return (hex + '0');
}
else if(hex >= 10 && hex <= 15)
{
return (hex - 10 + 'A');
}
return '0';
}
/*
函数名:Sim908_GBK2UCS
描述: 将GBK编码转换为UCS编码
输入: char *ucs ucs编码缓冲区指针
char *gbk GBK编码缓冲区指针
输出: 无
*/
void Sim908_GBK2UCS(char *ucs, char *gbk)
{
u16 temp;
u8 buff[2];
while(*gbk != 0)
{
if(*gbk < 0x81) //非汉字
{
temp = (u16)ff_convert((WCHAR)*gbk, 1);
gbk++;
}
else //汉字
{
buff[1] = *gbk++;
buff[0] = *gbk++;
temp = (u16)ff_convert((WCHAR)*(u16 *)buff, 1);
}
*ucs++ = Sim908_hex2char((temp >> 12)&0x0F);
*ucs++ = Sim908_hex2char((temp >> 8)&0x0F);
*ucs++ = Sim908_hex2char((temp >> 4)&0x0F);
*ucs++ = Sim908_hex2char(temp & 0x0F);
}
*ucs = 0;
}
/*
函数名:Sim908_UCS2GBK
描述: unicode码转换为GBK码
输入: char *ucs 保存unicode的缓冲区指针
char *gbk 保存gbk码的缓冲区指针
输出: 无
*/
void Sim908_UCS2GBK(char *gbk, char *ucs)
{
u16 temp;
u8 buff[2];
while(*ucs != 0)
{
buff[1] = Sim908_char2hex(*ucs++) * 16;
buff[1] += Sim908_char2hex(*ucs++);
buff[0] = Sim908_char2hex(*ucs++) * 16;
buff[0] += Sim908_char2hex(*ucs++);
temp = (u16)ff_convert((WCHAR)*(u16 *)buff, 0);
if(temp < 0x80)
{
*gbk = temp;
gbk++;
}
else
{
*(u16 *)gbk = swap16(temp);
gbk += 2;
}
}
*gbk = 0;
}
/*
函数名:Sim908_JudgeMessage
描述: 判断短信内容
输入: u8 *message 保存短信内容的数组指针
输出: 无
*/
u8 Sim908_JudgeMessage(Messagestr *mes_str)
{
u8 res = SIM_ERR_OTHER;
if(NULL != strstr((char *)mes_str->Message, "DW"))
{
if(SIM_ERR_NONE == Sim908_GetGPSInformation(&GPS_STR, NULL))
{
Sim908_GPSConvert(GPS_STR.Latitude, GPS_STR.Longtude, LOCATION);
Sim908_SendMessage(ADMINISTRATORPHONE, (char *)LOCATION, 1);
}
res = SIM_ERR_NONE;
}
else if(NULL != strstr((char *)mes_str->Message, "GMM"))
{
u8 *p1 = NULL;
//USART_printf(USART1, "\r\n 修改密码 \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));
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 right is $%s$ \r\n", PASSWORD);
// USART_printf(USART1, "\r\n error is $%s$ \r\n", p1+1);
if(NULL != strstr(PASSWORD, p1+1))
{
// USART_printf(USART1, "\r\n password is right \r\n");
strcpy(ADMINISTRATORPHONE, (char *)mes_str->Phonenum);
I2C_EE_BufferWrite(ADMINISTRATORPHONE, PHONE_FLASH_ADDR, sizeof(ADMINISTRATORPHONE));
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;
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;
}
return res;
}
/*
函数名:Sim908_CopyStr
描述: 将指针former和指针later中的内容复制到des源地址
输入: u8 * str 源数组
u8 Ch 指定的元素
u8 * des
输出: 操作成功则返回Ch在str的指针,失败则返回NULL
*/
void Sim908_CopyStr(u8 *des, u8 *former, u8 *later)
{
if(former > later)
{
return;
}
while(former != later)
{
*des = *former;
former++;
des++;
}
*des = '\0';
}
/*
函数名:Sim908_GetGPSInformation
描述: 获取GPS的相关数据
输入: GPSStruct *gpsstruct 保存gps数据的结构体
u8 *str 保存gps数据的数组指针
输出: 1 操作成功 0 操作失败
*/
u8 Sim908_GetGPSInformation(GPSStruct *gpsstrucr, u8 *pdatabuff)
{
u8 *p1 = NULL;
u8 *p2 = NULL;
u8 res = SIM_ERR_OTHER;
u8 gpsdatabuff[100];
if(SIM_ERR_NONE == Sim908_SendCmd("AT+CGPSSTATUS?", "D Fix", 200, NULL))
{
if(SIM_ERR_NONE == Sim908_SendCmd("AT+CGPSINF=32", ",A,", 200, &gpsdatabuff[0]))
{
if(NULL != pdatabuff)
{
strcpy(pdatabuff, gpsdatabuff);
}
p1 = (u8 *)strstr((char *)gpsdatabuff, ",");
p2 = (u8 *)strstr((char *)(p1+1), ",");
Sim908_CopyStr(gpsstrucr->Time, p1+1, p2);
p1 = (u8 *)strstr((char *)(p2+1), ",");
p2 = (u8 *)strstr((char *)(p1+1), ",");
Sim908_CopyStr(gpsstrucr->Latitude, p1+1, p2+2);
p1 = p2 + 3;
p2 = (u8 *)strstr((char *)p1, ",");
Sim908_CopyStr(gpsstrucr->Longtude, p1, p2+2);
p1 = p2 + 3;
p2 = (u8 *)strstr((char *)p1, ",");
Sim908_CopyStr(gpsstrucr->Speed, p1, p2);
p1 = p2 + 1;
p2 = (u8 *)strstr((char *)p1, ",");
Sim908_CopyStr(gpsstrucr->Direction, p1, p2);
p1 = p2 + 1;
p2 = (u8 *)strstr((char *)p1, ",");
Sim908_CopyStr(gpsstrucr->Data, p1, p2);
res = SIM_ERR_NONE;
}
}
else
{
res = SIM_ERR_GPSNOTFIX;
}
return res;
}
/*
函数名:Sim908_GPSConvert
描述: GPS单位转换 GPS读出的数据单位是 (ddmm.mmmm) 要转换为d
输入: u8 *latitude 转换前的纬度
u8 *longtude 转换前的经度
u8 *location 转换后的经纬度信息
输出: 无
*/
void Sim908_GPSConvert(u8 *latitude, u8 *longtude, u8 *location )
{
u8 *p1 = NULL;
u8 *p2 = NULL;
u32 data = 0;
u8 i = 0;
u8 j = 0;
u8 a = 0;
p1 = (u8 *)strstr((char *)latitude, ".");
a = p1 - latitude;
if(a == 5)
{
for(j=0; j<3; j++)
{
location[i] = latitude[i];
i++;
}
}
if(a == 4)
{
for(j=0; j<2; j++)
{
location[i] = latitude[i];
i++;
}
}
if(a == 3)
{
for(j=0; j<1; j++)
{
location[i] = latitude[i];
i++;
}
}
location[i] = '.';
data = 0;
data += (*(p1 - 2) - '0') * 10000000;
data += (*(p1 - 1) - '0') * 1000000;
data += (*(p1 + 1) - '0') * 100000;
data += (*(p1 + 2) - '0') * 10000;
data += (*(p1 + 3) - '0') * 1000;
data += (*(p1 + 4) - '0') * 100;
data += (*(p1 + 5) - '0') * 10;
data += (*(p1 + 6) - '0') ;
data /= 60;
for(j=6; j>0;j--)
{
location[i+j] = data%10 + '0' ;
data /= 10;
}
if(strstr((char *)latitude, "N"))
{
location[i+7] = 'N';
location[i+8] = ' ';
}
if(strstr((char *)latitude, "S"))
{
location[i+7] = 'N';
location[i+8] = ' ';
}
i += 9;
p2 = (u8 *)strstr((char *)longtude, ".");
a = p2 - longtude;
if(a == 5)
{
for(j=0; j<3; j++)
{
location[i] = longtude[j];
i++;
}
}
if(a == 4)
{
for(j=0; j<2; j++)
{
location[i] = longtude[j];
i++;
}
}
if(a == 3)
{
for(j=0; j<1; j++)
{
location[i] = longtude[j];
i++;
}
}
location[i] = '.';
data = 0;
data += (*(p2 - 2) - '0') * 10000000;
data += (*(p2 - 1) - '0') * 1000000;
data += (*(p2 + 1) - '0') * 100000;
data += (*(p2 + 2) - '0') * 10000;
data += (*(p2 + 3) - '0') * 1000;
data += (*(p2 + 4) - '0') * 100;
data += (*(p2 + 5) - '0') * 10;
data += (*(p2 + 6) - '0') ;
data /= 60;
for(j=6; j>0;j--)
{
location[i+j] = data%10 + '0' ;
data /= 10;
}
if(strstr((char *)longtude, "W"))
{
location[i+7] = 'W';
location[i+8] = '\0';
}
if(strstr((char *)GPS_STR.Longtude, "E"))
{
location[i+7] = 'E';
location[i+8] = '\0';
}
}
/*
描述:在str1中查找str2,忽略str2中的不可显示字符
*/
u8 Sim908_CheckPassword(u8 *str1, u8 *str2)
{
u8 i = 0;
if((NULL == str1) || (NULL == str2))
{
return SIM_ERR_OTHER;
}
while( *str1 )
{
i = 0;
while( 1 )
{
if(str2[i] == 0)
{
return SIM_ERR_NONE;
}
if(str2[i] != str1[i])
{
break;
}
i++;
}
str1++;
}
return SIM_ERR_OTHER;
}
/*
函数名:Sim908_TimeAdjust
描述: 用gps获取的时间来校准RTC
输入: struct tm *tm_t //保存日历时间的结构体指针
输出: 无
*/
void Sim908_TimeAdjust(struct tm *tm_t)
{
tm_t->tm_sec = (GPS_STR.Time[4] - '0') * 10 + GPS_STR.Time[5] - '0';
tm_t->tm_min = (GPS_STR.Time[2] - '0') * 10 + GPS_STR.Time[3] - '0';
tm_t->tm_hour = (GPS_STR.Time[0] - '0') * 10 + GPS_STR.Time[1] - '0' + 8; //GPS为UTC时间 与中国时差为8
tm_t->tm_mday = (GPS_STR.Data[0] - '0') * 10 + GPS_STR.Data[1] - '0';
tm_t->tm_mon = (GPS_STR.Data[2] - '0') * 10 + GPS_STR.Data[3] - '0' - 1;
tm_t->tm_year = (GPS_STR.Data[4] - '0') * 10 + GPS_STR.Data[5] - '0' + 2000;
if(tm_t->tm_hour > 23)
{
tm_t->tm_hour -= 24;
tm_t->tm_mday++;
if(tm_t->tm_mon==0||tm_t->tm_mon==2||tm_t->tm_mon==4||tm_t->tm_mon==6||tm_t->tm_mon==7||\
tm_t->tm_mon==9||tm_t->tm_mon==11)
{
if(tm_t->tm_mday > 31)
{
tm_t->tm_mday = 1;
tm_t->tm_mon++;
if(tm_t->tm_mon > 11)
{
tm_t->tm_mon = 0;
tm_t->tm_year++;
}
}
}
else if(tm_t->tm_mon==4||tm_t->tm_mon==6||tm_t->tm_mon==9||tm_t->tm_mon==10)
{
if(tm_t->tm_mday > 30)
{
tm_t->tm_mday = 1;
tm_t->tm_mon++;
}
}
else
{
if((tm_t->tm_year%4==0&&tm_t->tm_year%100!=0)||(tm_t->tm_year%400)) //闰年
{
if(tm_t->tm_mday > 29)
{
tm_t->tm_mday = 1;
tm_t->tm_mon++;
}
}
else //非闰年
{
if(tm_t->tm_mday > 28)
{
tm_t->tm_mday = 1;
tm_t->tm_mon++;
}
}
}
}
RTC_SetCalendarTime( *tm_t );
}
|