www.pudn.com > smsinterface.rar > MainFrm.cpp
// 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#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 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 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 { //设置PDU模式失败 break; } case AT_COMMAND_ATCMGS: if(iserror==0) { //可以发送短消息了 CString sendpdudata,centeradd,dastring,userdata; CString recphone; CString pdutext; CString headnum=_T("86"); char ch; int i; i=m_sendsmnum; //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 { break; } case AT_COMMAND_ATCMGSSEND: if(iserror==0) { //发送成功 iserror=iserror; m_sendsmnum++; if(m_sendsmnum GetActiveView()->PostMessage(WM_GETSMTEXT, (smallindex.GetLength())/3,(LPARAM)&m_smdata); break; } else { CReadSMDlg readsmdlg; int dlgflag; if(m_smdata.readflag) readsmdlg.m_smtype=_T("已读消息"); else readsmdlg.m_smtype=_T("未读消息"); readsmdlg.m_smphone=m_smdata.senddialnum; readsmdlg.m_smtime=m_smdata.timestr; readsmdlg.m_smtext=m_smdata.smtext; dlgflag=readsmdlg.DoModal(); m_strcommand.Empty(); m_strcommand=_T("AT+CMGL"); m_strcommand.Insert(30,ch1); m_commandtype=AT_COMMAND_ATCMGLTOTOAL; precommand=curcommand; curcommand=m_strcommand; m_isfirstlist=0; GetSubStr(smallindex,(cursmindex-1)*3,(cursmindex-1)*3+2, cursmindexstr); SendString(m_strcommand); } } break; } else { break; } } else { break; } case AT_COMMAND_ATCMGD: if(iserror==0) { AfxMessageBox("当前消息已被成功删除!",MB_OK); 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=0; smallindex.Delete((cursmindex-1)*3,3); if(smallindex.GetLength()>0) { cursmindexstr.Empty(); if((cursmindex*3)>smallindex.GetLength()) GetSubStr(smallindex,smallindex.GetLength()-3, smallindex.GetLength()-1,cursmindexstr); else GetSubStr(smallindex,(cursmindex-1)*3,(cursmindex-1)*3+2, cursmindexstr); } break; } else { break; } /* case AT_COMMAND_ATCPBS: if(iserror==0) { m_strcommand.Empty(); m_strcommand=_T("AT+CPBS?"); m_strcommand.Insert(30,ch1); m_commandtype=AT_COMMAND_ATCPBSINFO; SendString(m_strcommand); precommand=curcommand; curcommand=m_strcommand; //m_ComPort.RestartMonitoring(); break; } else { //打开SIM卡电话簿时出错 break; } case AT_COMMAND_ATCPBSINFO: if(iserror==0) { CStringArray arcpbsinfo; BOOL flagcpbs; flagcpbs=GetAnswerData(m_recstring,_T("+CPBS:"),arcpbsinfo); if(flagcpbs) { m_pbusedloc=atoi(arcpbsinfo[1]); m_pbset.usedloc=m_pbusedloc; m_pbtotalloc=atoi(arcpbsinfo[2]); m_pbset.totalloc=m_pbtotalloc; m_strcommand.Empty(); m_strcommand=_T("AT+CPBR=?"); m_strcommand.Insert(30,ch1); m_commandtype=AT_COMMAND_ATCPBRLEN; SendString(m_strcommand); precommand=curcommand; curcommand=m_strcommand; } break; } else { break; } case AT_COMMAND_ATCPBRLEN: if(iserror==0) { CStringArray arans; BOOL flag; flag=GetAnswerData(m_recstring,_T("+CPBR:"),arans); if(flag) { CString strnum,str1,str2,str3,strsi,strei; int startindex,endindex,maxphonenum,maxassotext,pos; str1=arans[0]; str2=arans[1]; str3=arans[2]; str1.Delete(0,2); pos=str1.Find('-',0); GetSubStr(str1,0,pos-1,strnum); startindex=atoi(strnum); m_pbsindex=startindex; m_pbset.startindex=m_pbsindex; strsi=strnum; str1.Delete(0,pos+1); pos=str1.Find(')',0); strnum.Empty(); GetSubStr(str1,0,pos-1,strnum); endindex=atoi(strnum); m_pbdindex=endindex; m_pbset.endindex=m_pbdindex; strei=strnum; maxphonenum=atoi(str2); m_pbphonemaxlen=maxphonenum; m_pbset.maxphonelen=maxphonenum; maxassotext=atoi(str3); m_pbset.maxnamelen=maxassotext; m_pbnamemaxlen=maxassotext; m_strcommand.Empty(); m_strcommand=_T("AT+CPBR="); m_strcommand+=strsi; m_strcommand.Insert(30,','); m_strcommand+=strei; m_strcommand.Insert(30,ch1); m_commandtype=AT_COMMAND_ATCPBRLIST; SendString(m_strcommand); precommand=curcommand; curcommand=m_strcommand; //m_ComPort.RestartMonitoring(); break; } else { //得到电话簿条目总数时出错 break; } } else { //查询SIM卡电话簿总条目时出错 break; } case AT_COMMAND_ATCPBRLIST: if(iserror==0) { m_strphonebook.RemoveAll(); BOOL flaglist; flaglist=GetAnswerData(m_recstring,_T("+CPBR:"),m_strphonebook); if(flaglist) { m_wndSplitter.SetActivePane(0,1,NULL); (this->GetActiveView())->PostMessage(WM_GETPHONEBOOKLIST, (WPARAM)&m_pbset,(LPARAM)&m_strphonebook); break; } else { //无法得到电话簿列表 } } else { //得到电话簿列表时出错 break; } break; */ case AT_COMMAND_ATCMGLTOTOAL: if(iserror==0) { smallindex.Empty(); int pos=m_recstring.Find(':',0); while(pos>=0) { CString oneindex; GetSubStr(m_recstring,pos+2,pos+4,oneindex); smallindex+=oneindex; m_recstring.Delete(0,pos+5); pos=m_recstring.Find(':',0); } int smlen=smallindex.GetLength()/3; if(smlen!=0) { CString notestr; notestr.Format("总共存储了%d条短消息",smlen); if(m_isfirstlist) { AfxMessageBox(notestr,MB_OK); cursmindex=1; } else { pos=smallindex.Find(cursmindexstr,0); cursmindex=(pos/3)+1; } m_strcommand.Empty(); m_strcommand=_T("AT+CMGR="); CString smindex; if(m_isfirstlist) GetSubStr(smallindex,(cursmindex-1)*3,(cursmindex-1)*3+2,smindex); else smindex=cursmindexstr; m_strcommand+=smindex; m_strcommand.Insert(30,ch1); m_commandtype=AT_COMMAND_ATCMGR; SendString(m_strcommand); precommand=curcommand; curcommand=m_strcommand; } else AfxMessageBox("存储器中无短信息",IDOK); break; } else { break; } /* case AT_COMMAND_ATCPBW: if(iserror==0) { m_strcommand.Empty(); m_strcommand=_T("AT+CPBS?"); m_strcommand.Insert(30,ch1); m_commandtype=AT_COMMAND_ATCPBSINFO; SendString(m_strcommand); precommand=curcommand; curcommand=m_strcommand; } else { }*/ } return(iserror); } BOOL CMainFrame::GetAnswerData(CString answerstr, CString headstr, CStringArray &resultarray) { CString astr,fstr,substr; fstr.Empty(); fstr.Insert(0,ch1); fstr.Insert(1,ch2); astr=answerstr; int pos,fpos,i; pos=astr.Find(headstr,0); if(pos<0) return(FALSE); while(pos>=0) { substr.Empty(); astr.Delete(0,2+headstr.GetLength()); fpos=astr.Find(fstr,0); i=0; GetSubStr(astr,0,fpos,substr); astr.Delete(0,fpos); i=0; do { fpos=substr.Find(',',0); int j=0; if(fpos<0) { CString tempstr2; substr.Delete(substr.GetLength()-1,1); tempstr2=substr; resultarray.Add(tempstr2); } else { CString tempstr; for(j=0;j =0); pos=astr.Find(headstr,0); } return(TRUE); } void CMainFrame::GetSubStr(CString str, int sindex, int eindex,CString& substr) { int endindex,startindex; if((sindex>=str.GetLength())||(sindex>eindex)) { substr=_T(""); return; } startindex=sindex; if(eindex>=str.GetLength()) endindex=str.GetLength()-1; else endindex=eindex; int i; for(i=startindex;i<=endindex;i++) substr.Insert(100,str.GetAt(i)); } void CMainFrame::Decode(CString sdata, CString &dedata) { CString tempstr,ss; int len=sdata.GetLength(); int tempi; tempi=0; while(tempi >8; pseptet[1]=x; test=pseptet[1]; temp=poctet[1]; high=temp&0xc0; temp=poctet[2]; low=temp&0x1f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<2; x=x>>8; pseptet[2]=x; temp=poctet[2]; high=temp&0xe0; temp=poctet[3]; low=temp&0x0f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<3; x=x>>8; pseptet[3]=x; temp=poctet[3]; high=temp&0xf0; temp=poctet[4]; low=temp&0x07; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<4; x=x>>8; pseptet[4]=x; temp=poctet[4]; high=temp&0xf8; temp=poctet[5]; low=temp&0x03; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<5; x=x>>8; pseptet[5]=x; temp=poctet[5]; high=temp&0xfc; temp=poctet[6]; low=temp&0x01; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<6; x=x>>8; pseptet[6]=x; temp=poctet[6]; high=temp&0xfe; char z=high; z=z>>1; z=z&0x7f; pseptet[7]=z; pseptet[8]='\0'; } if(remainder==1){ char temp=poctet[0]; char high=temp&0x80; temp=poctet[0]; char low=temp&0x7f; pseptet[0]=low; pseptet[1]='\0'; } if(remainder==2){ char test; char temp=poctet[0]; char high=temp&0x80; temp=poctet[0]; char low=temp&0x7f; pseptet[0]=low; test=pseptet[0]; temp=poctet[1]; low=temp&0x3f; WORD x,y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<1; x=x>>8; pseptet[1]=x; test=pseptet[1]; pseptet[2]='\0'; } if(remainder==3){ char test; char temp=poctet[0]; char high=temp&0x80; temp=poctet[0]; char low=temp&0x7f; pseptet[0]=low; test=pseptet[0]; temp=poctet[1]; low=temp&0x3f; WORD x,y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<1; x=x>>8; pseptet[1]=x; test=pseptet[1]; temp=poctet[1]; high=temp&0xc0; temp=poctet[2]; low=temp&0x1f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<2; x=x>>8; pseptet[2]=x; pseptet[3]='\0'; } if(remainder==4){ char test; char temp=poctet[0]; char high=temp&0x80; temp=poctet[0]; char low=temp&0x7f; pseptet[0]=low; test=pseptet[0]; temp=poctet[1]; low=temp&0x3f; WORD x,y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<1; x=x>>8; pseptet[1]=x; test=pseptet[1]; temp=poctet[1]; high=temp&0xc0; temp=poctet[2]; low=temp&0x1f; x,y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<2; x=x>>8; pseptet[2]=x; temp=poctet[2]; high=temp&0xe0; temp=poctet[3]; low=temp&0x0f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<3; x=x>>8; pseptet[3]=x; pseptet[4]='\0'; } if(remainder==5){ char test; char temp=poctet[0]; char high=temp&0x80; temp=poctet[0]; char low=temp&0x7f; pseptet[0]=low; test=pseptet[0]; temp=poctet[1]; low=temp&0x3f; WORD x,y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<1; x=x>>8; pseptet[1]=x; test=pseptet[1]; temp=poctet[1]; high=temp&0xc0; temp=poctet[2]; low=temp&0x1f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<2; x=x>>8; pseptet[2]=x; temp=poctet[2]; high=temp&0xe0; temp=poctet[3]; low=temp&0x0f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<3; x=x>>8; pseptet[3]=x; temp=poctet[3]; high=temp&0xf0; temp=poctet[4]; low=temp&0x07; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<4; x=x>>8; pseptet[4]=x; pseptet[5]='\0'; } if(remainder==6){ char test; char temp=poctet[0]; char high=temp&0x80; temp=poctet[0]; char low=temp&0x7f; pseptet[0]=low; test=pseptet[0]; temp=poctet[1]; low=temp&0x3f; WORD x,y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<1; x=x>>8; pseptet[1]=x; test=pseptet[1]; temp=poctet[1]; high=temp&0xc0; temp=poctet[2]; low=temp&0x1f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<2; x=x>>8; pseptet[2]=x; temp=poctet[2]; high=temp&0xe0; temp=poctet[3]; low=temp&0x0f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<3; x=x>>8; pseptet[3]=x; temp=poctet[3]; high=temp&0xf0; temp=poctet[4]; low=temp&0x07; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<4; x=x>>8; pseptet[4]=x; temp=poctet[4]; high=temp&0xf8; temp=poctet[5]; low=temp&0x03; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<5; x=x>>8; pseptet[5]=x; pseptet[6]='\0'; } if(remainder==7){ char test; char temp=poctet[0]; char high=temp&0x80; temp=poctet[0]; char low=temp&0x7f; pseptet[0]=low; test=pseptet[0]; temp=poctet[1]; low=temp&0x3f; WORD x,y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<1; x=x>>8; pseptet[1]=x; test=pseptet[1]; temp=poctet[1]; high=temp&0xc0; temp=poctet[2]; low=temp&0x1f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<2; x=x>>8; pseptet[2]=x; temp=poctet[2]; high=temp&0xe0; temp=poctet[3]; low=temp&0x0f; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<3; x=x>>8; pseptet[3]=x; temp=poctet[3]; high=temp&0xf0; temp=poctet[4]; low=temp&0x07; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<4; x=x>>8; pseptet[4]=x; temp=poctet[4]; high=temp&0xf8; temp=poctet[5]; low=temp&0x03; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<5; x=x>>8; pseptet[5]=x; temp=poctet[5]; high=temp&0xfc; temp=poctet[6]; low=temp&0x01; x=0; y=0; x=low; x=x<<8; y=y|high; y=y&0x00ff; x=x|y; x=x<<6; x=x>>8; pseptet[6]=x; pseptet[7]='\0'; } } void CMainFrame::EncodePdu(CString sstring, CString &dstring) { int i,j,len; char cur; dstring=_T(""); len=sstring.GetLength(); i=1; j=0; while(i > j) | ((sstring.GetAt(i+1) << (7-j)) & 0xff); else cur=(sstring.GetAt(i) >> j) & 0x7f; dstring.Insert(dstring.GetLength()+1,cur); i++; j=(j+1) % 7; if(j==0) i++; } } void CMainFrame::GetSumString(int sum, CString &string) { int ge,shi,bai; char ch; ge=sum % 10; sum=sum / 10; shi=sum % 10; sum=sum / 10; bai=bai % 10; ch=0x30+bai; string.Insert(string.GetLength()+1,ch); ch=0x30+shi; string.Insert(string.GetLength()+1,ch); ch=0x30+ge; string.Insert(string.GetLength()+1,ch); } void CMainFrame::TransAdd(CString sadd, CString &dadd,int ntype) { char weishu,ctype,ch,chh,chl; char hbyte,lbyte; ctype=0x91; if(ntype==SERVERCENTERADD) weishu=(sadd.GetLength()+3)/2; else weishu=sadd.GetLength(); dadd.Insert(1,weishu); dadd.Insert(2,ctype); int i,len; len=sadd.GetLength(); i=0; while(i =0x30)&&(ch<=0x39)) t=ch-0x30; else t=ch-55; sum=sum*16+t; } return(sum); } void CMainFrame::decodedialnum(CString sdialnum, CString &ddialnum) { int i,len; char cha,chb; len=sdialnum.GetLength(); i=0; while(i =0x100) { result=1; gfile.Seek(0,CFile::begin); ansistr=0; while(result) { result=gfile.Read(&tempch,2); if(result) if(tempch==wch) { gfile.Read(&tempch,2); ansistr=tempch; break; } else result=gfile.Read(&tempch,2); } if(!result) ansistr=0xB0A1; char ch; ch=ansistr / 0x100; dstr.Insert(dstr.GetLength(),ch); ch=ansistr % 0x100; dstr.Insert(dstr.GetLength(),ch); } else { char chansi; chansi=wch % 0x100; dstr.Insert(dstr.GetLength(),chansi); } i+=4; } gfile.Close(); }