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

📄 comtestevcdlg.cpp

📁 要求NK中包含串口驱动组件工作正常
💻 CPP
字号:
// ComTestEVCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ComTestEVC.h"
#include "ComTestEVCDlg.h"
#include "COMPort.h"


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


uint WM_COMUSERDEFINE=WM_USER+100;
/////////////////////////////////////////////////////////////////////////////
// CComTestEVCDlg dialog

CComTestEVCDlg::CComTestEVCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CComTestEVCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CComTestEVCDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CComTestEVCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CComTestEVCDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CComTestEVCDlg, CDialog)
	//{{AFX_MSG_MAP(CComTestEVCDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_MESSAGE(WM_COMUSERDEFINE,OnUserMSG)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComTestEVCDlg message handlers

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

	this->Com1=NULL;
	this->Com2=NULL;
	CurrentData=new byte[COM_FRAMECOUNT*COM_SINGLEFRAME];
	displaychar=new wchar_t[COM_FRAMECOUNT*COM_SINGLEFRAME];
	

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



void CComTestEVCDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	//open com1
	if (this->Com1!=NULL){
		COMPort_DeInit(this->Com1);
		this->Com1=NULL; 
	}
	this->Com1=COMPort_Init(COM1,
		              COM_Baudrate_115200,
					  COM_Parity_None,
					  COM_ByteSize_8,
					  COM_StopBits_1,
					  COM_FRAMECOUNT,
					  COM_SINGLEFRAME,
					  PRIORITY_BELOW_NORMAL,
					  PRIORITY_ABOVE_NORMAL,
					  WM_COMUSERDEFINE,
					  this->m_hWnd);
	if (this->Com1!=NULL){
		AfxMessageBox(_T("Com1 opened"));
	}
	else{
		AfxMessageBox(_T("Com1 opened Error"));

	}
}

void CComTestEVCDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	//open com2
	if (this->Com2!=NULL){
		COMPort_DeInit(this->Com2);
		this->Com2=NULL; 
	}
	this->Com2=COMPort_Init(COM2,
		              COM_Baudrate_115200,
					  COM_Parity_None,
					  COM_ByteSize_8,
					  COM_StopBits_1,
					  COM_FRAMECOUNT,
					  COM_SINGLEFRAME,
					  PRIORITY_BELOW_NORMAL,
					  PRIORITY_ABOVE_NORMAL,
					  WM_COMUSERDEFINE,
					  this->m_hWnd);
	if (this->Com2!=NULL){
		AfxMessageBox(_T("Com2 opened"));
	}
	else{
		AfxMessageBox(_T("Com2 opened Error"));
	}
	
}

void CComTestEVCDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default

	//如果打开串口,关闭
	if (this->Com1!=NULL){
		COMPort_DeInit(this->Com1);
		this->Com1=NULL; 
	}
	if (this->Com2!=NULL){
		COMPort_DeInit(this->Com2);
		this->Com2=NULL; 
	}

	delete this->CurrentData; 
	delete this->displaychar; 
	
	CDialog::OnClose();
}

void CComTestEVCDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	//com1 send
	byte a[20];
	for (int i=0;i<20;i++) a[i]='A'+i;
	if (this->Com1!=NULL){
		COMPort_SendByte(this->Com1,a,20);
	}

	
}

void CComTestEVCDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	//com2 send
	byte a[20];
	for (int i=0;i<20;i++) a[i]='a'+i;
	if (this->Com2!=NULL){
		COMPort_SendByte(this->Com2,a,20);
	}

	
}
LRESULT CComTestEVCDlg::OnUserMSG(WPARAM wParam, LPARAM lParam)
{

	LPVOID ComHandle=(LPVOID)wParam;
	int ComNo=*((int*)lParam)+1;

	CString s;
	uint ReadBytes;

	CListBox* ListBox;

	if ((this->Com1!=NULL) && (this->Com1==ComHandle)){
		//com1 Recv data
		COMPort_RecvByte(ComHandle,&this->CurrentData[0],&ReadBytes);
		s.Format(_T("COMHandle=%d COMNO=COM %d count=%d "),ComHandle,ComNo,ReadBytes);
		uint j;
		for (j=0;j<ReadBytes;j++){
			this->displaychar[j]=(wchar_t)(this->CurrentData[j]);
		}
		this->displaychar[j]=0x00;
		ListBox = (CListBox*)GetDlgItem(IDC_LIST1);
		s+=this->displaychar;
		ListBox->AddString(s);
		//ListBox->AddString(this->displaychar);
	}

	//com2
	if ((this->Com2!=NULL) && (this->Com2==ComHandle)){
		//com1 Recv data
		COMPort_RecvByte(ComHandle,&this->CurrentData[0],&ReadBytes);
		s.Format(_T("COMHandle=%d COMNO=COM %d count=%d "),ComHandle,ComNo,ReadBytes);
		uint j=0;
		for (j=0;j<ReadBytes;j++){
			this->displaychar[j]=(wchar_t)(this->CurrentData[j]);
		}
		this->displaychar[j]=0x00;
		ListBox = (CListBox*)GetDlgItem(IDC_LIST2);
		s+=this->displaychar;
		ListBox->AddString(s);
	}

	return 0;

}

void CComTestEVCDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	CListBox* ListBox = (CListBox*)GetDlgItem(IDC_LIST1);
	ListBox->ResetContent();
	ListBox = (CListBox*)GetDlgItem(IDC_LIST2);
	ListBox->ResetContent();

	
}

void CComTestEVCDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	//如果打开串口,关闭
	if (this->Com1!=NULL){
		COMPort_DeInit(this->Com1);
		this->Com1=NULL; 
	}
	
}

void CComTestEVCDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	if (this->Com2!=NULL){
		COMPort_DeInit(this->Com2);
		this->Com2=NULL; 
	}
	
}

⌨️ 快捷键说明

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