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

📄 evccommdlg.cpp

📁 eVCComm3.0.zip eVC3.0写的程序
💻 CPP
字号:
// eVCCommDlg.cpp : implementation file
//

#include "stdafx.h"
#include "eVCComm.h"
#include "eVCCommDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEVCCommDlg dialog

CEVCCommDlg::CEVCCommDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEVCCommDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEVCCommDlg)
	m_nPortNum = 2;
	m_sSendData = _T("");
	m_nRecvCount = 0;
	m_nSendCount = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEVCCommDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEVCCommDlg)
	DDX_Control(pDX, IDC_EDIT_RECVCOUNT, m_RecvCountCtrl);
	DDX_Control(pDX, IDC_EDIT_SENDCOUNT, m_SendCountCtrl);
	DDX_Control(pDX, IDC_EDIT_SEND_DATA, m_SendDataEditCtrl);
	DDX_Control(pDX, IDC_EDIT_RECV_DATA, m_RecvDataEditCtrl);
	DDX_Text(pDX, IDC_EDIT_PORT_NUM, m_nPortNum);
	DDV_MinMaxUInt(pDX, m_nPortNum, 0, 255);
	DDX_Text(pDX, IDC_EDIT_SEND_DATA, m_sSendData);
	DDV_MaxChars(pDX, m_sSendData, 255);
	DDX_Text(pDX, IDC_EDIT_RECVCOUNT, m_nRecvCount);
	DDX_Text(pDX, IDC_EDIT_SENDCOUNT, m_nSendCount);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEVCCommDlg, CDialog)
	//{{AFX_MSG_MAP(CEVCCommDlg)
	ON_BN_CLICKED(IDC_BUTTON_OPENPORT, OnButtonOpenport)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_RTS, OnButtonRts)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEVCCommDlg message handlers

BOOL CEVCCommDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	m_SerialPort.set_data_callback(OnDataFromPort,this);
	
	GetDlgItem(IDC_STATIC_RECV_DATA)->SetWindowText(_T("Received Data(HEX)"));
	GetDlgItem(IDC_STATIC_SEND_DATA)->SetWindowText(_T("Send Data"));	
	GetDlgItem(IDC_BUTTON_OPENPORT)->SetWindowText(_T("Open"));
	GetDlgItem(IDC_BUTTON_SEND)->SetWindowText(_T("Send"));
	GetDlgItem(IDC_BUTTON_CLEAR)->SetWindowText(_T("Clear"));
	GetDlgItem(IDC_STATIC_PORTNUM)->SetWindowText(_T("Port Number"));

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

void CEVCCommDlg::OnButtonOpenport() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_SerialPort.IsOpen())
	{
		m_SerialPort.ClosePort();
		GetDlgItem(IDC_BUTTON_OPENPORT)->SetWindowText(_T("Open"));
	}
	else
	{
		if(m_SerialPort.OpenPort(m_nPortNum))
		{
			GetDlgItem(IDC_BUTTON_OPENPORT)->SetWindowText(_T("Close"));
		}
		else
		{
			MessageBox(_T("Open Port Error"),_T("Info"),MB_ICONINFORMATION);
		}
	}
}

void CEVCCommDlg::OnButtonSend() 
{
	char * pszANSIString;
	int len;
	UpdateData();
//	m_sSendData=_T("hello");
	len=m_sSendData.GetLength()*2;
	pszANSIString=(char *)malloc(len);
	memset(pszANSIString,0,len);
	WideCharToMultiByte ( CP_ACP, 
                          WC_COMPOSITECHECK,
                          m_sSendData.GetBuffer(0),
                          -1, 
                          pszANSIString,
                          len,
                          NULL,
                          NULL);

	len=m_SerialPort.WriteData((BYTE *)pszANSIString,strlen(pszANSIString));
	m_sSendData=_T("");
	m_nSendCount+=len;
	
	free(pszANSIString);

	CString tmp;
	tmp.Format(_T("%d"),m_nSendCount);
	m_SendCountCtrl.SetWindowText(tmp);
	UpdateData(FALSE);
}

void CEVCCommDlg::OnDataFromPort(void * data, DWORD nDataCount,void * context)
{
	CEVCCommDlg *pDlg;
	static DWORD count=0;
	pDlg=(CEVCCommDlg *)context;
	char *buf;
	CString log;
	char tt[5];
	buf=(char *)data;
	for(DWORD i=0;i<nDataCount;i++)
	{
		sprintf(tt,"%02X ",(BYTE*)(buf[i] & 0xff));
		log+=tt;
		count++;
		if(count %10 ==0)
		{
			log+=_T("\r\n");
		}
	}
	pDlg->m_RecvDataEditCtrl.SetSel(pDlg->m_RecvDataEditCtrl.GetWindowTextLength(),pDlg->m_RecvDataEditCtrl.GetWindowTextLength());
	pDlg->m_RecvDataEditCtrl.ReplaceSel(log);
	pDlg->m_nRecvCount+=nDataCount;
	log.Format(_T("%d"),pDlg->m_nRecvCount);
	pDlg->m_RecvCountCtrl.SetWindowText(log);
}

void CEVCCommDlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	CString log;
	m_sSendData=_T("");
	m_RecvDataEditCtrl.SetWindowText(_T(""));
	m_nRecvCount=0;
	log.Format(_T("%d"),m_nRecvCount);
	m_RecvCountCtrl.SetWindowText(log);
	m_nSendCount=0;
	log.Format(_T("%d"),m_nSendCount);
	m_SendCountCtrl.SetWindowText(log);

}

void CEVCCommDlg::OnClose() 
{
	CDialog::OnClose();
}

void CEVCCommDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	if(m_SerialPort.IsOpen())
		m_SerialPort.ClosePort();
}

void CEVCCommDlg::OnButtonRts() 
{
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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