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

📄 modemtestdlg.cpp

📁 这是我在学校做通信时留下的资料 希望能给你带来帮助
💻 CPP
字号:
// ModemTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ModemTest.h"
#include "ModemTestDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CModemTestDlg dialog

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

void CModemTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModemTestDlg)
	DDX_Control(pDX, IDC_COMBO_COM, m_ctrlCom);
	DDX_Text(pDX, IDC_EDIT_IP_ADDRESS, m_strIPAddress);
	DDX_Text(pDX, IDC_EDIT_RECEIVE, m_strReceive);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
	DDX_Control(pDX, IDC_MSCOMM1, m_Com);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CModemTestDlg, CDialog)
	//{{AFX_MSG_MAP(CModemTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_DIAL, OnButtonDial)
	ON_BN_CLICKED(IDC_BUTTON_CUT, OnButtonCut)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_OPEN_COM, OnButtonOpenCom)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModemTestDlg message handlers

BOOL CModemTestDlg::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_ctrlCom.SetSelected(0);
	m_bModemConnected = FALSE;
	m_strIPAddress = "127.0.0.1:12345";
	UpdateData(FALSE);
	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 CModemTestDlg::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 CModemTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CModemTestDlg::OnButtonDial() 
{
	if (!m_Com.GetPortOpen())
	{
		AfxMessageBox("串口未打开");
		return;
	}

	// 如果Modem已经连接,则返回
	if (m_bModemConnected)
	{
		return;
	}

	SendString("ATS0=1\r\n");

	UpdateData(TRUE);

	CString strCommand;
	strCommand = "ATDT" + m_strIPAddress + "\r\n";
	SendString(strCommand);
	m_bModemConnected = TRUE;
}

void CModemTestDlg::SendString(CString strSend)
{
	char chData[100];
	int len = strSend.GetLength();

	for (int i = 0; i < len; i++)
	{
		chData[i] = strSend.GetAt(i);
	}
	
	CByteArray array;
	array.RemoveAll();
	array.SetSize(len);

	for (i = 0; i < len; i++)
	{
		array.SetAt(i, chData[i]);
	}

	m_Com.SetOutput(COleVariant(array));
}

void CModemTestDlg::OnButtonCut() 
{
	if (m_bModemConnected)
	{
		SendString("ATH0\r\n");
		m_bModemConnected = FALSE;
		m_Com.SetPortOpen(FALSE);
	}
}

void CModemTestDlg::OnButtonSend() 
{
	if (m_Com.GetPortOpen())
	{
		UpdateData(TRUE);
		SendString(m_strSend + "\r\n");
	}
	else
	{
		AfxMessageBox("串口未打开或Modem未拨号");
	}
}

BEGIN_EVENTSINK_MAP(CModemTestDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CModemTestDlg)
	ON_EVENT(CModemTestDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CModemTestDlg::OnComm() 
{
	LONG len, k;
	VARIANT vInput;
	COleSafeArray saInput;
	BYTE rxdata[2048];
	CString strTemp;

	if (m_Com.GetCommEvent() == 2)			//事件2表示接收缓冲区有字符
	{	
		vInput = m_Com.GetInput();		//读缓冲区
		saInput = vInput;
		len = saInput.GetOneDimSize();		//得到有效数据长度

		for (k = 0; k < len; k++)		//转换为BYTE型数组
		{
			saInput.GetElement(&k, rxdata + k);
			m_strReceive += rxdata[k];
		}
	}

	UpdateData(FALSE);				//更新编辑框内容
}

void CModemTestDlg::OnButtonOpenCom() 
{
	if (m_Com.GetPortOpen())
	{
		m_Com.SetPortOpen(FALSE);
	}

	if (m_ctrlCom.GetCurSel() == 0)
	{
		m_nCom = 10;
	}
	else
	{
		m_nCom = 11;
	}

	m_Com.SetCommPort(m_nCom);
	m_Com.SetInputMode(1);
	m_Com.SetInBufferSize(1024);
	m_Com.SetOutBufferSize(512);

	if (!m_Com.GetPortOpen())
	{
		m_Com.SetPortOpen(TRUE);
	}

	m_Com.SetRThreshold(1);
	m_Com.SetInputLen(0);
	m_Com.GetInput();	
}

⌨️ 快捷键说明

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