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

📄 comvs_2005dlg.cpp

📁 用VS_2005做的串口例程源码(Pocket PC 2003 (ARMV4)) 应用程序可以在WINCE5.0以上系统直接运行。
💻 CPP
字号:
// ComVS_2005Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ComVS_2005.h"
#include "ComVS_2005Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CComVS_2005Dlg dialog

CComVS_2005Dlg::CComVS_2005Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CComVS_2005Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CComVS_2005Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO2,m_PortComBox);
	DDX_Control(pDX, IDC_COMBO1,m_BaudRateComBox);
	DDX_Control(pDX, IDC_EDIT1,m_ReadEdit);
	DDX_Control(pDX, IDC_EDIT2,m_SendEdit);
}

BEGIN_MESSAGE_MAP(CComVS_2005Dlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CComVS_2005Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CComVS_2005Dlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CComVS_2005Dlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CComVS_2005Dlg::OnBnClickedButton4)
END_MESSAGE_MAP()


// CComVS_2005Dlg message handlers

BOOL CComVS_2005Dlg::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

#ifdef WIN32_PLATFORM_WFSP
	if (!m_dlgCommandBar.Create(this) ||
	    !m_dlgCommandBar.InsertMenuBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create CommandBar\n");
		return FALSE;      // fail to create
	}
#endif // WIN32_PLATFORM_WFSP
	// TODO: Add extra initialization here
	//设定combox
	for(int i=1;i<10;i++)
	{
		CString str;
		str.Format(_T("COM%d"),i);
		m_PortComBox.AddString(str);
	}
	m_BaudRateComBox.InsertString(0,_T("110"));
	m_BaudRateComBox.InsertString(1,_T("300"));
	m_BaudRateComBox.InsertString(2,_T("600"));
	m_BaudRateComBox.InsertString(3,_T("1200"));
	m_BaudRateComBox.InsertString(4,_T("2400"));
	m_BaudRateComBox.InsertString(5,_T("4800"));
	m_BaudRateComBox.InsertString(6,_T("9600"));
	m_BaudRateComBox.InsertString(7,_T("14400"));
	m_BaudRateComBox.InsertString(8,_T("19200"));
	m_BaudRateComBox.InsertString(9,_T("38400"));
	m_BaudRateComBox.InsertString(10,_T("57600"));
	m_BaudRateComBox.InsertString(11,_T("115200"));
	m_BaudRateComBox.InsertString(12,_T("128000"));
	m_BaudRateComBox.InsertString(13,_T("256000"));
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CComVS_2005Dlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	DRA::RelayoutDialog(
		AfxGetInstanceHandle(), 
		this->m_hWnd, 
		DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_COMVS_2005_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_COMVS_2005_DIALOG));
}
#endif

//发送
void CComVS_2005Dlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	CString str;
	m_SendEdit.GetWindowTextW(str);
	m_pComTest->WriteTTYBlock(str,str.GetLength());
}
//打开串口
void CComVS_2005Dlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	//创建等待线程,设置串行端口参数
	m_pComTest=(CComTest *)AfxBeginThread(RUNTIME_CLASS(CComTest),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	//	::CreateThread(NULL,0,m_pComTest->f,NULL,CREATE_SUSPENDED,NULL);
	DWORD dw=GetLastError();
	if((m_pComTest==NULL)||(m_fLineCom==TRUE))
	{
		m_fLineCom=FALSE;
		return;
	}
	else
	{
		//保存视类指针
		m_pComTest->SetParentWnd(this);
		//打开串口准备读串口
		CString strPort,strBaudRate;
		m_PortComBox.GetWindowTextW(strPort);
		m_BaudRateComBox.GetWindowTextW(strBaudRate);
		strPort+=_T(":");
		int nBaudRate=::_ttoi(strBaudRate.GetBuffer());
		m_fLineCom=m_pComTest->OpenConnection(strPort,nBaudRate);
		//串口初始化,读串口数据
		m_pComTest->bCONNECTED=TRUE;
		//线程开始执行
		m_pComTest->ResumeThread();
	}
}
//关闭串口
void CComVS_2005Dlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
	DWORD dwStatus;
	VERIFY(::GetExitCodeThread(m_pComTest->m_hThread,&dwStatus));
	m_pComTest->CloseConnection();
}

void CComVS_2005Dlg::OnBnClickedButton4()
{
	// TODO: Add your control notification handler code here
	m_ReadEdit.SetWindowTextW(_T(""));
}

⌨️ 快捷键说明

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