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

📄 mainfrm.cpp

📁 对Motorola的手机模块
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "smsinterface.h"
#include "MainFrm.h"
#include "LeftView.h"
#include "smsinterfaceView.h"
#include "SmsDelView.h"
#include "SmsEditView.h"
#include "SmsSetView.h"
#include "SimpinDlg.h"
#include <afx.h>
#include "ReadSMDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_MESSAGE(WM_COMM_RXCHAR, OnReceive)
	ON_MESSAGE(WM_ATCOMM_RECEIVED, OnGetAck)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	RegisterWindowMessage("WM_SENDFORM");
	RegisterWindowMessage("WM_DELFORM");
	RegisterWindowMessage("WM_EDITFORM");
	RegisterWindowMessage("WM_SETFORM");
	curcommand.Empty();
	precommand.Empty();
	ch1=0x0d;
	ch2=0x0a;
	endflag=_T("OK");
	endflag.Insert(0,ch1);
	endflag.Insert(1,ch2);
	endflag.Insert(5,ch1);
	endflag.Insert(6,ch2);
	errorE=_T("+CME ERROR:");
	errorE.Insert(0,ch1);
	errorE.Insert(1,ch2);
	errorS=_T("+CMS ERROR:");
	errorS.Insert(0,ch1);
	errorS.Insert(1,ch2);	
	/////////////////////////////////////////////////////////////////////////
	CFile gfile;
	CFileException e;
	if( !gfile.Open( "e:/smsinterface/serverphone.dat", CFile::modeReadWrite, &e ) )
	{
	#ifdef _DEBUG
	     afxDump << "File could not be opened " << e.m_cause << "\n";
	#endif
	return;
	}
	else
	{
	unsigned char ch;
	int result;
	result=gfile.Read(&ch,1);
	servercenterphone.Empty();
	while(result)
	{
		if(ch!=0xff)
		{
			servercenterphone.Insert(100,ch);
			
		}
		else
			break;
		result=gfile.Read(&ch,1);
	}
	gfile.Close();
	//servercenterphone=_T("86")+servercenterphone;

	/////////////////////////////////////////////////////////////////////////
	m_chChecksum1=0;  //校验和置0
	m_chChecksum2=0;
	m_nRXErrorCOM1=0;   //COM1接收数据错误帧数置0
	m_nRXErrorCOM2=0;   //COM2接收数据错误帧数置0
	m_nRXCounterCOM1=0;   //COM1接收数据错误帧数置0
	m_nRXCounterCOM2=0;   //COM2接收数据错误帧数置0
	m_strRXhhCOM1.Empty();  //清空半BYTE校验hh存储变量
	m_strRXhhCOM2.Empty();
	if (m_ComPort.InitPort(this,1,9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
	{
		//SetTimer(1,1000,NULL);   //设置定时器,1秒后发送数据
		//////////////////////////////////////////////////////
		m_pbnamemaxlen=0;
		m_pbphonemaxlen=0;
		m_pbdindex=0;
		m_pbsindex=0;
		m_pbtotalloc=0;
		m_pbusedloc=0;
		m_strcommand=_T("AT*P1");
		m_strcommand.Insert(6,ch1);
		SendString(m_strcommand);
		m_commandtype=AT_COMMAND_AT;
		curcommand=m_strcommand;
		m_ComPort.StartMonitoring();   //启动串口监视线程
	}
	else
	{
		CString str;
		str.Format("COM%d 没有发现,或被其它设备占用",1);
		AfxMessageBox(str);
	}
	}
}

CMainFrame::~CMainFrame()
{
	CFile gfile;
	CFileException e;
	if( !gfile.Open( "e:/smsinterface/serverphone.dat", CFile::modeCreate|CFile::modeWrite, &e ) )
	{
	#ifdef _DEBUG
	     afxDump << "File could not be opened " << e.m_cause << "\n";
	#endif
	return;
	}
	else
	{
	unsigned char ch;
	int result;
	CString scp;
	ch=0xff;
	scp=servercenterphone;
	scp.Insert(100,ch);
	gfile.Write(scp,scp.GetLength());
	gfile.Close();
	m_ComPort.StopMonitoring();
	}
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	// create splitter window
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;

	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(100, 100), pContext) ||
		!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CSmsinterfaceView), CSize(100, 100), pContext))
	{
		
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}
	m_pContext=pContext;
	m_currentwindow=SENDFORM;
	return TRUE;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


LRESULT CMainFrame::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if((message==WM_SENDFORM)&&(wParam!=1))
	{
		m_wndSplitter.DeleteView(0, 1);
		m_wndSplitter.CreateView(0, 1, 
		RUNTIME_CLASS(CSmsinterfaceView), 
		CSize(0, 0), 
		NULL);
		m_wndSplitter.RecalcLayout();
		/*
		m_currentwindow=SENDFORM;
		m_strcommand.Empty();
		m_strcommand=_T("AT+CPBS=");
		m_strcommand.Insert(20,'"');
		m_strcommand+=_T("SM");
		m_strcommand.Insert(30,'"');
		m_strcommand.Insert(30,ch1);
		m_commandtype=AT_COMMAND_ATCPBS;
		SendString(m_strcommand);
		precommand=curcommand;
		curcommand=m_strcommand;*/
		m_wndSplitter.SetActivePane(0,1,NULL);
		(this->GetActiveView())->PostMessage(WM_GETPHONEBOOKLIST,0,0);
			
	}
	if((message==WM_DELFORM)&&(wParam!=2))
	{
		m_wndSplitter.DeleteView(0, 1);
		m_wndSplitter.CreateView(0, 1, 
		RUNTIME_CLASS(CSmsDelView), 
		CSize(0, 0), 
		NULL);
		m_wndSplitter.RecalcLayout();
		m_currentwindow=DELFORM;
		m_strcommand.Empty();
		m_strcommand=_T("AT+CMGL");
		m_strcommand.Insert(30,ch1);
		m_commandtype=AT_COMMAND_ATCMGLTOTOAL;
		SendString(m_strcommand);
		precommand=curcommand;
		curcommand=m_strcommand;
		m_isfirstlist=1;
	}
	if((message==WM_EDITFORM)&&(wParam!=3))
	{
		m_wndSplitter.DeleteView(0, 1);
		m_wndSplitter.CreateView(0, 1, 
		RUNTIME_CLASS(CSmsEditView), 
		CSize(0, 0), 
		NULL);
		m_wndSplitter.RecalcLayout();
		m_currentwindow=EDITFORM;
		/*
		m_strcommand.Empty();
		m_strcommand=_T("AT+CPBS=");
		m_strcommand.Insert(20,'"');
		m_strcommand+=_T("SM");
		m_strcommand.Insert(30,'"');
		m_strcommand.Insert(30,ch1);
		m_commandtype=AT_COMMAND_ATCPBS;
		SendString(m_strcommand);
		precommand=curcommand;
		curcommand=m_strcommand;*/
		m_wndSplitter.SetActivePane(0,1,NULL);
		(this->GetActiveView())->PostMessage(WM_GETPHONEBOOKLIST,0,0);
	}
	if((message==WM_SETFORM)&&(wParam!=4))
	{
		m_wndSplitter.DeleteView(0, 1);
		m_wndSplitter.CreateView(0, 1, 
		RUNTIME_CLASS(CSmsSetView), 
		CSize(0, 0), 
		NULL);
		m_wndSplitter.RecalcLayout();
		m_currentwindow=SETFORM;
		int len,i;
		len=servercenterphone.GetLength();
		i=0;
		while(i<len)
		{
			serverphone[i]=servercenterphone.GetAt(i);
			i++;
		}
		serverphone[i]=0xff;
		m_wndSplitter.SetActivePane(0,1,NULL);
		this->GetActiveView()->PostMessage(WM_SENDSERVERPHONE,(WPARAM)i,(LPARAM)&serverphone);
	}

	if(message==WM_REQUEST_SMSTEXTSAVE)
	{
		m_strcommand.Empty();
		m_strcommand=_T("");
	}
	if(message==WM_REQUEST_SMSSENDTEXT)
	{
		GetSendOrderData((char*)wParam,(CStringArray *)lParam);
		m_sendsmnum=0;
		m_strcommand.Empty();
		m_strcommand=_T("AT+CMGF=0");
		m_strcommand.Insert(30,ch1);
		m_commandtype=AT_COMMAND_ATCMGF;
		pduexmode=PDUSENDMODE;
		SendString(m_strcommand);
		//m_ComPort.RestartMonitoring();
	}
	if(message==WM_REQUEST_SMSTEXTSAVE)
	{
		GetSendOrderData((char*)wParam,(CStringArray *)lParam);
		m_strcommand.Empty();
		m_strcommand=_T("AT+CMGF=0");
		m_strcommand.Insert(30,ch1);
		m_commandtype=AT_COMMAND_ATCMGF;
		pduexmode=PDUSAVEMODE;
		SendString(m_strcommand);
		//m_ComPort.RestartMonitoring();
	}
	if(message==WM_REQUEST_SMSTEXT)
	{
		cursmindex=(int)wParam;
		m_strcommand.Empty();
		m_strcommand=_T("AT+CMGR=");
		CString smindex;
		GetSubStr(smallindex,(cursmindex-1)*3,(cursmindex-1)*3+2,smindex);
		m_strcommand+=smindex;
		m_strcommand.Insert(30,ch1);
		m_commandtype=AT_COMMAND_ATCMGR;
		SendString(m_strcommand);
		precommand=curcommand;
		curcommand=m_strcommand;
	}
	if(message==WM_REQUEST_SMSDEL)
	{
		cursmindex=(int)wParam;
		m_strcommand.Empty();
		m_strcommand=_T("AT+CMGD=");
		CString smindex;
		GetSubStr(smallindex,(cursmindex-1)*3,(cursmindex-1)*3+2,smindex);
		m_strcommand+=smindex;
		m_strcommand.Insert(30,ch1);
		m_commandtype=AT_COMMAND_ATCMGD;
		SendString(m_strcommand);
		precommand=curcommand;
		curcommand=m_strcommand;
	}/*
	if(message==WM_REQUEST_PHONEBOOKWRITE)
	{
		int optype;
		optype=(int)wParam;
		if((optype==EDITENTRY)||(optype==ADDENTRY))
		{
			m_pbcpbw=(struct cpbw *)lParam;
			m_strcommand.Empty();
			m_strcommand=_T("AT+CPBW=");
			if(optype==EDITENTRY)
				m_strcommand+=m_pbcpbw->indexstr;
			m_strcommand+=_T(",");
			char yinhao;
			yinhao='"';
			m_strcommand.Insert(100,yinhao);
			m_strcommand+=m_pbcpbw->phonestr;
			m_strcommand.Insert(100,yinhao);
			m_strcommand+=_T(",129,");
			m_strcommand.Insert(100,yinhao);
			CString nameunistr;
			AnsiToUni(m_pbcpbw->namestr,nameunistr);
			
			char chff=0xff;
			nameunistr.Insert(50,chff);
			nameunistr.Insert(50,chff);
			m_strcommand+=nameunistr;
			m_strcommand.Insert(100,yinhao);
			m_strcommand.Insert(100,ch1);
			m_commandtype=AT_COMMAND_ATCPBW;
			SendString(m_strcommand);
			precommand=curcommand;
			curcommand=m_strcommand;
		}
	}*/
	if(message==WM_REQUEST_GETSERVERPHONE)
	{
		GetServerPhone((int)wParam,(char *)lParam);
	}
	return CFrameWnd::DefWindowProc(message, wParam, lParam);
}

void CMainFrame::GetServerPhone(int nSize, char serverno[])
{
	int i=0;
	servercenterphone.Empty();
	while(i<nSize)
	{
		servercenterphone.Insert(nSize+1,serverno[i]);
		i++;
	}
	
}

void CMainFrame::SendString(CString str)
{
	m_ComPort.WriteToPort((LPCTSTR)str);
}

void CMainFrame::OnReceive(WPARAM ch, LPARAM port)
{
	CString tempstr;
	static int errorflag=0;
	m_strRXDataCOM2 += (char)ch;
	switch(ch)
	{
	case 0x0D:
		if(m_strRXDataCOM2==curcommand)
			m_strRXDataCOM2=_T("");
		break;
	case 0x1a:
		m_strRXDataCOM2=_T("");
		break;
	case 0x0A:
		if(errorflag)
		{
			//m_ComPort.StopMonitoring();
			errorflag=0;
			m_recstring=m_strRXDataCOM2;
			m_strRXDataCOM2=_T("");
			this->PostMessage(WM_ATCOMM_RECEIVED,m_commandtype,1);
			break;
		}
		else
			if(m_strRXDataCOM2.Right(6)==endflag)
			{
				//m_ComPort.StopMonitoring();
				errorflag=0;
				m_recstring=m_strRXDataCOM2;
				m_strRXDataCOM2=_T("");
				this->PostMessage(WM_ATCOMM_RECEIVED,m_commandtype,0);
				break;
			}
			else if((m_commandtype==AT_COMMAND_ATTESTPIN)
					&&(m_strRXDataCOM2.GetLength()>3))
				{
					//m_ComPort.StopMonitoring();
					errorflag=0;
					m_recstring=m_strRXDataCOM2;
					m_strRXDataCOM2=_T("");
					this->PostMessage(WM_ATCOMM_RECEIVED,m_commandtype,0);
					break;
				}
				else if(m_commandtype==AT_COMMAND_RECSMS)
				{
						//m_ComPort.StopMonitoring();
						m_recstring=m_strRXDataCOM2;
						m_strRXDataCOM2=_T("");
						this->PostMessage(WM_ATCOMM_RECEIVED,m_commandtype,0);
				}
				break;
	case ':':
		if(m_strRXDataCOM2.Right(13)==errorE)
			errorflag=1;
		else
			if(m_strRXDataCOM2.Right(13)==errorS)
				errorflag=2;
			else
			{
				CString recsm;
				recsm.Insert(1,ch1);
				recsm.Insert(2,ch2);
				recsm+=_T("+CMTI:");
				
				if(m_strRXDataCOM2.Right(8)==recsm)
					m_commandtype=AT_COMMAND_RECSMS;
			}
		break;
	case ' ':
		tempstr.Insert(1,ch1);
		tempstr.Insert(2,ch2);
		tempstr.Insert(3,'>');
		tempstr.Insert(4,' ');
		if(m_strRXDataCOM2==tempstr)
		{
			errorflag=0;
			//m_ComPort.StopMonitoring();
			m_recstring=m_strRXDataCOM2;
			m_strRXDataCOM2=_T("");
			this->PostMessage(WM_ATCOMM_RECEIVED,m_commandtype,0);
		}
		break;
	default:
		break;
	}

}

int CMainFrame::OnGetAck(WPARAM command, LPARAM iserror)
{
	static CString num;
	static int nnum;
	command=command;
	iserror=iserror;
	switch(command)	
	{
	case AT_COMMAND_AT:
		if(iserror==0)
		{
			m_strcommand.Empty();
			m_strcommand=_T("AT+CPIN?");
			m_strcommand.Insert(30,ch1);
			m_commandtype=AT_COMMAND_ATTESTPIN;
			SendString(m_strcommand);
			precommand=curcommand;
			curcommand=m_strcommand;
			//m_ComPort.RestartMonitoring();
			break;
		}
		else
		{
			//初始化失败,报告连接错误或重试
			//
			break;
		}
	case AT_COMMAND_ATTESTPIN:
		if(iserror==0)
		{
			CString strnopin;
			strnopin=_T("");
			strnopin.Insert(0,ch1);
			strnopin.Insert(1,ch2);
			strnopin+=_T("+CPIN: READY");
			strnopin.Insert(20,ch1);
			strnopin.Insert(20,ch2);
			if(m_recstring==strnopin)
			{
				m_strcommand.Empty();
				m_strcommand=_T("AT+CNMI=0,1,0,0,0");
				m_strcommand.Insert(m_strcommand.GetLength()+1,ch1);
				m_commandtype=AT_COMMAND_ATCNMI;
				precommand=curcommand;
				curcommand=m_strcommand;
				SendString(m_strcommand);
				//m_ComPort.RestartMonitoring();
				break;
			}
			else
			{
				CSimpinDlg *ppindlg=new CSimpinDlg;
				if(ppindlg->DoModal()==IDOK)
				{
					m_strcommand.Empty();
					m_strcommand=_T("AT+CPIN=");
					m_strcommand.Insert(30,'"');
					m_strcommand+=ppindlg->m_simpin;
					m_strcommand.Insert(50,'"');
					m_strcommand.Insert(30,ch1);
					m_commandtype=AT_COMMAND_ATPIN;
					SendString(m_strcommand);
					precommand=curcommand;
					curcommand=m_strcommand;
					//m_ComPort.RestartMonitoring();
				}
				else
				{
					//用户不能提供SIM密码
				}
				ppindlg->DestroyWindow();
				break;
			}
		}
		else
		{
			//SIM卡打开失败
		}
		break;
	case AT_COMMAND_ATPIN:
		if(iserror==0)
		{
			//SIM卡密码正确,可以进行下一步操作
			m_strcommand.Empty();
			m_strcommand=_T("AT+CNMI=0,1,0,0,0");
			m_strcommand.Insert(m_strcommand.GetLength()+1,ch1);
			m_commandtype=AT_COMMAND_ATCNMI;
			precommand=curcommand;
			curcommand=m_strcommand;
			SendString(m_strcommand);
			break;
		}
		else
		{
			///SIM卡密码不正确
			break;
		}
	case AT_COMMAND_ATCNMI:
		if(iserror==0)
		{
			m_wndSplitter.SetActivePane(0,1,NULL);
			(this->GetActiveView())->PostMessage(WM_GETPHONEBOOKLIST,0,0);
			/*
			m_strcommand.Empty();
			m_strcommand=_T("AT+CPBS=");
			m_strcommand.Insert(20,'"');
			m_strcommand+=_T("SM");
			m_strcommand.Insert(30,'"');
			m_strcommand.Insert(30,ch1);
			m_commandtype=AT_COMMAND_ATCPBS;
			SendString(m_strcommand);
			precommand=curcommand;
			curcommand=m_strcommand;*/
			//m_ComPort.RestartMonitoring();
			break;
		}
		else
		{
			break;
		}
	case AT_COMMAND_ATCMGF:
		if(iserror==0)
		{
			if(pduexmode=PDUSENDMODE)
			{
				//设置PDU模式成功,可发送短消息
				m_strcommand.Empty();
				m_strcommand=_T("AT+CMGS=");
				CString tempstr,unitext;
				int tsum;
				AnsiToUni(messagetext,unitext);
				tsum=unitext.GetLength()+15;
				GetSumString(tsum,tempstr);
				m_strcommand+=tempstr;
				m_strcommand.Insert(m_strcommand.GetLength()+1,ch1);
				m_commandtype=AT_COMMAND_ATCMGS;
				SendString(m_strcommand);
				precommand=curcommand;
				curcommand=m_strcommand;
				//m_ComPort.RestartMonitoring();
				break;
			}
			else if(pduexmode=PDUSAVEMODE)
			{
				//可以发送短消息了
			CString sendpdudata,centeradd,dastring,userdata;
			CString recphone;
			CString pdutext;
			CString headnum=_T("86");
			char ch;
			int i=0;
			//servercenterphone=_T("8613800270500");
			recphone=headnum+dialnumar.GetAt(i);
			//recphone=_T("8613971405741");
			sendpdudata=_T("");
			TransAdd(headnum+servercenterphone,centeradd,SERVERCENTERADD);
			sendpdudata+=centeradd;
			ch=0x11;
			sendpdudata.Insert(sendpdudata.GetLength(),ch);
			ch=0x00;
			sendpdudata.Insert(sendpdudata.GetLength(),ch);
			TransAdd(recphone,dastring,DESTINATIONADD);
			sendpdudata+=dastring;
			ch=0x00;
			sendpdudata.Insert(sendpdudata.GetLength(),ch);
			ch=0x0A;
			sendpdudata.Insert(sendpdudata.GetLength(),ch);
			ch=0x01;
			sendpdudata.Insert(sendpdudata.GetLength(),ch);
			AnsiToUni(messagetext,pdutext);
			ch=pdutext.GetLength();
			sendpdudata.Insert(sendpdudata.GetLength(),ch);
		   	CString ascpdustr;
			pdutext.Empty();
			AnsiToUni(messagetext,pdutext);
			//ascpdustr+=pdutext;
			sendpdudata+=pdutext;
			TransToAscii(sendpdudata,ascpdustr);
			
			sendpdudata=ascpdustr;
			ch=0x1a;
			sendpdudata.Insert(sendpdudata.GetLength(),ch);
			m_commandtype=AT_COMMAND_ATCMGSSEND;
			m_strcommand=sendpdudata;
			SendString(m_strcommand);
			precommand=curcommand;
			curcommand=m_strcommand;
			//m_ComPort.RestartMonitoring();
			break;
			}
		}
		else

⌨️ 快捷键说明

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