?? gsmmodem.cpp
字號(hào):
m_pWritePool[i]=m_cGSMProtocol.TwoCharacterToByte(&pReadPool[nPosition+2*i]);
}
m_cGSMProtocol.SevenBitToEightBit(m_pWritePool, m_pShortMsgInfo->MsgData, nDataLen);
}
return 1;
}
////////////////////////////////////////////////////////////////////////////
//功能:刪除短消息
//history name data remark
// wanfangjie 2003.04.17 創(chuàng)建
//返回值 0 刪除短消息失敗
// 1 刪除短消息成功
////////////////////////////////////////////////////////////////////////////
int CGsmModem::DeleteShortMsg(int nMsgIndex)
{
int nTimeOut=0;
int nDataLen=0;
char pAtCmd[64];
int nReturn=0;
_itoa(nMsgIndex,m_pReadPool,10);
strcpy(pAtCmd,"AT+CMGD=");
strcat(pAtCmd,m_pReadPool);
strcat(pAtCmd,"\r");
ClearCommBuf(0);
SendATCmd(pAtCmd);
while(nTimeOut<200)
{
DelayTimer(300);
nDataLen=GetCommEvent(0);
if(nDataLen>0)
{
ReadDataFromPort(m_pReadPool,nDataLen,1);
if(strstr(m_pReadPool,"OK")!=NULL)
{
nReturn=1;
break;
}
ClearCommBuf(0);
SendATCmd(pAtCmd);
}
nTimeOut++;
}
return nReturn;
}
//////////////////////////////////////////////////////////////////////////////////
//功能:判斷服務(wù)公司
/////////////////////////////////////////////////////////////////////////////////
int CGsmModem::ServiceCorp(char *pCropName)
{
int nBufLen=0;
//網(wǎng)絡(luò)注冊
ClearCommBuf(0);
SendATCmd("AT+CREG=1\r");
nBufLen=GetCommEvent(0);
if(nBufLen>0)
{
ReadDataFromPort(m_pReadPool,nBufLen,1);
if(strstr(m_pReadPool,"OK")==NULL)
{
// AfxMessageBox(_T("網(wǎng)絡(luò)注冊失敗"));
return 0;
}
}
SendATCmd("AT+COPS=0,2\r");
nBufLen=GetCommEvent(0);
if(nBufLen>0)
{
ReadDataFromPort(m_pReadPool,nBufLen,1);
if(strstr(m_pReadPool,"OK")==NULL)
{
// AfxMessageBox(_T("網(wǎng)絡(luò)注冊失敗"));
return 0;
}
}
SendATCmd("AT+COPS?\r");
nBufLen=GetCommEvent(0);
if(nBufLen>0)
{
ReadDataFromPort(m_pReadPool,nBufLen,1);
if(strstr(m_pReadPool,"46000")!=NULL)
{
strcpy(pCropName,"中國移動(dòng)");
return 1;
}
else if(strstr(m_pReadPool,"46001")!=NULL)
{
strcpy(pCropName,"中國聯(lián)通");
return 1;
}
else
{
// AfxMessageBox(_T("SIM卡無效"));
return 0;
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////
//功能:發(fā)送短消息
//history: name data remarks
// wanfangjie 2003.09.05 create
// wanfangjie 2003.09.17 添加參數(shù) int nIsUnicode
//參數(shù)說明 char *pData 發(fā)送的數(shù)據(jù)
// char *pPhoneNum 電話號(hào)碼
// int nDataLen 數(shù)據(jù)的長度 小于140
// int nMsgType 消息類型 0x00 文本短消息
// 0x08 UCS2格式
// 0x15 數(shù)據(jù)格式
// int nIsUnicode 是否為UNICODE代碼
//
///////////////////////////////////////////////////////////////////////////
int CGsmModem::SendMsg(char *pData, char *pPhoneNum, int nDataLen, int nMsgType,int nIsUnicode)
{
int flag=0;
long nFlagCode;
int nTimeOut;
char ucPackNum=0;
SYSTEMTIME flagTime;
char pTempPool[160];
int nRealLen=0;
int i=0;
char nRecord=0;
int nCommLen=0;
m_bIsSendOver=1;
m_nTransNum=1;
//數(shù)據(jù)格式
if(nMsgType==0x15)
{
GetLocalTime(&flagTime);
//生成標(biāo)志碼
nFlagCode=flagTime.wHour*3600;
nFlagCode+=flagTime.wMinute*60;
nFlagCode+=flagTime.wSecond;
nRecord=0;
ucPackNum=(char)(nDataLen/M_MSGPACKLEN+1);
m_nTransNum=(int)ucPackNum;
for(i=0;i<nDataLen&&nRecord<ucPackNum;i+=M_MSGPACKLEN)
{
nRecord++;
memcpy(pTempPool,&nFlagCode,sizeof(long));
if(nDataLen-i<=M_MSGPACKLEN)
{
pTempPool[4]=ucPackNum;
pTempPool[5]=nRecord;
memcpy(&pTempPool[6],&pData[i],nDataLen-i);
nRealLen=6+nDataLen-i;
}
else
{
pTempPool[4]=ucPackNum;
pTempPool[5]=nRecord;
memcpy(&pTempPool[6],&pData[i],M_MSGPACKLEN);
nRealLen=6+M_MSGPACKLEN;
}
nTimeOut=0;
while(nTimeOut<100)
{
DelayTimer(1000);
nCommLen=GetCommEvent(0);
if(nCommLen>0)
{
nCommLen=GetCommEvent(0);
ReadGsmData(nCommLen);
}
if(m_bIsSendOver)
{
m_bIsSendOver=0;
m_nTransNum--;
flag=OrganizeMsg(pTempPool, pPhoneNum,nRealLen,nMsgType,nIsUnicode);
break;
}
nTimeOut++;
}
}
}
//文本格式
else
{
m_nTransNum=0;
flag=OrganizeMsg(pData, pPhoneNum,nDataLen,nMsgType,nIsUnicode);
}
return flag;
}
////////////////////////////////////////////////////////////////////////////////
//功能:時(shí)間延遲函數(shù)(以毫秒為單位)
//history: name data remarks
// wanfangjie 2002.08.26 create
// wanfangjie 2002.10.10 添加邊界條件(對0點(diǎn)的處理)
////////////////////////////////////////////////////////////////////////////////
int CGsmModem::DelayTimer(int nMillisecond)
{
long nRealTime;
SYSTEMTIME startTime,endTime;
GetLocalTime(&startTime);
do
{
GetLocalTime(&endTime);
if(endTime.wHour<startTime.wHour)
{
endTime.wHour+=24;
}
nRealTime=(endTime.wHour-startTime.wHour)*3600*1000;
nRealTime+=(endTime.wMinute-startTime.wMinute)*60*1000;
nRealTime+=(endTime.wSecond-startTime.wSecond)*1000;
nRealTime+=endTime.wMilliseconds-startTime.wMilliseconds;
}while(nRealTime<nMillisecond);
return 1;
}
////////////////////////////////////////////////////////////////////////////////
//功能:關(guān)機(jī)
//history: name data remarks
// wanfangjie 2003.09.04 create
////////////////////////////////////////////////////////////////////////////////
int CGsmModem::CloseGsmPhone()
{
SendATCmd("AT^SMSO\r");
ClearCommBuf(2);
m_bIsGsmOn=0;
return 1;
}
////////////////////////////////////////////////////////////////////////////////
//功能:顯示來電號(hào)碼
//history: name data remarks
// wanfangjie 2003.09.04 create
////////////////////////////////////////////////////////////////////////////////
int CGsmModem::DispInComingNum(char *pIncomingNum)
{
int nDataLen=0;
int nLen=0;
int nLimtLen=31;
char pTemp[32];
int nMode=0;
char *pFirst=NULL;
#ifdef _SIEMENS
SendATCmd("AT+CLCC\r");
nDataLen=GetCommEvent(0);
if(nDataLen>0)
{
ReadDataFromPort(m_pReadPool,nDataLen,1);
pFirst=strstr(m_pReadPool,"+CLCC:");
if(pFirst!=NULL)//讀取電話號(hào)碼
{
//跳過"+CLCC: "
pFirst+=strlen("+CLCC: ");
//讀id
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳過ID與逗號(hào)
//讀dir
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳過dir與逗號(hào)
//讀state
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳過state與逗號(hào)
//讀mode
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳過mode與逗號(hào)
nMode=atoi(pTemp);
//讀mpty
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳過mpty與逗號(hào)
//讀電話號(hào)碼
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
if(strlen(pTemp)<=2)
{
strcpy(pIncomingNum,"未知電話");
}
else
{
strcpy(pIncomingNum,&pTemp[1]);
nLen=strlen(pIncomingNum);
pIncomingNum[nLen-1]=0;
}
}
}
#else
int i = 1;
ZeroMemory(pIncomingNum, 32);
if(m_pReadPool)
{
pFirst=strstr(m_pReadPool,"*ECAV:");
if(pFirst!=NULL)//讀取電話號(hào)碼
{
pFirst=strstr(m_pReadPool,"\"");
if(pFirst!=NULL)
{
while((*(pFirst+i) !='\"') && (i<= 32))
{
pIncomingNum[i-1] = *(pFirst+i);
i++;
}
}
}
}
#endif
return 1;
}
////////////////////////////////////////////////////////////////////////////////
//功能:打電話
//history: name data remarks
// wanfangjie 2003.09.04 create
////////////////////////////////////////////////////////////////////////////////
int CGsmModem::CallPhone(const char *pPhoneNum)
{
#if(M_GSMTYPE==2)
// mic_on(1);
#endif
char aAtCmd[64];
strcpy(aAtCmd,"ATD");
strcat(aAtCmd,pPhoneNum);
strcat(aAtCmd,";\r");
SendATCmd(aAtCmd);
return 1;
}
void CGsmModem::SendDTMF(const char *pPhoneNum)
{
char aAtCmd[64];
sprintf(aAtCmd,"AT+VTS=\"%s\"\r", pPhoneNum);
SendATCmd(aAtCmd);
return ;
}
////////////////////////////////////////////////////////////////////////////////
//功能:掛斷電話
//history: name data remarks
// wanfangjie 2003.09.04 create
////////////////////////////////////////////////////////////////////////////////
int CGsmModem::HangupPhone()
{
SendATCmd("ATH\r");
return 1;
}
///////////////////////////////////////////////////////////////////////////
//功能:接聽電話
//history: name data remarks
// wanfangjie 2003.09.18 create
///////////////////////////////////////////////////////////////////////////
int CGsmModem::ReceivePhone()
{
#if(M_GSMTYPE==2)
//mic_on(1);
#endif
SendATCmd("ATA\r");
return 1;
}
////////////////////////////////////////////////////////////////////////////////
//功能:把短消息以文件的形式保存
//history: name data remarks
// wanfangjie 2003.09.04 create
// wanfangjie 2003.09.09 添家返回功能
//參數(shù)說明: TCHAR *pathName 存放短消息的文件名
//返回參數(shù) 1 保存短消息成功 2 保存短消息成功并寫完畢
////////////////////////////////////////////////////////////////////////////////
int CGsmModem::SaveMsgFile(TCHAR *pathName)
{
TCHAR pStartName[64];
TCHAR pEndName[64];
TCHAR pFlagName[64];
long nFlag=0;
char nPos=0;
int nReturn=1;
CFile msgFile;
m_cGSMProtocol.StrCopy(pStartName,pathName);
m_cGSMProtocol.StrCopy(pEndName,pathName);
//數(shù)據(jù)格式
if((m_pShortMsgInfo->nMsgType==0x015)&&(m_pShortMsgInfo->nMsgLen>0))
{
nPos=0;
nFlag=ReadLong(m_pShortMsgInfo->MsgData);
nPos+=sizeof(long);
nFlag*=10;
m_cGSMProtocol.IntToString(nFlag, pFlagName,10);
m_cGSMProtocol.StrCat(pStartName,pFlagName);
m_cGSMProtocol.StrCat(pStartName,_T(".txt"));
if(msgFile.Open(pStartName,CFile::modeReadWrite)==NULL)
{
msgFile.Open(pStartName,CFile::modeCreate|CFile::modeReadWrite);
}
msgFile.SeekToEnd();
msgFile.Write(&m_pShortMsgInfo->MsgData[6],m_pShortMsgInfo->nMsgLen-6);
msgFile.Close();
//最后一塊
if(m_pShortMsgInfo->MsgData[4]==m_pShortMsgInfo->MsgData[5])
{
nFlag+=1;
//m_cGSMProtocol.IntToString(nFlag, pFlagName,10);
//GetPrivateProfileString("TwoCodeSettings","TwoCodeFileName",NULL,pFlagName,63,"DirectCenter.ini");
//m_cGSMProtocol.StrCat(pEndName,pFlagName);
//m_cGSMProtocol.StrCat(pEndName,_T(".txt"));
// strcat(pEndName,"commdata.txt");
// msgFile.Rename(pStartName,pEndName);
short mLength;
char buffer[10240],buffer2[64];
CFile updatefile,recordfile,tempfile;
// updatefile.Open(pEndName,CFile::modeRead);
updatefile.Open(pStartName,CFile::modeRead);
updatefile.Read(&mLength,sizeof(short));
/* updatefile.Read(buffer,mLength);
for(int i=0;i<mLength;i++)
{
if(buffer[i]!=0&&buffer[i+1]!=0)//漢字
{
buffer2[i]=buffer[i];
buffer2[i+1]=buffer[i+1];
}
else if(buffer[i]!=0&&buffer[i+1]==0)//單字符
{
buffer2[i]=buffer[i];
i++;
}
}
*/
updatefile.Read(buffer2,mLength);
buffer2[mLength]=0;
int FileLength=updatefile.GetLength()-mLength-2;
updatefile.Read(buffer,FileLength);
buffer[FileLength]=0;
updatefile.Close();
// updatefile.Remove(pStartName);
CString fileName;//子目錄名或文件名
fileName=_T("..\\Data\\");
fileName+=buffer2;//原來的文件名
recordfile.Remove(fileName);
if(recordfile.Open(fileName,CFile::modeReadWrite)==NULL)//新的文件
{
if(recordfile.Open(fileName,CFile::modeCreate|CFile::modeReadWrite)==NULL)
{
// ::AfxMessageBox("接受失敗,重新發(fā)送!");
return 0;
}
recordfile.Write(buffer,FileLength);
recordfile.Close();
//從buffer2中取得地圖名和圖層名
char mapname[50],lvlname[21];
for(int i=0;i<mLength;i++)
{
int j;
if(buffer2[i]=='_')
{
for(j=0;j<i;j++)
{
mapname[j]=buffer2[j];
}
mapname[i]=0;
for(j=i+2;j<mLength;j++)//?
{
lvlname[j-i-2]=buffer2[j];
}
int k=j-i-3;
for(k;k<21;k++)
{
lvlname[k]=0;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -