📄 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, "+8613800758500");
SetDlgItemText(IDC_EDIT_RECEIVER, "");
SetDlgItemText(IDC_EDIT_CONTENT,"我们村穷啊!致富基本靠抢,结婚基本靠想,交通基本靠走,通讯基本靠吼,治安基本靠狗,取暖基本靠抖! 李桂生");
port.Open(this, 1, 9600);
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;
}
void CSMSDemoDlg::OnSend()
{
m_bRead = FALSE;
DoSend();
}
void CSMSDemoDlg::DoSend()
{//MessageBox("s");
// TODO: Add your control notification handler code here
static char szCmd[1024] = {0};
unsigned int nLen = 0;
if(m_nSendStep == 0)
{
wsprintf(szCmd, "ATE\r");//表示已连接上
port.WriteToPort(szCmd);
}
else
if(m_nSendStep == 1)
{
if(m_szReceive.Find("OK") != -1)
{
wsprintf(szCmd, "AT+CMGF=0\r");//表示采用PDU
port.WriteToPort(szCmd);
}
else
AfxMessageBox("发送短信失败0");
}
else
if(m_nSendStep == 2)
{
if(m_szReceive.Find("OK") != -1)
{
wsprintf(szCmd, "AT+CSMS=0\r");//检测手机是否支持SMS命令
port.WriteToPort(szCmd);
}
else
AfxMessageBox("发送短信失败1");
}
else
if(m_nSendStep == 3)
{
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);
/* CString s;
s.Format("%d",nLen);
MessageBox(s);*/
//wsprintf(szCmd,"AT+CSMS=0\r");
wsprintf(szCmd, "AT+CMGS=%d\r", nLen);//表发送短信息
port.WriteToPort(szCmd);
}
else
AfxMessageBox("发送短信失败2");
}
else
if(m_nSendStep == 4)
{
if(m_szReceive.Find('>') != -1)
{
strcpy(szCmd, (LPCTSTR)strStream);
unsigned int nLen = strlen(szCmd);
if(nLen > 5)
{
szCmd[nLen] = 26; //追加结束符号
port.WriteToPort(szCmd);//发送信息内容
//MessageBox(szCmd);
}
}
else
AfxMessageBox("发送短信失败3");
}
else
if(m_nSendStep == 5)
{
m_nSendStep = -1;
if(m_szReceive.Find("OK") != -1)
{//MessageBox(szCmd);
AfxMessageBox("发送短信成功");
}
else
AfxMessageBox("发送短信失败4");
}
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");//列出SIM卡中短信息
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 + -