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

📄 datacard_smsdlg.cpp

📁 通过接入PC的数据卡发送SMS短信的小工具
💻 CPP
字号:
// datacard_smsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "datacard_sms.h"
#include "datacard_smsDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDatacard_smsDlg dialog

CDatacard_smsDlg::CDatacard_smsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDatacard_smsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDatacard_smsDlg)
	m_strDestNo = _T("");
	m_strSmsContent = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDatacard_smsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDatacard_smsDlg)
	DDX_Control(pDX, IDC_LIST_STATUS, m_Status);
	DDX_Text(pDX, IDC_EDIT_DSTNO, m_strDestNo);
	DDX_Text(pDX, IDC_EDIT_SMSCONTENT, m_strSmsContent);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDatacard_smsDlg, CDialog)
	//{{AFX_MSG_MAP(CDatacard_smsDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_BUTTON_OPENPORT, OnButtonOpenport)
	ON_BN_CLICKED(ID_BUTTON_STARTWIRELESS, OnButtonStartwireless)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDatacard_smsDlg message handlers

BOOL CDatacard_smsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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

	char cmd[16];
	int nLength;
	char ans[128];		// 应答串

	// 打开端口
	m_Status.AddString((CString)("Opening ComPort!"));
	if (OnButtonOpenport()) {
		/*
		    If MSComm1.PortOpen = True Then
        If cmdShutDown.Caption = "关机(&U)" Then
            MSComm1.Output = "AT+CPOF" & vbCr '"AT+CFUN=0" & vbCr
            cmdShutDown.Caption = "开机(&U)"
'            MSComm1.RTSEnable = False
        Else
            MSComm1.Output = "AT+CFUN=1" & vbCr
            cmdShutDown.Caption = "关机(&U)"
'            MSComm1.RTSEnable = True
        End If
    End If
	*/
		// 开机
		m_Status.AddString("Opening wireless!");
		sprintf(cmd, "AT+CFUN=1\r");
		WriteComm(cmd, strlen(cmd));

		nLength = ReadComm(ans, 128);	// 读应答数据
		TRACE("%d", nLength);
		if (strstr(ans, "OK") == NULL) {
			/*
			sprintf(cmd, "%s", "AT+CPOF\r");
			WriteComm(cmd, strlen(cmd));
			Sleep(1000);
			*/
		}
	}

	m_pSmsTraffic = new CSmsTraffic;  // 启动进程管理
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDatacard_smsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CDatacard_smsDlg::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 CDatacard_smsDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDatacard_smsDlg::OnOK() 
{
	// TODO: Add extra validation here
	// 发送消息
	CString strDestNo = m_strDestNo;
	CString strSmsContent = m_strSmsContent;
	m_Status.AddString("Sending!");
	SendSms(strDestNo, strSmsContent);
	
	//CDialog::OnOK();
}

void CDatacard_smsDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	// 关机
	char cmd[16];

	sprintf(cmd, "AT+CPOF\r");
	WriteComm(cmd, strlen(cmd));

	delete m_pSmsTraffic;

	::CloseComm();
	
	CDialog::OnCancel();
}

void CDatacard_smsDlg::SendSms(CString strDestNo, CString strSmsContent)
{
	CString strSmsc = DATACARD_LOCALNO;

	// 检查号码
	if(strDestNo.GetLength() < 11)
	{
		AfxMessageBox("请输入正确的号码!");
		return;
	}

	// 检查短消息内容是否空,或者超长
	CString strUnicode;
	WCHAR wchar[1024];
	int nCount = ::MultiByteToWideChar(CP_ACP, 0, strSmsContent, -1, wchar, 1024);
	if(nCount <= 1)
	{
		AfxMessageBox("请输入消息内容!");
		return;
	}
	else if(nCount > 70)		// 决定全部用UCS2编码,最大70个字符(半角/全角)
	{
		AfxMessageBox("消息内容太长,无法发送!");
		return;
	}

	if(AfxMessageBox("确定发送吗?", MB_YESNO) == IDYES)
	{
		SM_PARAM SmParam;

		::memset(&SmParam, 0, sizeof(SM_PARAM));

		// 去掉号码前的"+"
		if(strDestNo[0] == '+')  strDestNo = strDestNo.Mid(1);
		if(strSmsContent[0] == '+')  strSmsContent = strSmsContent.Mid(1);

		// 在号码前加"86"
		if(strSmsc.Left(2) != "86")  strSmsc = "86" + strSmsc;
		if(strDestNo.Left(2) != "86")  strDestNo = "86" + strDestNo;

		// 填充短消息结构
		::strcpy(SmParam.SCA, strSmsc);
		::strcpy(SmParam.TPA, strDestNo);
		::strcpy(SmParam.TP_UD, strSmsContent);
		SmParam.TP_PID = 0;
		SmParam.TP_DCS = GSM_UCS2;

		// 发送短消息
		this->m_pSmsTraffic->PutSendMessage(&SmParam);
	}
}

BOOL CDatacard_smsDlg::OnButtonOpenport() 
{
	// TODO: Add your control notification handler code here
	if (!::OpenComm(DATACARD_COMPORT, DATACARD_BITRAT))
	{
		CString strError;
		strError.Format("无法打开端口%s!", DATACARD_COMPORT);
		m_Status.AddString(strError);
		return false;
	} else {
		return true;
	}
}

BOOL CDatacard_smsDlg::OnButtonStartwireless() 
{
	// TODO: Add your control notification handler code here
	return true;
}

⌨️ 快捷键说明

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