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

📄 wsminterface.cpp

📁 手机发送短信的Activex控件
💻 CPP
字号:
// WSMInterface.cpp: implementation of the CWSMInterface class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//tts #include "WSMModule.h"
#include "WSMInterface.h"
#include "SendMsgOCXCtl.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWSMInterface::CWSMInterface()
{
	InitMembers();
}

CWSMInterface::~CWSMInterface()
{

}

BOOL CWSMInterface::OpenCom(int portNo)
{
	BOOL bResult = CWSerialComm::OpenCom(portNo);
	if(bResult) bResult = InitSMSModule();

	return bResult;
}

BOOL CWSMInterface::WriteString(LPCSTR lpszToWrite)
{
	int nSize = strlen(lpszToWrite);
	return WriteDataBlock((const BYTE*)lpszToWrite, nSize);
}

DWORD CWSMInterface::SendAtCmd(LPCSTR lpszToSend, int len, DWORD dwTimeOut )
{
	
	ResetEvent(m_hSendEvent);
	
	if(len < 0) len = strlen(lpszToSend);

	if(!WriteDataBlock((const BYTE*)lpszToSend, len))
		return ERR_COM_WRITE_ERROR;

#ifndef TEST_SMS

	DWORD dwWait = WaitForSingleObject(m_hSendEvent, dwTimeOut);
	if(WAIT_OBJECT_0 != dwWait)
		return ERR_SMS_NO_ANSWER;

#endif	// return immediately if TEST_EMS defined

	return 0;
}

BOOL CWSMInterface::InitSMSModule()
{
	ASSERT(IsOpen());
	
	DWORD dwResult = 0;
	
	const char *pCmds[] = {AT_ECHO_OFF, AT_SET_TEXT_MODE, AT_PASSIVE_RECEIVE_MODE};

	for( int i = 0; i < 3; i++)
	{
		dwResult = SendAtCmd(pCmds[i], -1);
		if(dwResult != 0) break;
	}
	
	return dwResult == 0;
}

void CWSMInterface::SetWinHandleAndNotifyMessage(HWND hwnd, WORD msg)
{
	m_hwndNotify = hwnd;
	m_msgNotify = msg;
}

DWORD CWSMInterface::SendOneFrame(const CWMsgSent& toSent)
{
	DWORD dwError = 0;
	int nPacketCount = toSent.GetPacketCount();
	for( int i = 0; i < nPacketCount; i ++)
	{
		dwError = SendOnePacket(toSent, i);
		if(dwError != 0) break;					// error ?
	}

	return dwError;
}

DWORD CWSMInterface::SendOnePacket(const CWMsgSent& toSent, int packetIndex)
{
	CWMsgPacket msgPacket;
	toSent.GetMsgPacket(packetIndex, msgPacket);
	return SendPacket(msgPacket);
}

DWORD CWSMInterface::SendPacket(const CWMsgPacket& toSent, BOOL bAsPC)
{
	// set phone number

	CString strSetPhoneNumber(AT_SEND_MSG);

	// for test
	if(!bAsPC) strSetPhoneNumber = "+CMT: \"+86%number%\",,\"04/04/14,17:51:52+00\",145,36,0,0,\"+8613800250540\",145,9\r\n";

	strSetPhoneNumber.Replace("%number%", toSent.GetPhoneNumber());

	DWORD dwError = SendAtCmd(strSetPhoneNumber, strSetPhoneNumber.GetLength());
	if( 0 != dwError)	return dwError;

	// set content
	int nLen;
	const BYTE* lpData = toSent.GetDataAndSize(nLen);
	if(!WriteDataBlock(lpData, nLen))
		return ERR_COM_WRITE_ERROR;

	return SendAtCmd(AT_SHORT_MSG_END, -1);
}

DWORD CWSMInterface::SendMessage(LPCTSTR lpcszPhoneNumber, BYTE msgType, 
								 const BYTE* lpData, DWORD dwDataSize)
{
	DWORD dwResult = 0;

	// initialize CWMsgSent 
	CWMsgSent toSent;
	toSent.SetPhoneNumber(lpcszPhoneNumber);
	toSent.SetMessage(msgType, lpData, dwDataSize);

	// save and get frame no -- send
	
	if( m_msgHistory.SaveMsg(toSent) )
		dwResult = SendOneFrame(toSent);
	else
		dwResult = ERR_TOO_MANY_WAITING_FRAMES;

	return dwResult;
}

void CWSMInterface::OnDataRead( const LPBYTE lpData, DWORD dwLength)
{
	for( DWORD dwIndex = 0; dwIndex < dwLength; dwIndex ++ )
		ProcessOneByte(lpData[dwIndex]);
}

void CWSMInterface::ParseAtCmd(BYTE byData)
{
	switch(m_atParser.ParseOneByte(byData))
	{
	case FrameOK:
		{
			const CWAtCmdResult& atCmdReturn = m_atParser.GetParseResult();
			ProcessReceivedAtCmdResult(atCmdReturn);
			ResetParsers();
			break;
		}
	case FrameNotFinished:
		break;

	case FrameCheckSumError:			// parser will reset automatically
	case FrameSegmentDiscarded:
		m_currCmd = NOT_DETERMINED;
		break;
	default:
		ASSERT(FALSE);
		break;
	}
}

void CWSMInterface::ResetParsers()
{

	m_atParser.Reset();
	m_msgParser.Reset();
	m_currCmd = NOT_DETERMINED;
}

void CWSMInterface::ParseMsgFrame(BYTE byData)
{
	switch(m_msgParser.ParseOneByte(byData))
	{
	case FrameOK:
		{
			const CWMsgPacket& msgFrame = m_msgParser.GetParseResult();
			ProcessReceivedMsgFrame(msgFrame);
			ResetParsers();
			break;
		}

	case FrameNotFinished:

		break;

	case FrameSegmentDiscarded:		// parser will reset automatically
	case FrameCheckSumError:
		m_currCmd = NOT_DETERMINED;
		break;
	default:
		ASSERT(FALSE);
		break;
	}
}

void CWSMInterface::ProcessOneByte(BYTE byData)
{
	TRACE2("process char = %c byte = %d \n", byData, byData);

	switch(m_currCmd)
	{
	case AT_CMD:
		ParseAtCmd(byData);
		break;

	case MSG_FRAME_CMD:
		ParseMsgFrame(byData);
		break;

	case NOT_DETERMINED:

		if(m_atParser.ParseOneByte(byData) == FrameNotFinished)
			m_currCmd = AT_CMD;
		
		if(m_msgParser.ParseOneByte(byData) == FrameNotFinished)
			m_currCmd = MSG_FRAME_CMD;

		break;

	default:
		ASSERT(FALSE);
		break;
	}
}

BOOL CWSMInterface::InitMembers()
{
	m_currCmd = CWSMInterface::NOT_DETERMINED;
	
	m_hSendEvent = CreateEvent( NULL, TRUE, FALSE,  NULL ) ; 
	return m_hSendEvent != NULL;
}

void CWSMInterface::ProcessReceivedMsgFrame(const CWMsgPacket& msgFrame)
{

	BYTE dataBuff[MAX_BIN_MSG_BODY_LEN_PER_PACKET];
	int nSize;
	WORD wMsgNo;
	DWORD dwError;

#ifdef TEST_SMS 
	
	::SendMessage(m_hwndNotify, m_msgNotify, 1, (long)&msgFrame);

#endif

	CWMsgSent historyItem;
	historyItem.SetPhoneNumber(msgFrame.GetPhoneNumber());
	historyItem.SetFrameNo(msgFrame.GetFrameNo());

	switch(msgFrame.GetMessageType())
	{
	case CWMsgPacket::ANSWER_FRAME_MSG :
		nSize = msgFrame.GetMsgBinaryData(dataBuff);
		ASSERT(nSize <= 2);
		wMsgNo = *(WORD*)dataBuff;
		strcpy((char*)dataBuff, msgFrame.GetPhoneNumber());
		::SendMessage(m_hwndNotify, m_msgNotify, wMsgNo, (LPARAM)&dataBuff[0]);
		//删除该条回复短信
		dwError = SendAtCmd("at+cmgd=1", 9);
		//if( 0 != dwError)	return dwError;
		break;
	
	case CWMsgPacket::SEND_FRAME_AGAIN_MSG :
		
		if( m_msgHistory.GetACopy(historyItem, TRUE) )
			SendOneFrame(historyItem);

		break;
	
	case CWMsgPacket:: SEND_PACKET_AGAIN_MSG:
		if( m_msgHistory.GetACopy(historyItem, TRUE) )
		{
			BYTE packetNo = msgFrame.GetPacketNo();
			int idxPacket = CWMsgPacket::PacketNo2PacketIndex(packetNo);
			SendOnePacket(historyItem, idxPacket);
		}
		break;
	
	case CWMsgPacket::DASHI_MSG :
	case CWMsgPacket::DIAODU_MSG_NEED_ANSWER :
	case CWMsgPacket::DIAODU_MSG_NEEDNOT_ANSWER :
		break;

	default:
		ASSERT(FALSE);
		break;

	}
}

DWORD CWSMInterface::SetSMSCenter(LPCTSTR lpcszCenterNumber)
{
	CString strCmd(AT_SET_SMS_CENTER);
	strCmd.Replace("%number%", lpcszCenterNumber);

	return SendAtCmd(strCmd,-1);
}

void CWSMInterface::ProcessReceivedAtCmdResult(const CWAtCmdResult& atCmdRes)
{
	SetEvent(m_hSendEvent);
}

void CWSMInterface::OnSysTimeChanged()
{
	m_msgHistory.ResetSaveTime();
}

void CWSMInterface::SetTimeOut(const MsgTimeOutType& timeOut)
{
	m_msgHistory.SetTimeOut(timeOut);
}

⌨️ 快捷键说明

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