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

📄 instestdlg.cpp

📁 wince 下的串口编程
💻 CPP
字号:
// InsTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "InsTest.h"
#include "InsTestDlg.h"
#include "serial.h"

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

/////////////////////////////////////////////////////////////////////////////
// CInsTestDlg dialog

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

void CInsTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInsTestDlg)
	DDX_Control(pDX, IDC_BTN_DISCON, m_btndiscon);
	DDX_Control(pDX, IDC_BTN_CON, m_btncon);
	DDX_Control(pDX, IDC_EDIT_REC, m_EditRecCtrl);
	DDX_Text(pDX, IDC_EDIT_COM, m_editcom);
	DDV_MaxChars(pDX, m_editcom, 10);
	DDX_Text(pDX, IDC_EDIT_SEND, m_editsend);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInsTestDlg, CDialog)
	//{{AFX_MSG_MAP(CInsTestDlg)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_CON, OnBtnCon)
	ON_BN_CLICKED(IDC_BTN_DISCON, OnBtnDiscon)
	ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInsTestDlg message handlers
DWORD  WINAPI  ComReadThread(LPVOID lpVoid);


BOOL CInsTestDlg::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_editcom = L"COM2:";
	UpdateData(FALSE);
	m_btncon.EnableWindow(TRUE);
	m_btndiscon.EnableWindow(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

static BYTE * Buffer =new BYTE[1024 + 64];
CSerial * CInsTestDlg::GetSerialPort()
{
	return &m_serial;
}

DWORD  WINAPI  ComReadThread(LPVOID lpVoid)
{
	CInsTestDlg *dlg = (CInsTestDlg *)lpVoid;
	CSerial * serial  = dlg->GetSerialPort();
	HANDLE hPort = serial->GetHandle();


	DWORD ret;		//读入信息时返回值,若为TRUE就是正确读入
	DWORD dwCommModemStatus;//串口状态,以判断是否有事件发生
	DWORD dwLength;//读入信息的长度
	COMSTAT ComStat;//串口状态的详细情况表,
	DWORD dwErrorFlags;				//读串口状态的标志

	//设置程序响应的事件
//	SetCommMask (hPort, EV_RXCHAR);
	//程序在串口有效的状态下,无限循环
	while (hPort != INVALID_HANDLE_VALUE) {
         //等待串口的事件发生,当dwCommModemStatus值为1时表示接收到数据
  //      WaitCommEvent (hPort, &dwCommModemStatus, 0);
   //     SetCommMask (hPort, EV_RXCHAR);//重新设置程序响应的事件,但这个只是保证程序的安全性
		                               //一般并不起作用
   //     if (dwCommModemStatus & EV_RXCHAR) //检测收到的事件是否为"接收字符"的事件
//		{ 
 //           ClearCommError(hPort, &dwErrorFlags, &ComStat);//清除串口状态标志,并返回当前状态
			//cbInQue返回在串行驱动程序输入队列中的字符数

   //         dwLength = (ComStat.cbInQue < 1024) ? ComStat.cbInQue : 1024;

     //       if(dwLength > 0) {		//防止无故产生事件
			     //读入数据,并返回数据长度,采用同步方式                
				ret = serial->ReadPort(Buffer, 1);           
					dlg->OnComReadData(Buffer, ret);

	//		} 
	//	}

//       PurgeComm(hPort,PURGE_RXCLEAR);//清空串口的接收缓冲区,必要性不大,但有保证作用
		//重新获得串口状态,必要性不大,48表示没有事件产生
     hPort = serial->GetHandle();
	 }
     delete[] Buffer;//释放缓冲区
	
	 //Just for debug
//	::MessageBox (NULL, TEXT("Thread Run Out!"),  TEXT("Error!"), MB_OK);

     return 0L;
}


void CInsTestDlg::OnComReadData(BYTE *pDat, DWORD dwLen)
{
	DWORD i;
	int nLength;
	unsigned short disp[2048];
	if (dwLen <= 0) return;
	for (i =0; i < dwLen ; i++)
		disp[i] = pDat[i];
	disp[dwLen] = 0;
	nLength=m_EditRecCtrl.SendMessage(WM_GETTEXTLENGTH);
	m_EditRecCtrl.SetSel(nLength,nLength);
	m_EditRecCtrl.ReplaceSel(disp);
}

void CInsTestDlg::StrToByteArray(CString str, CByteArray *pByteArray, int nLen)
{
	int i;
	for (i=0;i<nLen;i++)
		pByteArray->Add(str.GetAt(i));
}


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



void CInsTestDlg::OnBtnCon() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (!m_serial.OpenPort(m_editcom))
		return;
	DWORD dwThreadID;
	if (m_hThreadComRead = CreateThread(NULL, 0, ComReadThread, this, 0,&dwThreadID));
	//	CloseHandle(m_hThreadComRead);
	else 	//不能创建线程
		MessageBox (TEXT("创建串口监听线程失败!"),  TEXT("Error!"), MB_OK);
	m_btncon.EnableWindow(FALSE);
	m_btndiscon.EnableWindow(TRUE);
}

void CInsTestDlg::OnBtnDiscon() 
{
	
	m_serial.ClosePort();
//	TerminateThread(m_hThreadComRead, 0);
	m_btncon.EnableWindow(TRUE);
	m_btndiscon.EnableWindow(FALSE);
}

void CInsTestDlg::OnBtnSend() 
{
	UpdateData(TRUE);
	CByteArray ByteArraySendText;
	int nlen;
	nlen = m_editsend.GetLength();
	StrToByteArray(m_editsend, &ByteArraySendText, nlen);
	m_serial.WritePort(&ByteArraySendText[0], nlen);
}

⌨️ 快捷键说明

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