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

📄 evccommdlg.cpp

📁 wince下多串口并发程序
💻 CPP
字号:
// eVCCommDlg.cpp : implementation file
//

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

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

#define TIMER_OPEN_PORT 1001
/////////////////////////////////////////////////////////////////////////////
// CEVCCommDlg dialog

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

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_RECV_DATA, m_RecvDataEditCtrl);
	DDX_Control(pDX, IDC_EDIT_SENDCOUNT, m_SendCountCtrl);
	DDX_Text(pDX, IDC_EDIT_SEND_DATA, m_sSendData);
	DDX_Text(pDX, IDC_EDIT_SENDCOUNT, m_nSendCount);
	DDX_Text(pDX, IDC_EDIT_RECVCOUNT, m_nRecvCount);
	//}}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_WM_TIMER()
	//}}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

	CSettingDlg settings;
	if(settings.DoModal()==IDCANCEL)
	{
		CDialog::OnCancel();
		return FALSE;
	}
	m_nPortNum=settings.m_nPort;
	m_SerialPort.m_nBaudRate=settings.m_nBaudrate;

	// TODO: Add extra initialization here
	m_SerialPort.set_data_callback(OnDataFromPort,this);
	
	ActivateTopParent();
//	OnButtonOpenport();

	SetTimer(TIMER_OPEN_PORT,500,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CEVCCommDlg::OnButtonOpenport() 
{
	if(m_SerialPort.IsOpen())
	{
		m_SerialPort.ClosePort();
		GetDlgItem(IDC_BUTTON_OPENPORT)->SetWindowText(_T("OpenPort"));
	}
	else
	{
		if(m_SerialPort.OpenPort(m_nPortNum))
		{
			GetDlgItem(IDC_BUTTON_OPENPORT)->SetWindowText(_T("ClosePort"));
		}
		else
		{
			MessageBox(_T("Open Port Error"),_T("Info"),MB_ICONINFORMATION);
		}
	}	
}

/*
	将数据转换成ANS字符后从串口发送出去
*/
void CEVCCommDlg::OnButtonSend() 
{
	char * pszANSIString;
	int len;
	UpdateData();

	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::OnButtonClear() 
{
	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::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 %16 ==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::OnDestroy() 
{
	CDialog::OnDestroy();
	if(m_SerialPort.IsOpen())
		m_SerialPort.ClosePort();
}

void CEVCCommDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==TIMER_OPEN_PORT)
	{
		KillTimer(TIMER_OPEN_PORT);
		OnButtonOpenport();
	}
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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