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

📄 modemsmsdlg.cpp

📁 一个通过串口把PC机与MODEM连接的程序
💻 CPP
字号:
// ModemSMSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ModemSMS.h"
#include "ModemSMSDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CModemSMSDlg dialog

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

void CModemSMSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModemSMSDlg)
	DDX_Control(pDX, IDC_NUMBER, m_number);
	DDX_Text(pDX, IDC_CONTENT, m_content);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CModemSMSDlg, CDialog)
	//{{AFX_MSG_MAP(CModemSMSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_DIAL, OnDial)
	ON_BN_CLICKED(IDC_SETTINGS, OnSettings)
	ON_BN_CLICKED(IDC_BUTTON_COMOPEN, OnButtonComopen)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_HANGUP, OnHangup)
	ON_BN_CLICKED(IDC_READ, OnRead)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModemSMSDlg message handlers

BOOL CModemSMSDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	UpdateData(TRUE);

	// 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
	//m_comm.gsmInit();
//	m_number = "13776816150";
		
	char *pNum;
	pNum = (char *) malloc(m_number.GetWindowTextLength() + 1);

	if(m_sms.IsEmpty()) 
	//m_sms="13776816150";
	pNum = "13776816150";
	m_number.GetWindowText(pNum,12);
	
//	m_number.GetLBText(-1, m_sms);
	UpdateData(false);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CModemSMSDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		//CAboutDlg dlgAbout;
		m_showdlg.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 CModemSMSDlg::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 CModemSMSDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CModemSMSDlg::OnDial() 
{
	// TODO: Add your control notification handler code here
	
	char *pNum;
	int len;
	/*CString pNumber;
	pNumber = m_number.GetWindowText(m_number);*/
	len = m_number.GetWindowTextLength();
	pNum = (char *) malloc (len + 6);
	m_number.GetWindowText(pNum + 3, len + 1);
	if(m_number.FindString(-1, pNum) < 0)
	m_number.AddString(pNum);
	memmove(pNum, "ATD", 3);
	//memmove(pNum + 3, pNum, strlen(pNum));
	memset(pNum + 3 + len, ';', 1);
	memset(pNum + 4 + len, '\r', 1);
	memset(pNum + 5 + len, 0x00, 1);
	m_comm.WriteComm(pNum,16);
	AfxMessageBox(pNum+3);
	free(pNum);
}

void CModemSMSDlg::OnSettings() 
{
	// TODO: Add your control notification handler code here
	//CAboutDlg dlgsetcom;
	m_showdlg.DoModal();
	
}

void CModemSMSDlg::OnButtonComopen() 
{
	//m_comm.OpenComm(COM1,9600,0,8,1);
	 UpdateData(TRUE);
	m_port.m_strPort="COM1";
	m_comm.OpenComm(m_port.m_strPort,9600,0,8,1);
	m_comm.gsmInit();
	if (m_comm.gsmInit())
	{
		AfxMessageBox("连接成功");
	}
	
	else
	//if (!m_comm.gsmInit())
	{
		AfxMessageBox("该端口上没有发现MODEM!");

		//return FALSE;
	}
	
	
	
 
  UpdateData(FALSE);
}

void CModemSMSDlg::OnSend() 
{
	
	UpdateData(TRUE);
	CString strSmsc;
	CString strNumber;
	CString strContent;
	char *pNumber;

//	UpdateData(TRUE);
	strSmsc = m_showdlg.m_strSmsc;
	pNumber = (char *) malloc(m_number.GetWindowTextLength() + 1);
	m_number.GetWindowText(pNumber, m_number.GetWindowTextLength() +1);
	strNumber.Format("%s", pNumber);
	strContent = m_content;
	if(strNumber.GetLength() < 11)
	{
		AfxMessageBox("请输入正确的号码!");
		//pNumberWnd->SetFocus();
		//pNumberWnd->SetEditSel(-1, 0);
		return;
	}
	if(AfxMessageBox("确定发送吗?", MB_YESNO) == IDYES)
	{
		//SM_PARAM SmParam;

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

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

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

		// 填充短消息结构
		strcpy(SmParam.SCA, strSmsc);
		strcpy(SmParam.TPA, strNumber);
		strcpy(SmParam.TP_UD, strContent);
		SmParam.TP_PID = 0;
		SmParam.TP_DCS = GSM_UCS2;
		
		// 发送短消息
		m_gsmcomm.gsmSendMessage(&SmParam);
	
	}
	
	UpdateData(false);
	free(pNumber);
	
}

void CModemSMSDlg::OnHangup() 
{
	// TODO: Add your control notification handler code here
	char *pNum;
	pNum = (char *) malloc (4);
	memmove(pNum, "ATH", 3);
	memset(pNum + 3, '\r', 1);
	m_comm.WriteComm(pNum,4);
	free(pNum);
}

void CModemSMSDlg::OnRead() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_gsmcomm.GetRecvMessage(&SmParam);
	UpdateData(false);
}

⌨️ 快捷键说明

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