?? 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 + -