📄 gsmmodem.cpp
字号:
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 创建
//返回值 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;
}
//////////////////////////////////////////////////////////////////////////////////
//功能:判断服务公司
/////////////////////////////////////////////////////////////////////////////////
int CGsmModem::ServiceCorp(char *pCropName)
{
int nBufLen=0;
//网络注册
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("网络注册失败"));
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("网络注册失败"));
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,"中国移动");
return 1;
}
else if(strstr(m_pReadPool,"46001")!=NULL)
{
strcpy(pCropName,"中国联通");
return 1;
}
else
{
// AfxMessageBox(_T("SIM卡无效"));
return 0;
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////
//功能:发送短消息
//history: name data remarks
// wanfangjie 2003.09.05 create
// wanfangjie 2003.09.17 添加参数 int nIsUnicode
//参数说明 char *pData 发送的数据
// char *pPhoneNum 电话号码
// int nDataLen 数据的长度 小于140
// int nMsgType 消息类型 0x00 文本短消息
// 0x08 UCS2格式
// 0x15 数据格式
// 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;
//数据格式
if(nMsgType==0x15)
{
GetLocalTime(&flagTime);
//生成标志码
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;
}
////////////////////////////////////////////////////////////////////////////////
//功能:时间延迟函数(以毫秒为单位)
//history: name data remarks
// wanfangjie 2002.08.26 create
// wanfangjie 2002.10.10 添加边界条件(对0点的处理)
////////////////////////////////////////////////////////////////////////////////
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;
}
////////////////////////////////////////////////////////////////////////////////
//功能:关机
//history: name data remarks
// wanfangjie 2003.09.04 create
////////////////////////////////////////////////////////////////////////////////
int CGsmModem::CloseGsmPhone()
{
SendATCmd("AT^SMSO\r");
ClearCommBuf(2);
m_bIsGsmOn=0;
return 1;
}
////////////////////////////////////////////////////////////////////////////////
//功能:显示来电号码
//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)//读取电话号码
{
//跳过"+CLCC: "
pFirst+=strlen("+CLCC: ");
//读id
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳过ID与逗号
//读dir
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳过dir与逗号
//读state
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳过state与逗号
//读mode
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳过mode与逗号
nMode=atoi(pTemp);
//读mpty
if(ReadSmsStr(pFirst,pTemp,',')==0)
{
return 0;
}
pFirst+=strlen(pTemp)+1;//跳过mpty与逗号
//读电话号码
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)//读取电话号码
{
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 添家返回功能
//参数说明: TCHAR *pathName 存放短消息的文件名
//返回参数 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);
//数据格式
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("接受失败,重新发送!");
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;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -