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

📄 serialtestdlg.cpp

📁 一个关于计算机串口通讯的源代码
💻 CPP
字号:
// SerialTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SerialTest.h"
#include "SerialTestDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSerialTestDlg dialog

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

void CSerialTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerialTestDlg)
	DDX_Control(pDX, IDC_EDIT_SEND, m_edtSend);
	DDX_Control(pDX, IDC_EDIT_RECEIVE, m_edtReceive);
	DDX_Text(pDX, IDC_EDIT_PORT, m_dwPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSerialTestDlg, CDialog)
	//{{AFX_MSG_MAP(CSerialTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN_COMM, OnOpenComm)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_BTN_CPU_SPEED, OnBtnCpuSpeed)
	ON_EN_KILLFOCUS(IDC_EDIT_PORT, OnKillfocusEditPort)
	//}}AFX_MSG_MAP
    ON_MESSAGE(WM_COMM_RXCHAR, Handler)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerialTestDlg message handlers

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

void CSerialTestDlg::OnOpenComm() 
{
	// TODO: Add your control notification handler code here
	m_CommPort.InitPort(m_hWnd, m_dwPort);
    m_CommPort.StartMonitoring();
}

void CSerialTestDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
    TCHAR szBuffer[1024] = { '\0' };
    m_edtSend.GetWindowText(szBuffer, 1024);
    int i = m_edtSend.GetWindowTextLength();
/*
    for(int j = 0; j<i && j<1024; j++)
    {
        m_CommPort.WriteToPort(szBuffer[j]);
    }
//*/////
    m_CommPort.WriteToPort(szBuffer);
}

#define MAX_RECIEVED_CHAR 1024 

LRESULT CSerialTestDlg::Handler(WPARAM wParam, LPARAM lParam)
{
     TCHAR szBuffer[MAX_RECIEVED_CHAR]={'\0'};

     int i =  GetDlgItem(IDC_EDIT_RECEIVE)->GetWindowTextLength();
     if (i > MAX_RECIEVED_CHAR -1) 
     {
         GetDlgItem(IDC_EDIT_RECEIVE)->SetWindowText(szBuffer);
         i = 0;
     }
     GetDlgItem(IDC_EDIT_RECEIVE)->GetWindowText(szBuffer, MAX_RECIEVED_CHAR);

     szBuffer[i] = wParam; 
     GetDlgItem(IDC_EDIT_RECEIVE)->SetWindowText(szBuffer);
     return 0;
}


void CSerialTestDlg::OnBtnCpuSpeed() 
{
	// TODO: Add your control notification handler code here
    KTimer timer; 
    TCHAR mess[128];
    timer.Start(); 
    Sleep(1000); 
    unsigned cpuspeed10 = (unsigned)(timer.Stop()/100000);
    timer.Start(); 
    CreateSolidBrush(RGB(0xAA, 0xAA, 0xAA)); 
    unsigned time = (unsigned) timer.Stop();
    
    wsprintf(mess, 
        _T("CPU speed %d.%d mhz\n") 
        _T("KTimer overhead %d clock cycles\n") 
        _T("CreateSolidBrush %d clock cycles %d ns"), 
        cpuspeed10 / 10, cpuspeed10 % 10, (unsigned) timer.m_overHead, 
        time, time * 10000 / cpuspeed10);
    ::MessageBox(NULL, mess, _T("How fast is GDI?"), MB_OK);
}

void CSerialTestDlg::OnKillfocusEditPort() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
}

⌨️ 快捷键说明

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