#pragma once
#include "SIM_Card.h"
class SIM_ReadMessage: public SIM_Card
{
public:
SIM_ReadMessage(void);
SIM_ReadMessage(Comm* pC);
~SIM_ReadMessage(void);
// 读取短消息,仅发送命令,不读取应答
// 用+CMGL代替+CMGR,可一次性读出全部短消息
int gsmReadMessageList(SM_PARAM* pMsg, int& iMsg_Count);
int gsmParseMessageList(SM_PARAM* pMsg, SM_BUFF* pBuff);
};
#include "StdAfx.h"
#include "SIM_ReadMessage.h"
SIM_ReadMessage::SIM_ReadMessage(void)
{
}
SIM_ReadMessage::SIM_ReadMessage(Comm* pC):SIM_Card(pC)
{
}
SIM_ReadMessage::~SIM_ReadMessage(void)
{
}
// 读取短消息,仅发送命令,不读取应答
// 用+CMGL代替+CMGR,可一次性读出全部短消息
int SIM_ReadMessage::gsmReadMessageList(SM_PARAM* pMsg, int& iMsg_Count)
{
pComm->WriteComm("AT+CMGF=0/r", 20);
SM_BUFF Buff;
int k = gsmGetResponse(&Buff, 10, "OK","ERROR");
if( GSM_OK == k)
{
pComm->WriteComm("AT+CMGL=4/r", 20);
k = gsmGetResponse(&Buff, 10, "OK","ERROR");
if( GSM_OK == k)
{
iMsg_Count = gsmParseMessageList(pMsg, &Buff);
}
}
return k;
}
int SIM_ReadMessage::gsmParseMessageList(SM_PARAM* pMsg, SM_BUFF* pBuff)
{
int nMsg; // 短消息计数值
char* ptr; // 内部用的数据指针
nMsg = 0;
ptr = pBuff->data;
// 循环读取每一条短消息, 以"+CMGL:"开头
while((ptr = strstr(ptr, "+CMGL:")) != NULL)
{
ptr += 6; // 跳过"+CMGL:", 定位到序号
sscanf(ptr, "%d", &pMsg->index); // 读取序号
// TRACE(" index=%d/n",pMsg->index);
ptr = strstr(ptr, "/r/n"); // 找下一行
if (ptr != NULL)
{
ptr += 2; // 跳过"/r/n", 定位到PDU
gsmDecodePdu(ptr, pMsg); // PDU串解码
//cout<<"序号 - "<<pMsg->index<<endl;
//cout<<"回复号码 - "<<pMsg->TPA<<endl;
//cout<<"短信中心号码 - "<<pMsg->SCA<<endl;
//cout<<"发送时间 - "<<pMsg->TP_SCTS<<endl;
//cout<<"短信内容 - "<<pMsg->TP_UD<<endl;
//cout<<endl<<endl;
pMsg++; // 准备读下一条短消息
nMsg++; // 短消息计数加1
}
}
return nMsg;
}