请问在Dev C++中如何动态设定串口接受缓冲区的大小.

rolinyang 2004-11-24 10:20:47
请问在Dev C++中如何动态设定串口接受缓冲区的大小.
...全文
176 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
rolinyang 2004-11-24
谢谢
接受过后,如何清除缓冲区呢?
回复
lzzqqq 2004-11-24
//打开串口
BOOL CSerialWatch::OpenComm()
{
if (m_bOpened)
CloseComm();
::memset(&m_olWait,0,sizeof(m_olWait));
::memset(&m_olRead,0,sizeof(m_olRead));
::memset(&m_olWrite,0,sizeof(m_olWrite));
::memset(&m_RxBuffer,0,sizeof(m_RxBuffer));
ResetEvent(m_hExitEvent);

//打开指定的串口,并初始化参数。
DWORD dwErr;
DCB dcb;
COMMTIMEOUTS CommTimeOuts;
CString strName;

strName.Format("\\\\.\\COM%d",m_iCommNo);

//创建端口
m_hComm = CreateFile(strName,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (INVALID_HANDLE_VALUE == m_hComm)
{
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}
//通信端口的现存状态和I/O缓冲区
if(!::PurgeComm(m_hComm,
PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR))
{
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}

//设置收发缓冲
if(!::SetupComm(m_hComm,BUFFER_IN_SIZE,BUFFER_OUT_SIZE))
{
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}

//设置端口
if(!::GetCommState(m_hComm,&dcb))
{
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}

dcb.BaudRate =m_iBaudRate;
dcb.ByteSize=m_iDataBits;

int iStopBits=m_fStopBits*2;
switch(iStopBits)
{
case 2:
dcb.StopBits=ONESTOPBIT;
break;
case 3:
dcb.StopBits=ONE5STOPBITS;
break;
case 4:
dcb.StopBits=TWOSTOPBITS;
break;
default:
break;
}

switch(m_cParity)
{
case 'J':
dcb.fParity=true;
dcb.Parity=ODDPARITY;
break;
case 'O':
dcb.fParity=true;
dcb.Parity=EVENPARITY;
break;
case 'N':
dcb.fParity=false;
dcb.Parity=NOPARITY;
break;
default:
break;
}


if(!::SetCommState(m_hComm,&dcb))
{
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}

//设置通信超时属性
CommTimeOuts.ReadIntervalTimeout = 100;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;

if(!::SetCommTimeouts(m_hComm,&CommTimeOuts))
{
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
};

//初始化Overlapped参数
m_olWait.hEvent = ::CreateEvent(NULL,TRUE,FALSE,NULL);
if (NULL == m_olWait.hEvent)
{
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}

m_olRead.hEvent = ::CreateEvent(NULL,TRUE,FALSE,NULL);
if (NULL == m_olRead.hEvent)
{

::CloseHandle(m_olWait.hEvent);
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}

m_olWrite.hEvent = ::CreateEvent(NULL,TRUE,TRUE,NULL);
if (NULL == m_olWrite.hEvent)
{
::CloseHandle(m_olWait.hEvent);
::CloseHandle(m_olRead.hEvent);
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}

if (!::SetCommMask(m_hComm,EV_ERR|EV_RXCHAR))
{
dwErr = ::GetLastError();
::AfxTrace("****ErrorCode %d Found in CSerialWatch::OpenComm****",dwErr);
return FALSE;
}

//创建监视线程
DWORD dwThreadId;
char szMsg[80];

m_hThread = ::CreateThread(
NULL, // no security attributes
0, // use default stack size
ThreadFunc, // thread function
this, // argument to thread function
0, // use default creation flags
&dwThreadId);

if (m_hThread == NULL)
{
wsprintf( szMsg, "CreateThread failed." );
MessageBox( NULL, szMsg, "main", MB_OK );
};
m_bOpened=true;
return TRUE;
}
回复
相关推荐
基于java的企业人事管理系统设计--软件工程课程设计(含源码与论文设计).rar 1 引言 4 1.1 课程设计目标 4 1.2 编程工具(编程环境)介绍 4 1.3 实施时间及主要实施步骤 4 2 需求分析 5 3 系统总体设计 6 4 数据库设计 6 5 主要功能模块的设计与实现 10 5.1 功能模块1详细设计(综合查询员工信息)..........................................................10 5.1.1 详细设计.......................................................................................................10 5.1.2 算法流程........................................................................................................15 5.1.3 界面设计及测试结果.....................................................................................15 6 调试分析 15 7 用户手册 16 8 测试结果 17 8.1 员工信息的添加...................................................................................................17 8.2 员工信息的修改...................................................................................................17 8.3 员工信息的删除...................................................................................................18 8.4 员工信息的综合查询...........................................................................................18 8.5 员工信息按性别统计的结果................................................................................19 8.6 员工信息按状态统计的结果...............................................................................19 8.7 员工信息按职称统计的结果................................................................................20 9 结论 20 10 参考文献 20
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2004-11-24 10:20
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……