⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smsg.cpp

📁 GSM Mobile收发短信
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		CString strRet("");
		// Wait for an event		
		//WaitForComEvent("",&strRet);		
		MR.init();
		MR.type(strRet);
		MR.parse(strRet);

		/*for(int j = 0 ; j < 1024 ; j++)
		{
			if(MR.mp[j].msgCalledNo=="" || MR.mp[j].msgMsgTime=="" || MR.mp[j].msgBody=="")
			{
				break;
			}			
			memset(Ret[k],0,sizeof(Ret[k]));//<主叫号码><接收时间><接收内容><短信索引>
			t="<"+MR.mp[j].msgCalledNo+"><"+
						MR.mp[j].msgMsgTime+"><"+
						MR.mp[j].msgBody+"><"+
						MR.mp[j].msgSeq+">";
					sprintf(Ret[k++],"%s\0",(LPCTSTR)t);			
		}*/
	}
	catch(...)
	{
		return CODE_ERR;
	}

    return CODE_OK;
}
/*99
涵数说明:
	int CSMsg::msgRead(int Type)
参数说明:
	int Type:手机中短信类型[0-->没读,1-->已读];
反回值:
  Ret >=0:成功;
	Ret ==-1 :失败;没有结果;
*/
int CSMsg::msgRead(const CString& strContext,char strCalledNo[],char strCallingNo[],char strTime[],char strBody[])
{
    CMsgRecv MR;
	MR.init();
	MR.type(strContext);
	MR.parse(strContext);
	
	if(MR.mp[0].msgCalledNo=="" || MR.mp[0].msgMsgTime=="" || MR.mp[0].msgBody=="")
	{
		return CODE_ERR;
	}			
	sprintf(strCalledNo,"%s\0",MR.mp[0].msgCalledNo.GetBuffer(0));

	int nYear = 0 , nMonth = 0 , nDate = 0, nHour = 0, nMinute = 0 ,nSecond = 0;
	if(MR.mp[0].msgMsgTime.GetLength() >= 12)
	{
		MR.mp[0].msgMsgTime = MR.mp[0].msgMsgTime.Mid(0,12);
		nYear = atoi(MR.mp[0].msgMsgTime.Mid(0,2).GetBuffer(0));
		nMonth = atoi(MR.mp[0].msgMsgTime.Mid(2,2).GetBuffer(0));
		nDate = atoi(MR.mp[0].msgMsgTime.Mid(4,2).GetBuffer(0));
		nHour = atoi(MR.mp[0].msgMsgTime.Mid(6,2).GetBuffer(0));
		nMinute = atoi(MR.mp[0].msgMsgTime.Mid(8,2).GetBuffer(0));
		nSecond = atoi(MR.mp[0].msgMsgTime.Mid(10,2).GetBuffer(0));
		sprintf(strTime,"20%02d-%02d-%02d %02d:%02d:%02d\0",nYear,nMonth,nDate,nHour,nMinute,nSecond);
	}
	sprintf(strBody,"%s\0",MR.mp[0].msgBody.GetBuffer(0));
	sprintf(strCallingNo,"%s\0",MR.msgHand.GetBuffer(0));	
	
	CTime tTime(atol(strTime));
	CString s = tTime.Format( "%A, %B %d, %Y" );
	//MR.mp[j].msgSeq;
	return CODE_OK;
}

/*99
涵数说明:
	int CSMsg::msgSend(char* Body,char* Hand)
参数说明:
	char* Body:发送的短信内容;
	char* Hand:对方的手机器号码;
反回值:
  Ret >=0:成功;
	Ret ==-1 :失败;没有结果;
*/
int CSMsg::msgSend(char* strBody,char* strHand)
{
	try
	{
		CMsgSend MsgSendInfo;
		CString  Len= "";
		if(strlen(strBody) < 1 || strlen(strHand) < 1)
		{
			return CODE_ERR;
		}
		//获取待发送短信内容,并按PDU编码0011000D91683150420271F0000801025fb7
		m_strAppendData = "";
		m_strAppendData = MsgSendInfo.get_msgSend(m_strSmsCentNum,strHand,strBody);
        m_strAppendData += '\x1A';
		Len = MsgSendInfo.get_msgTLen();

		char strATOrder[20];
		memset(strATOrder,0,20);
		//发送短信
		sprintf(strATOrder,"AT+CMGS=%s\r\n\0",Len);
		int nRet = m_Com.Write(strATOrder,strlen(strATOrder));
		if (nRet != ERROR_SUCCESS) 
		{	
		    return CMGS_ERR; 
		}
        
		if(WaitForSingleObject(m_hSendEvent, 2000) == WAIT_OBJECT_0)
		{
		    if(m_RetValue.CompareNoCase("OK") != 0)
			{
				m_RetValue = "";
			    return CMGF_ERR;
			}
		}
	}
	catch(...)
	{
		return CODE_ERR;
	}
	m_RetValue = "";
	return CODE_OK;
}

///////////////////////////////////////////////////////////////////////////////
int CSMsg::IncomeSms(const int nIndex)
{
	try
	{
		char strATOrder[20];
		memset(strATOrder,0,20);
				
		sprintf(strATOrder,"AT+CMGR=%d\r\n\0",nIndex);

		int nRet = m_Com.Write(strATOrder,strlen(strATOrder));
		if (nRet != ERROR_SUCCESS) 
		{	
			return CMGL_ERR; 
		}
	}
	catch(...)
	{
		return CODE_ERR;
	}

    return CODE_OK;
}
/*
涵数说明:串口事件监控线程
	int CSMsg::ComEventProc(LPVOID lpVoid)
参数说明:
	lpVoid:
反回值:
    Ret >= 0:成功;
	Ret ==-1 :失败;没有结果;
*/
unsigned __stdcall ComEventProc(LPVOID lpVoid)
{
    CSMsg* pThis = reinterpret_cast<CSMsg*>(lpVoid);

	if(pThis == NULL)
		return -1;
    CString strEventValue;
	char strATOrder[128];

	bool fContinue = true;				
	DWORD dwRead = 0;
	char szData[2048];
	const  unsigned int nBuflen = sizeof(szData);		
	unsigned long nRet = ERROR_SUCCESS;
	CString strRecv,strComData,strSection1 = "",strSection2 = "",strSection3 = "";

	CString RetClf = "\r\n";
	CString EndOK = "OK";
	CString EndError = "ERROR";
	CString SendB = "> ";
	CString RECV_TAG_CMTI = "CMTI";
	CString RECV_TAG_CMGS = "CMGS";
	CString RECV_TAG_CMGL = "CMGL";
	CString RECV_TAG_CMGR = "CMGR";
	CString RECV_TAG_CMGD = "CMGD";
	CString RECV_TAG_CMGF = "CMGF";
	CString RECV_TAG_CSCA = "CSCA";
	CString RECV_TAG_CNMI = "CNMI";

	for( ; ; )
	{
          try
		  {
			    if(WaitForSingleObject(pThis->m_hKillEvent, 100) == WAIT_OBJECT_0)
					return CODE_OK;

				if(!pThis->m_Com.IsOpen())
				{ 
					return CODE_ERR; 
				}
				// Wait for an event		
				nRet = pThis->m_Com.WaitEvent();
				if (nRet != ERROR_SUCCESS) 
				{ 
					return CODE_ERR; 
				}
				// Save event
				const CSerial::EEvent eEvent = pThis->m_Com.GetEventType();
				
				// Handle data receive event
				if (eEvent & CSerial::EEventRecv)
				{
					do
					{
						memset(szData,0,2048);
						nRet = pThis->m_Com.Read(szData,nBuflen,&dwRead);
						if (nRet != ERROR_SUCCESS) 
						{ 
							return CODE_ERR;
						}

						szData[dwRead] = '\0';
						strComData.Format("%s\0",szData);			
						if(dwRead != 0)
						{
							strRecv += strComData;
							if(strRecv.GetLength() > 4)
							{
								strSection1 = strRecv.Mid(3, 4);
								strSection1.MakeUpper();
								if(strSection1 == RECV_TAG_CMTI)//AT+CMTI
								{	
									if(strRecv.GetLength() >= 12)
									{
										strSection2 = strRecv.Mid(10,2);
										strSection2.MakeUpper();
										if(strSection2 == "SM")
										{			
                                            if(strRecv.GetLength() >= 16)
											{	//获取Index
												strSection3 = strRecv.Mid(14,strRecv.GetLength()- 16);							
												//读取该短消息
												pThis->IncomeSms(atoi(strSection3.GetBuffer(0)));
												strRecv.Empty();
												break;//return;	
											}
										}
									}
								}
								else if(strSection1 == RECV_TAG_CMGL)//AT+CMTI
								{	
									pThis->DealWithComEcho(strRecv,pThis->m_hListEvent);													
								}
								else if(strSection1 == RECV_TAG_CMGR)//AT+CMGR
								{	
									strSection2 = strRecv.Mid(strRecv.GetLength() - 2 , 2 );
									strSection2.MakeUpper();
									if(strSection2 == RetClf)
									{	
										strSection3 = strRecv.Mid(strRecv.GetLength()-4,2);
										strSection3.MakeUpper();
										if(strSection3 == EndOK)
										{			
											pThis->m_RetValue = EndOK;
											char strCalledNo[20],char strCallingNo[20],char strTime[20],char strBody[1024];
											memset(strCalledNo,0,20);
											memset(strCallingNo,0,20);
											memset(strTime,0,20);
											memset(strBody,0,1024);
										    int nRetValue = pThis->msgRead(strRecv,strCalledNo,strCallingNo,strTime,strBody);
											if(nRetValue == CODE_OK)
											{
												strRecv.Empty();
												//pThis->m_pNotifyProc(strCalledNo,strCallingNo,strTime,strBody,true);
											    //删除该信息
												pThis->msgDel(1);
											}
											else
											{
												strRecv.Empty();
                                                //pThis->m_pNotifyProc(strCalledNo,strCallingNo,strTime,strBody,false);
											}
										}
										strSection3 = strRecv.Mid(strRecv.GetLength()-2-5,5);
										strSection3.MakeUpper();
										if(strSection3 == EndError)
										{					
											strRecv.Empty();
											//pThis->m_pNotifyProc("","","","",false);
										}						
									}															
								}
								else if(strSection1 == RECV_TAG_CMGD)//AT+CMGD
								{	
									pThis->DealWithComEcho(strRecv,pThis->m_hDelEvent);															
								}
								else if(strSection1 == RECV_TAG_CMGF)//AT+CMGD
								{	
									pThis->DealWithComEcho(strRecv,pThis->m_hCMGFEvent);															
								}
								else if(strSection1 == RECV_TAG_CNMI)//AT+CMGD
								{	
									pThis->DealWithComEcho(strRecv,pThis->m_hCNMIEvent);																							
								}
								else if(strSection1 == RECV_TAG_CSCA)//AT+CMGD
								{	
									pThis->DealWithComEcho(strRecv,pThis->m_hCSCAEvent);														
								}
								else if(strSection1 == RECV_TAG_CMGS)//AT+CMGS
								{
									if(!(pThis->DealWithComEcho(strRecv,pThis->m_hSendEvent)))
									{
										strSection3=strRecv.Mid(strRecv.GetLength()-2,2);
										strSection3.MakeUpper();
										if(strSection3 == SendB)//INFINITE
										{	
											int nReturn = pThis->m_Com.Write(pThis->m_strAppendData.GetBuffer(0),pThis->m_strAppendData.GetLength());							
											if (nReturn != ERROR_SUCCESS) 
											{	
												strRecv.Empty();
												pThis->m_RetValue = EndError;
											    SetEvent(pThis->m_hSendEvent);	
											}
										}
									}	
								}
							} 
						} 
					} while (dwRead == nBuflen);		
				}
		}
		catch(...)
		{
			return CODE_ERR;
		}
	}
	return CODE_OK;
}

/*涵数说明:
	void CSMsg::WaitForComEvent();
参数说明:
	无;
反回值:
	无;
*/
const bool CSMsg::DealWithComEcho(CString& strRecv,HANDLE hEvent)
{
    CString RetClf = "\r\n";
	CString EndOK = "OK";
	CString EndError = "ERROR";
	CString strTag1,strTag2;
    strTag1 = strRecv.Mid(strRecv.GetLength() - 2 , 2 );
	strTag1.MakeUpper();
	if(strTag1 == RetClf)
	{	
		strTag2 = strRecv.Mid(strRecv.GetLength() - 4 , 2);
		strTag2.MakeUpper();
		if(strTag2 == EndOK)
		{			
			strRecv.Empty();
			m_RetValue = EndOK;
			SetEvent(hEvent);
		}						
		strTag2 = strRecv.Mid(strRecv.GetLength() - 2 - 5 , 5);
		strTag2.MakeUpper();
		if(strTag2 == EndError)
		{
			strRecv.Empty();
			m_RetValue = EndError;
			SetEvent(hEvent);								
		}	
		return true;
	}	
	else
		return false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -