📄 gsmmodem.cpp
字号:
DelayTimer(100);
ReadDataFromPort(m_pReadPool,iPoolLen,1);
if(strstr(m_pReadPool,">")!=NULL)
{
DelayTimer(100);
WriteDataToPort(m_pWritePool,nShortMsgLen);
DelayTimer(1000);
//AfxMessageBox(_T("发送短消息OK1"));
delete [] pTempPool;
delete [] pUnicode;
return 1;
}
else
{
strcpy(aTempPool,"发送短消息错");
// AfxMessageBox(aTempPool);
}
}
else
{
strcpy(aTempPool,"发送短消息错");
// AfxMessageBox(aTempPool);
}
//删除空间
delete[] pTempPool;
delete [] pUnicode;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
//功能:发送AT命令
//history name data remark
// wanfangjie 2002.09.26 create
//返回值:1 成功,0 失败
///////////////////////////////////////////////////////////////////////////////
int CGsmModem::SendATCmd(const char *strATCmd)
{
int nATCmdLen=0;
nATCmdLen=strlen(strATCmd);
WriteDataToPort(strATCmd,nATCmdLen);
DelayTimer(150);
return 1;
}
//////////////////////////////////////////////////////////////////
//功能:GSM信号的质量
//history name data remark
// wanfangjie 2003.04.14 create
//返回值:信号的质量(0~31)
///////////////////////////////////////////////////////////////////////
int CGsmModem::GsmSignalQuality()
{
int nSignalQuality=0;
int nBufLen=0;
char *pFirst=NULL;
ClearCommBuf(0);
//发送AT命令
SendATCmd("AT+CSQ\r");
nBufLen=GetCommEvent(0);
if(nBufLen>0)
{
ReadDataFromPort(m_pReadPool,nBufLen,1);
pFirst=strstr(m_pReadPool,"+CSQ");
if(pFirst!=NULL)
{
//调过+CSQ,冒号与空格读取信号质量
pFirst+=6;
if(pFirst[0]>='0'&&pFirst[0]<='9')
{
nSignalQuality=atoi(pFirst);
}
}
}
return nSignalQuality;
}
//////////////////////////////////////////////////////////////////
//功能:关闭通信串口
//history name data remark
// wanfangjie 2003.04.14 create
///////////////////////////////////////////////////////////////////////
int CGsmModem::ClosePort()
{
CloseHandle(m_hComm);
return 1;
}
///////////////////////////////////////////////////////////////////////////////
//功能:分析GSM返回的值
//history name date remark
// wanfangjie 2003.03.24 create
//返回值 1~10数传方面
// 11~20短消息方面 11,有新短消息
//
////////////////////////////////////////////////////////////////////////////////
SHORTMSGINFO* CGsmModem::ReadGsmData(int nDataLen)
{
int nMsgIndex=0;
//为短消息信息结构赋值
m_pShortMsgInfo->nMsgLen=0;
m_pShortMsgInfo->MsgData[0]=0;
m_pShortMsgInfo->nMsgType=0x00;
m_pShortMsgInfo->pSrcPhoneNum[0]=0;
m_pShortMsgInfo->pTime[0]=0;
m_pShortMsgInfo->nRecInfo=0;
//从串口中读数据
ReadDataFromPort(m_pReadPool,nDataLen,1);
#ifdef _SIEMENS
if(strstr(m_pReadPool,"RING")!=NULL)
{
m_pShortMsgInfo->nRecInfo=2;
return m_pShortMsgInfo;
}
if(strstr(m_pReadPool,"+CMGS")!=NULL)
{
m_bIsSendOver=1;
if(m_nTransNum==0)
{
m_pShortMsgInfo->nRecInfo=1;
}
}
//有新短消息在SM中
if(strstr(m_pReadPool,"+CMTI:")!=NULL)
{
nMsgIndex=ReadMsgIndex(m_pReadPool);
//从SIM卡中读短消息
ReadMsgFromSIM(nMsgIndex,m_pReadPool);
DecodeShortMsg(m_pReadPool,1);
//删除短消息
DeleteShortMsg(nMsgIndex);
return m_pShortMsgInfo;
}
else if(strstr(m_pReadPool,"+CMT")!=NULL)
{
DecodeShortMsg(m_pReadPool,0);
}
#else
char * pCurrPtr = strstr(m_pReadPool,"*ECAV:");
if(pCurrPtr!=NULL)
{
if(*(pCurrPtr+9) == '6')
{
g_GsmModem.DispInComingNum(g_strPhoneNum);
m_pShortMsgInfo->nRecInfo=2; //有拨入
}
else if(*(pCurrPtr+9) == '0')
{
//ZeroMemory(g_strPhoneNum, 32);
m_pShortMsgInfo->nRecInfo=3;//对方挂断
}
else if(*(pCurrPtr+9) == '3')
{
m_pShortMsgInfo->nRecInfo=4;//已接通
}
}
if(strstr(m_pReadPool,"+CMGS")!=NULL)
{
m_bIsSendOver=1;
if(m_nTransNum==0)
{
m_pShortMsgInfo->nRecInfo=1;
}
}
//有新短消息在SM中
if(strstr(m_pReadPool,"+CMTI:")!=NULL)
{
nMsgIndex=ReadMsgIndex(m_pReadPool);
//从SIM卡中读短消息
ReadMsgFromSIM(nMsgIndex,m_pReadPool);
DecodeShortMsg(m_pReadPool,1);
//删除短消息
DeleteShortMsg(nMsgIndex);
return m_pShortMsgInfo;
}
else if(strstr(m_pReadPool,"+CMT")!=NULL)
{
DecodeShortMsg(m_pReadPool,0);
}
//拨入时
//..RING....*ECAV: 1,6,1,,,"037163857820",161.........(有回车换行符)
//拨入不接对方挂断时
//.RING....RING....*ECAV: 1,0,1,08,222........
//拨入接通时
//..RING..ATA...OK....*ECAV: 1,3,1.
#endif
return m_pShortMsgInfo;
}
///////////////////////////////////////////////////////////////////////////
//功能:读取新消息在SM中的索引号
//history name date remark
// wanfangjie 2003.03.24 create
//返回值:新短消息的索引号
///////////////////////////////////////////////////////////////////////////
int CGsmModem::ReadMsgIndex(char *pReadPool)
{
int i=0;
int nLimitLen=50;
int nMsgIndex=-1;
int nPos=0;
char cTemp[4];
char *pFirst=NULL;
pFirst=strstr(m_pReadPool,"+CMTI:");
//短消息格式
if(pFirst!=NULL)
{
nPos=pFirst-pReadPool;
i=0;
//寻找冒号:
while(i<nLimitLen)
{
if(pReadPool[nPos]==':')
{
break;
}
nPos++;
i++;
}
//失败
if(i==nLimitLen)
{
return nMsgIndex;
}
//跳过冒号:
nPos++;
//寻找逗号,
while(i<nLimitLen)
{
if(pReadPool[nPos]==',')
{
break;
}
nPos++;
i++;
}
//失败
if(i==nLimitLen)
{
return nMsgIndex;
}
//跳过逗号,
nPos++;
//读取新短消息的索引号
i=0;
while(1)
{
if(m_pReadPool[nPos]==0x0D||i>3)
{
break;
}
cTemp[i++]=m_pReadPool[nPos++];
}
cTemp[i]=0;
nMsgIndex=atoi(cTemp);
return nMsgIndex;
}
return nMsgIndex;
}
////////////////////////////////////////////////////////////////////////////
//功能:从SIM卡中读取短消息
//history name data remark
// wanfangjie 2003.04.17
//参数说明 char *pReadPool 从SIM卡中读取的短消息
// int nMsgIndex 短消息的序列号
//返回值:数据的长度
/////////////////////////////////////////////////////////////////////////////
int CGsmModem::ReadMsgFromSIM(int nMsgIndex,char *pReadPool)
{
char pAtCmd[64];
int nDataLen=0;
_itoa(nMsgIndex,pReadPool,10);
strcpy(pAtCmd,"AT+CMGR=");
strcat(pAtCmd,pReadPool);
strcat(pAtCmd,"\r");
SendATCmd(pAtCmd);
DelayTimer(500);
nDataLen=GetCommEvent(0);
if(nDataLen>0)
{
ReadDataFromPort(pReadPool,nDataLen,1);
}
return nDataLen;
}
////////////////////////////////////////////////////////////////////////////
//功能:对短消息进行解码
//history name data remark
// wanfangjie 2003.03.20 创建
// wanfangjie 2003.04.03 从VC下移植到VCE
// wanfangjie 2003.04.14 添加边界条件
// wanfangjie 2003.04.17 添加不在SIM卡上的情况
// wanfangjie 2003.09.04 修改函数的返回类型
//参数说明 char *pReadPool 短消息的内容
// BOOL nIsInSIM 是否在SIM卡上,0不在,1在
//返回值: 接受到短消息的内容与类型
/////////////////////////////////////////////////////////////////////////////
int CGsmModem::DecodeShortMsg(char *pReadPool, BOOL nIsInSIM)
{
int nLimitLen=50;//循环次数的限制
int i=0;
char cTemp[4];//读PDU数据的长度
char cMsgState=0;
int nPduLen=0;
int nDataLen=0;
char *pFirst=NULL;
int nPosition=0;
char cNum=0;
//为短消息信息结构赋值
m_pShortMsgInfo->nMsgLen=0;
m_pShortMsgInfo->MsgData[0]=0;
m_pShortMsgInfo->nMsgType=0x00;
m_pShortMsgInfo->pSrcPhoneNum[0]=0;
m_pShortMsgInfo->pTime[0]=0;
//在SIM卡上的情况
if(nIsInSIM)
{
//寻找头信息
pFirst=strstr(pReadPool,"CMGR");
if(pFirst==NULL)
{
return 0;
}
nPosition=pFirst-pReadPool;
//跳过CMGR:和空格
nPosition+=strlen("CMGR: ");
//读消息状态 0,未读过,1读过
cMsgState=pReadPool[nPosition];
nPosition++;
//跳过第一个逗号,
nPosition++;
}
else//不在SIM卡上的情况
{
//寻找头信息
pFirst=strstr(pReadPool,"CMT:");
if(pFirst==NULL)
{
return 0;
}
nPosition=pFirst-pReadPool;
//跳过CMT:和空格
nPosition+=strlen("CMT: ");
}
//找下一个逗号,
i=0;
while(i<nLimitLen)
{
if(pReadPool[nPosition]==',')
{
break;
}
i++;
nPosition++;
}
if(i==nLimitLen)
{
return 0;
}
//跳过该逗号,
nPosition++;
//读PDU数据的长度
i=0;
while(i<nLimitLen)
{
if(pReadPool[nPosition]==0x0D)
{
break;
}
cTemp[i++]=pReadPool[nPosition++];
}
cTemp[i]=0;
if(i==nLimitLen)
{
return 0;
}
//长度为
nPduLen=atoi(cTemp);
if(nPduLen==0)
{
return 0;
}
//跳过回车与换行
nPosition+=2;
//读取服务中心号码的长度
cTemp[0]=pReadPool[nPosition++];
cTemp[1]=pReadPool[nPosition++];
cNum=m_cGSMProtocol.TwoCharacterToByte(cTemp);
//跳过服务中心号码
nPosition+=cNum*2;
//跳过FO(第一字节)
nPosition+=2;
//读原地址的长度
cTemp[0]=pReadPool[nPosition++];
cTemp[1]=pReadPool[nPosition++];
cNum=m_cGSMProtocol.TwoCharacterToByte(cTemp);
//读地址类型
cTemp[0]=pReadPool[nPosition++];
cTemp[1]=pReadPool[nPosition++];
cTemp[2]=0;
//国际型码
if(strcmp(cTemp,"91")==0||cNum==0x0D)
{
nPosition+=2;
cNum-=2;
}
//处理字符F
if(cNum%2!=0)
{
cNum+=1;
}
//读取电话号码
m_cGSMProtocol.PduNumToPhoneNum(m_pShortMsgInfo->pSrcPhoneNum,&pReadPool[nPosition],cNum);
//跳过电话号码
nPosition+=cNum;
//跳过PID协议
nPosition+=2;
//读取DCS编码标准
cTemp[0]=pReadPool[nPosition++];
cTemp[1]=pReadPool[nPosition++];
cNum=m_cGSMProtocol.TwoCharacterToByte(cTemp);
m_pShortMsgInfo->nMsgType=cNum;
//读去时间,年
m_pShortMsgInfo->pTime[1]=pReadPool[nPosition++];
m_pShortMsgInfo->pTime[0]=pReadPool[nPosition++];
//月
m_pShortMsgInfo->pTime[3]=pReadPool[nPosition++];
m_pShortMsgInfo->pTime[2]=pReadPool[nPosition++];
//日
m_pShortMsgInfo->pTime[5]=pReadPool[nPosition++];
m_pShortMsgInfo->pTime[4]=pReadPool[nPosition++];
//小时
m_pShortMsgInfo->pTime[7]=pReadPool[nPosition++];
m_pShortMsgInfo->pTime[6]=pReadPool[nPosition++];
//分钟
m_pShortMsgInfo->pTime[9]=pReadPool[nPosition++];
m_pShortMsgInfo->pTime[8]=pReadPool[nPosition++];
//秒
m_pShortMsgInfo->pTime[11]=pReadPool[nPosition++];
m_pShortMsgInfo->pTime[10]=pReadPool[nPosition++];
m_pShortMsgInfo->pTime[12]=0;
//跳过时区
nPosition+=2;
//读取数据长度
cTemp[0]=pReadPool[nPosition++];
cTemp[1]=pReadPool[nPosition++];
cNum=m_cGSMProtocol.TwoCharacterToByte(cTemp);
nDataLen=(unsigned char)cNum;
m_pShortMsgInfo->nMsgLen=nDataLen;
//数据格式
if(m_pShortMsgInfo->nMsgType==0x15)
{
for(i=0;i<nDataLen;i++)
{
m_pShortMsgInfo->MsgData[i]=m_cGSMProtocol.TwoCharacterToByte(&pReadPool[nPosition+2*i]);
}
}
//UCS2格式
else if(m_pShortMsgInfo->nMsgType==0x08)
{
for(i=0;i<nDataLen;i++)
{
m_pWritePool[i]=m_cGSMProtocol.TwoCharacterToByte(&pReadPool[nPosition+2*i]);
}
m_cGSMProtocol.InterchangeByteInWord(m_pWritePool,m_pShortMsgInfo->MsgData,nDataLen);
m_pShortMsgInfo->MsgData[nDataLen]=0;
m_pShortMsgInfo->MsgData[nDataLen+1]=0;
}
else//文本格式
{
m_pShortMsgInfo->nMsgLen=nDataLen;
//7位编码变为8位
for(i=0;i<nDataLen;i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -