当前位置:首页 > 编程开发

WINCE下如何使用串口!

webgou16年前 (2010-09-08)编程开发173
1.创建线程

////////////////////////////////////////////////////////////////////////////

DWORD CApplicationDlg::CommRecvTread(LPVOID lparam)
{
DWORD dwLength;
char *recvBuf = new char[1024];
CBuletoothApplicationDlg *pDlg = (CBuletoothApplicationDlg*)lparam;

while(TRUE)
{
if (WaitForSingleObject(pDlg->m_ExitThreadEvent, 0) == WAIT_OBJECT_0)
break;

if (pDlg->m_hComm != INVALID_HANDLE_VALUE)
{
BOOL fReadState = ReadFile(pDlg->m_hComm, recvBuf, 1024, &dwLength, NULL);
if(!fReadState)
{
//MessageBox(_T("无法从串口读取数据!"));
}
else
{
if(dwLength != 0)
OnCommRecv(pDlg, recvBuf, dwLength);
}
}
}

delete[] recvBuf;
return 0;
}

///////////////////////////////////////////////////////////////////////////////

2.打开串口,创建串口接收线程

//////////////////////////////////////////////////////////////////////////////

void CApplicationDlg::OnButtonPair()
{
// TODO: Add your control notification handler code here
////////////////////////////////////////////////////////
Sleep(100);
////////////////////////////////////////////////////////
DWORD IDThread;
HANDLE hRecvThread;
UpdateData(TRUE);

CString strPort = PorTbl[m_ComboPort.GetCurSel()];
DWORD baud = BaudTbl[m_ComboBaud.GetCurSel()];
DWORD databit = DataBitTbl[1];
BYTE stopbit = StopBitTbl[0];
BYTE parity = ParityTbl[0];

BOOL ret = OpenPort(strPort, baud, databit, stopbit, parity);
if (ret == FALSE)
return;

m_ExitThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

// 创建串口接收线程
hRecvThread = CreateThread(0, 0, CommRecvTread, this, 0, &IDThread);
if (hRecvThread == NULL)
{
MessageBox(_T("创建接收线程失败!"));
return;
}
CloseHandle(hRecvThread);

m_ButOpen.EnableWindow(FALSE);
m_ButClose.EnableWindow(TRUE);
MessageBox(_T("打开") + strPort + _T("成功!"));
}
/////////////////////////////////////////////////////////////////////////////////

3.串口接收线程

////////////////////////////////////////////////////////////////////////////////

DWORD CApplicationDlg::CommRecvTread(LPVOID lparam)
{

DWORD dwLength;
char *recvBuf = new char[1024];
CApplicationDlg *pDlg = (CApplicationDlg*)lparam;

while(TRUE)
{
if (WaitForSingleObject(pDlg->m_ExitThreadEvent, 0) == WAIT_OBJECT_0)
break;

if (pDlg->m_hComm != INVALID_HANDLE_VALUE)
{
BOOL fReadState = ReadFile(pDlg->m_hComm, recvBuf, 1024, &dwLength, NULL);
if(!fReadState)
{
//MessageBox(_T("无法从串口读取数据!"));
}
else
{
if(dwLength != 0)
OnCommRecv(pDlg, recvBuf, dwLength);
}
}
}

delete[] recvBuf;
return 0;

}

/////////////////////////////////////////////////////////////////////////////////

4.串口接收信息处理

////////////////////////////////////////////////////////////////////////////////

void CALLBACK CApplicationDlg::OnCommRecv(CWnd* pWnd, char *buf, int buflen)

{

//此处代码可以自己写了

//接收到的串口信息处理部分

}

//////////////////////////////////////////////////////////////////////////////

5.关闭串口

/////////////////////////////////////////////////////////////////////////////////

BOOL CApplicationDlg::ClosePort(void)
{
if(m_hComm != INVALID_HANDLE_VALUE)
{
SetCommMask(m_hComm, 0);
PurgeComm(m_hComm, PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(m_hComm);
m_hComm = INVALID_HANDLE_VALUE;
return TRUE;
}

return FALSE;
}

/////////////////////////////////////////////////////////////////////////////////

来源:http://hi.baidu.com/sudshine/blog/item/435135b26c5535aad9335ae4.html

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=318

标签: wince
分享给朋友:

“WINCE下如何使用串口!” 的相关文章

atl CSimpleArray

// This is a part of the Active Template Library.// Copyright (C) Microsoft Corporation// All rights reserved.//// This source code is only intended a…

详解Android源码的编译详解Android源码的编译

本文将为大家介绍的是如何设置Android源码的编译环境,包括Linux下的配置。主要基于Android 1.0环境,希望对大家了解Android开发有所帮助。本次编译过程主要参考官方文档(http://source.Android.com/download)和网上相关资料(如http://blog…

谁偷了我的热更新?Mono,JIT,IOS

 谁偷了我的热更新?Mono,JIT,IOS…

Using Blocks in iOS 4: The Basics

July 28, 2010 by Mike ClarkiOS 4 introduces one new feature that will fundamentally change the way you program in general: blocks. Blocks are an exten…

GDB调试精粹及使用实例

一:列文件清单 1. List (gdb) list line1,line2 二:执行程序 要想运行准备调试的程序,可使用run命令,在它后面可以跟随发给该程序的任何参数,包括标准输入和标准输出说明符()和外壳通配符(*、?、[、])在内。 如果你使用不带参数的run命令,gdb就再次使用你给予前一…

devenv.exe的命令行参数

devenv.exe的命令行参数…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。