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

📄 newatdlg.cpp

📁 VisualC++通信编程工程实例精解 Chapter 2 Example 1 MSCOMM控件编程实例 Example 2 基于Windows API的虚拟终端实现 Example 3
💻 CPP
字号:
// NewATDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NewAT.h"
#include "NewATDlg.h"
#include "ysatmodem.h"
#include "statedlg.h"

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

UINT CNewATDlg::GetStateThread(PVOID pLaram)
{
	CNewATDlg* dlg=(CNewATDlg*)pLaram;
	CString s;
	while(!gbExit)
	{
		s=dlg->m_ATModem->GetStateDesc();
		::PostMessage(dlg->m_stateDlg->m_hWnd,WM_SHOWSTATE,(DWORD)&s,NULL);
//		TRACE("当前CYsATModem状态:  ");
//		TRACE(s+"\n");
		Sleep(1000);
	}
	return 999;
}

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

/////////////////////////////////////////////////////////////////////////////
// CNewATDlg dialog

CNewATDlg::CNewATDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNewATDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNewATDlg)
	m_iComPort = 0;
	m_sRead = _T("");
	m_iTimeOut = 1000;
	m_iBufSize = 10;
	m_sWrite = _T("");
	m_sDialNumber = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_ATModem=new CYsATModem;
	m_stateDlg=new CStateDlg;
}

void CNewATDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNewATDlg)
	DDX_Radio(pDX, IDC_RADIO_COM1, m_iComPort);
	DDX_Text(pDX, IDC_EDIT_READ, m_sRead);
	DDX_Text(pDX, IDC_EDIT_TIMEOUT, m_iTimeOut);
	DDV_MinMaxInt(pDX, m_iTimeOut, 0, 2147483647);
	DDX_Text(pDX, IDC_EDIT_BUFSIZE, m_iBufSize);
	DDV_MinMaxInt(pDX, m_iBufSize, 0, 2147483647);
	DDX_Text(pDX, IDC_EDIT_WRITE, m_sWrite);
	DDX_Text(pDX, IDC_EDIT_DIALNUMBER, m_sDialNumber);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNewATDlg, CDialog)
	//{{AFX_MSG_MAP(CNewATDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_INITALIZE, OnButtonInitalize)
	ON_BN_CLICKED(IDC_BUTTON_DIAL, OnButtonDial)
	ON_BN_CLICKED(IDC_BUTTON_HANGUP, OnButtonHangup)
	ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
	ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
	ON_BN_CLICKED(IDC_BUTTON_WAITRING, OnButtonWaitring)
	ON_BN_CLICKED(IDC_BUTTON_STOPSTATE, OnButtonStopstate)
	ON_WM_MOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewATDlg message handlers
CWinThread* stateThread;
BOOL gInit=FALSE;
BOOL CNewATDlg::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
	m_stateDlg->Create(IDD_DIALOG1);
	m_stateDlg->ShowWindow(SW_SHOW);
	gInit=TRUE;
	stateThread=AfxBeginThread(GetStateThread,(PVOID)this);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CNewATDlg::OnButtonInitalize() 
{
	UpdateData();
	if(!m_ATModem->InitModem(m_iComPort+1,1200,2,m_hWnd))
	{
		AfxMessageBox(m_ATModem->GetLastError());
		return;
	}
	CString s;
	s.Format(_T(" Com%d "),m_iComPort+1);
	m_sDialNumber.Format(_T("80%d"),m_iComPort==0?2:1);
	UpdateData(FALSE);
	this->SetWindowText(s);

}

CString gDial;

UINT DialThread(LPVOID dd)
{	
	CYsATModem * pAT=(CYsATModem*)dd;
	DWORD time=GetTickCount();
	if(!pAT->DialUp(gDial))
		AfxMessageBox(pAT->GetLastError());
	TRACE("dial use %ld minsecond\n",GetTickCount()-time);
	return 1;
}


void CNewATDlg::OnButtonDial() 
{
	UpdateData();
	gDial=m_sDialNumber;
	AfxBeginThread(DialThread,m_ATModem);
}

void CNewATDlg::OnButtonHangup() 
{
	if(!m_ATModem->HangUp())
	{
		AfxMessageBox("挂断操作失败");
	}

}

LRESULT CNewATDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	switch(message)
	{
	case WM_MODEM_CHANGE:
		TRACE("Message:Com%d Modem Change State=%d \n",lParam,wParam);
		break;
	}	
	return CDialog::WindowProc(message, wParam, lParam);
}

TCHAR* buf;

void CNewATDlg::OnButtonRead() 
{
	UpdateData();
	DWORD dw;
	
	buf=new TCHAR[m_iBufSize+10];
	int i=m_ATModem->Read(buf,m_iBufSize,&dw,m_iTimeOut);
	if(i!=1&&i!=-2) 
	{
		TRACE("读数出错%d\n",i);
		return;
	}
	buf[dw]='\0';
	CString sTemp;
	sTemp.Format("%s",buf);
	m_sRead+=sTemp;
	UpdateData(FALSE);
}


void CNewATDlg::OnButtonWrite() 
{
	UpdateData();
	int i=m_ATModem->Write(m_sWrite,m_sWrite.GetLength());
	if(i!=1) TRACE("Write Error %d\n",i);
}


void CNewATDlg::OnButtonWaitring() 
{
	UpdateData();
}

void CNewATDlg::OnCancel() 
{
	gbExit=TRUE;
	Sleep(1000);
	delete buf;
	delete m_ATModem;
	delete m_stateDlg;
	CDialog::OnCancel();
}

void CNewATDlg::OnButtonStopstate() 
{
	TerminateThread(stateThread->m_hThread,-99);	
}

void CNewATDlg::OnMove(int x, int y) 
{
	CDialog::OnMove(x, y);
	if(gInit)
	{
		CRect rect;
		(GetDlgItem(IDC_STATIC_STATE))->GetWindowRect(&rect);
	//	rect+=CPoint(x,y);
		m_stateDlg->SetWindowPos(NULL,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,0);
	}
}

⌨️ 快捷键说明

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