📄 smsdemodlg.cpp
字号:
// SMSDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SMSDemo.h"
#include "Comm.h"
#include "ShortMessage.h"
#include "SMSPDU.h"
#include "SMSDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSMSDemoDlg dialog
CSMSDemoDlg * CSMSDemoDlg::pThisDlg = NULL;
CSMSDemoDlg::CSMSDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSMSDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSMSDemoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSMSDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSMSDemoDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSMSDemoDlg, CDialog)
//{{AFX_MSG_MAP(CSMSDemoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_BUTTON_RECEIVE, OnButtonReceive)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSMSDemoDlg message handlers
BOOL CSMSDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_nSendStep = 0;
SetDlgItemText(IDC_EDIT_SMSCENTER, "+8613800200500");
SetDlgItemText(IDC_EDIT_RECEIVER, "");
SetDlgItemText(IDC_EDIT_CONTENT,"我们村穷啊!致富基本靠抢,结婚基本靠想,交通基本靠走,通讯基本靠吼,治安基本靠狗,取暖基本靠抖! 李桂生");
port.Open(this, 1, 19200);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSMSDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSMSDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*
BOOL Send(CComm & comPort, LPCTSTR szCallCenter, LPCTSTR szTarget, LPCTSTR szSMSContent, BOOL bUnicode)
{
char achBuffer[512] = {0};
char strResult[512] = {0};
CString szResult; //查找返回状态
if( bUnicode ) //按照Unicode编码方式发送短信
{
CSMSPDU objPDU;
CString strStream = objPDU.GetEncAddr(szCallCenter, 0);
strStream += conSendSeg;
strStream += objPDU.GetEncAddr(szTarget, 1);
strStream += conUCodeSeg;
strStream += objPDU.GetUCSSegment(szSMSContent);
unsigned int nLen = objPDU.GetStreamLength(strStream);
//设置为Unicode编发方式
wsprintf(achBuffer, "AT+CMGF=0\r");
comPort.Write((void*)achBuffer, strlen(achBuffer));
memset(strResult, 0, 512);
if(!comPort.Read(strResult, 128))
return FALSE;
szResult = strResult;
if(szResult.Find(_T("OK")) == -1)
return FALSE;
//发送短消息内容长度
wsprintf(achBuffer, "AT+CMGS=%d\r", nLen);
comPort.Write((void*)achBuffer, strlen(achBuffer));
memset(strResult, 0, 512);
if(!comPort.Read(strResult, 128))
return FALSE;
szResult = strResult;
if(szResult.Find('>') == -1)
return FALSE;
strcpy(achBuffer, (LPCTSTR)strStream);
nLen = strlen(achBuffer);
if(nLen > 5)
{
achBuffer[nLen] = 26; //追加结束符号
//发送短消息内容
comPort.Write((void*)achBuffer, strlen(achBuffer));
//查找OK或ERROR字符串
memset(strResult, 0, 512);
if(!comPort.Read(strResult, 128))
return FALSE;
szResult = strResult;
while(TRUE)
{
if ( szResult.Find(_T("OK")) != -1 || szResult.Find(_T("ERROR")) != -1 )
break;
else
{
memset(strResult, 0, 512);
if(!comPort.Read(strResult, 128))
return FALSE;
szResult = strResult;
}
}
}
}
else //按照纯英文7位编码方式进行发送
{
//设置为7位编码方式
wsprintf(achBuffer, "AT+CMGF=1\r");
comPort.Write((void*)achBuffer, strlen(achBuffer));
memset(strResult, 0, 512);
if(!comPort.Read(strResult, 128))
return FALSE;
szResult = strResult;
if(szResult.Find(_T("OK")) == -1)
return FALSE;
//设置目标手机号码
wsprintf(achBuffer, "AT+CMGS=\"%s\"\r",szTarget);
comPort.Write((void*)achBuffer, strlen(achBuffer));
memset(strResult, 0, 512);
if(!comPort.Read(strResult, 128))
return FALSE;
szResult = strResult;
if(szResult.Find('>') == -1)
return FALSE;
//纯英文7位方式最多只能发送160个字符
if(strlen(szSMSContent) > 160)
{
strncpy(achBuffer, szSMSContent, 160);
achBuffer[160] = 26;
}
else
wsprintf(achBuffer, "%s%c", szSMSContent, 26);
comPort.Write((void*)achBuffer, strlen(achBuffer));
memset(strResult, 0, 512);
if(!comPort.Read(strResult, 128))
return FALSE;
szResult = strResult;
while(TRUE)
{
if ( szResult.Find(_T("OK")) != -1 || szResult.Find(_T("ERROR")) != -1 )
break;
else
{
memset(strResult, 0, 512);
if(!comPort.Read(strResult, 128))
return FALSE;
szResult = strResult;
}
}
}
//返回
return szResult.Find(_T("OK")) != -1 ? TRUE : FALSE;
}
CString Receive(CComm & comPort, BOOL bUnread, BOOL bDeleteAfterRead)
{
char szCmd[20] = {0};
char szText[4096] = {0};
CString szResult = _T("");
//读取短信
if (bUnread)
strcpy(szCmd, "AT+CMGL=0\r");
else
strcpy(szCmd, "AT+CMGL=4\r");
comPort.Write((void*)szCmd, strlen(szCmd));
memset(szText, 0, 4096);
comPort.Read(szText, 4096);
szResult += szText;
/*
comPort.Read(szText, 2);
//szText.ReleaseBuffer();
szResult += szText;
{
while(TRUE)
{
if ( szResult.Find(_T("OK")) != -1 || szResult.Find(_T("ERROR")) != -1 )
break;
else
{
memset(szText, 0, 4096);
comPort.Read(szText, 4096);
szResult += szText;
}
}
if ( szResult.Find(_T("OK")) ==-1 )
return _T("");
//删除已读短信
if( bDeleteAfterRead )
{
CUIntArray IndexAry; //短信索引值数组
CString szMsg = szResult;
int nPos = szMsg.Find(_T("\r\n"));
szMsg.Delete(0,nPos+2);
nPos = szMsg.Find(_T("+CMGL: "));
while(nPos!=-1)
{
szMsg.Delete(0,nPos+7);
int p = szMsg.Find(_T(","));
if (p!=-1)
{
CString szIndex = szMsg.Left(p);
int nIndex = atoi((LPCTSTR)szIndex);
if(nIndex>0) //从1开始,得到索引值列表
IndexAry.Add((UINT)nIndex);
szMsg.Delete(0,p+1);
nPos = szMsg.Find(_T("+CMGL: "));
}
}
//将索引值从大到小进行排序
for(int i=0;i<IndexAry.GetSize();i++)
{
for(int j=i+1;j<IndexAry.GetSize();j++)
{
UINT s1 = IndexAry[i];
UINT s2 = IndexAry[j];
if(s1<s2)
{
IndexAry[i] = s2;
IndexAry[j] = s1;
}
}
}
//删除短信
for(i=0;i<IndexAry.GetSize();i++)
{
wsprintf(szCmd, "AT+CMGD=%d\r",IndexAry[i]);
comPort.Write((void*)szCmd, strlen(szCmd));
szMsg = _T("");
memset(szText, 0, 4096);
comPort.Read(szText, 4096);
szMsg += szText;
//接收状态
while(TRUE)
{
if ( szMsg.Find(_T("OK")) != -1 || szMsg.Find(_T("ERROR")) != -1 )
break;
else
{
memset(szText, 0, 4096);
comPort.Read(szText, 4096);
szMsg += szText;
}
}
}
}
}
return (LPCTSTR)szResult;
}
*/
void CSMSDemoDlg::OnSend()
{
m_bRead = FALSE;
DoSend();
}
void CSMSDemoDlg::DoSend()
{
// TODO: Add your control notification handler code here
static char szCmd[1024] = {0};
unsigned int nLen = 0;
if(m_nSendStep == 0)
{
wsprintf(szCmd, "AT+CMGF=0\r");
port.WriteToPort(szCmd);
}
else
if(m_nSendStep == 1)
{
if(m_szReceive.Find("OK") != -1)
{
CString szCallCenter;
CString szTarget;
CString szContent;
GetDlgItemText(IDC_EDIT_SMSCENTER, szCallCenter);
GetDlgItemText(IDC_EDIT_RECEIVER, szTarget);
GetDlgItemText(IDC_EDIT_CONTENT, szContent);
szContent.TrimRight();
CSMSPDU objPDU;
nLen = objPDU.GetPDUContent(strStream, szCallCenter, szTarget, szContent);
wsprintf(szCmd, "AT+CMGS=%d\r", nLen);
port.WriteToPort(szCmd);
}
else
AfxMessageBox("发送短信失败");
}
else
if(m_nSendStep == 2)
{
if(m_szReceive.Find('>') != -1)
{
strcpy(szCmd, (LPCTSTR)strStream);
unsigned int nLen = strlen(szCmd);
if(nLen > 5)
{
szCmd[nLen] = 26; //追加结束符号
port.WriteToPort(szCmd);
}
}
else
AfxMessageBox("发送短信失败");
}
else
if(m_nSendStep == 3)
{
m_nSendStep = -1;
if(m_szReceive.Find("OK") != -1)
AfxMessageBox("发送短信成功");
else
AfxMessageBox("发送短信失败");
}
m_nSendStep ++;
}
void CSMSDemoDlg::OnButtonReceive()
{
// TODO: Add your control notification handler code here
m_bRead = TRUE;
DoRecv();
}
void CSMSDemoDlg::DoRecv()
{
// TODO: Add your control notification handler code here
static char szCmd[1024] = {0};
unsigned int nLen = 0;
if(m_nSendStep == 0)
{
wsprintf(szCmd, "AT+CMGL=4\r");
port.WriteToPort(szCmd);
}
else
if(m_nSendStep == 1)
{
m_nSendStep = -1;
if(m_szReceive.Find("OK") != -1)
{
CShortMessage Message;
Message.Open(m_szReceive);
if(Message.GetCount() > 0) //有消息
{
CString szText;
szText.Format("接收短信成功,共收到%d条信息。", Message.GetCount());
AfxMessageBox(szText);
while(!Message.GetEOF())
{
CString szCallCenter = Message.GetCallCenter();
CString szCallFrom = Message.GetCallNumber();
CString szSMSContent = Message.GetSMSContent();
CString szDateTime = Message.GetDateString();
szText.Format("短信中心:\t%s\r\n发送号码:\t%s\r\n短信内容:\t%s\r\n\r\n日期时间:\t%s",
Message.GetCallCenter(), Message.GetCallNumber(), Message.GetSMSContent(), Message.GetDateString());
AfxMessageBox(szText);
Message.MoveNext();
}
}
//delete Message;
}
else
AfxMessageBox("接收短信失败");
}
m_nSendStep ++;
}
void CSMSDemoDlg::SMSCallbackProc(WPARAM wParam, LPARAM lParam)
{
if(pThisDlg)
//pThisDlg->SendMessage(WM_COMM_RXCHAR, wParam, lParam);
pThisDlg->OnCommunication(wParam, lParam);
return;
}
LONG CSMSDemoDlg::OnCommunication(WPARAM ch, LPARAM port)
{
m_szReceive += (char)ch;
if(m_szReceive.Find(_T("\r\n> ")) != -1 ||
m_szReceive.Find(_T("\r\nOK\r\n")) != -1 ||
m_szReceive.Find(_T("\r\nERROR\r\n")) != -1)
{
if(m_bRead)
{
//收取短信
DoRecv();
}
else
{
//发送短信
DoSend();
}
m_szReceive.Empty();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -