使用API异步串口通信 为什么每次都只能收到8个字节

wzyzb 2009-12-23 02:37:54
怎么设置,才能使改变每次收到的字节数呢?

线程中使用 WaitCommEvent 当EV_RXCHAR事件发生时,发送消息到主线程,OnReceive处理接收到的消息
下面是读缓冲
if (ClearCommError(m_hCom, &dwErr, &nStat)&&dwErr>0)
{
DT("read Function :ClearCommError return Error ");
PurgeComm(m_hCom, PURGE_RXABORT | PURGE_RXCLEAR);
return 0;
}
if(!ReadFile(m_hCom, chBuf, nSize, &lLen, &m_ro)) //ReadFile 怎么老是返回TRUE
{
if(GetLastError() == ERROR_IO_PENDING) //这里根本没有执行到
{
WaitForSingleObject(m_ro.hEvent,5000);
if(!GetOverlappedResult(m_hCom, &m_ro, &lLen, false))
{
if(GetLastError() != ERROR_IO_INCOMPLETE)
lLen = 0;
}
}
else
lLen = 0;
}
...全文
532 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhou1xp 2009-12-31
  • 打赏
  • 举报
回复
你在ReadFile(m_hCom, chBuf, nSize, &lLen, &m_ro)) 的时候将nSize设为1,用do while(lLen==1)来读取,我在上面说过定义一个字节数组,这样就可以完全接受了
一条晚起的虫 2009-12-30
  • 打赏
  • 举报
回复
// 字节间超时设置的长一些,串口软缓冲设的大些。
// 8个一触发主要是由于串口芯片的硬缓存是8个字节。
zhou1xp 2009-12-30
  • 打赏
  • 举报
回复
我曾经写过一个串口监控程序,假如你的串口中读入了80个字符,那么监控程序将接受到10个数据已经读入的信号,它是8个字节就会发一个接受到的信号,我建议你用BYTE数组来接受数据
zgl7903 2009-12-30
  • 打赏
  • 举报
回复
if(!ReadFile(m_hCom, chBuf, nSize, &lLen, &m_ro)) //ReadFile 怎么老是返回TRUE
nSize是多少?
wzyzb 2009-12-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zoulie 的回复:]
MSDN的例子
The following sample code illustrates testing for end-of-file for an asynchronous read operation:

// set up overlapped structure fields
// to simplify this sample, we'll eschew an event handle
gOverLapped.Offset    = 0;
gOverLapped.OffsetHigh = 0;
gOverLapped.hEvent    = NULL;

// attempt an asynchronous read operation
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead,
    &gOverlapped) ;

// if there was a problem, or the async. operation's still pending ...
if (!bResult)
{
    // deal with the error code
    switch (dwError = GetLastError())
    {
        case ERROR_HANDLE_EOF:
        {
            // we're reached the end of the file
            // during the call to ReadFile

            // code to handle that
        }

        case ERROR_IO_PENDING:
        {
            // asynchronous i/o is still in progress

            // do something else for a while
            GoDoSomethingElse() ;

            // check on the results of the asynchronous read
            bResult = GetOverlappedResult(hFile, &gOverlapped,
                &nBytesRead, FALSE) ;

            // if there was a problem ...
            if (!bResult)
            {
                // deal with the error code
                switch (dwError = GetLastError())
                {
                    case ERROR_HANDLE_EOF:
                    {
                        // we're reached the end of the file
                        //during asynchronous operation
                    }

                    // deal with other error cases
                }
            }
        } // end case

        // deal with other error cases

    } // end switch
} // end if


[/Quote]
很感谢你的回答, 这些例子我在msdn上周末没找到
wzyzb 2009-12-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lfchen 的回复:]
// 字节间超时设置的长一些,串口软缓冲设的大些。
// 8个一触发主要是由于串口芯片的硬缓存是8个字节。
[/Quote]我在读之前sleep一小段时间后 可以接受到全部发送的字节。 有没有什么办法可以让串口在接收到更多比如50(可以自己设置)才发送消息。 另外 我这里在线程中是不读数据的,waitcommEvent得到串口消息 EV_RXCHAR 线程就发送窗口消息,在窗口消息的响应函数里读数据。 现在我在窗口消息的响应函数里读数据之前
sleep 一段时间可以一次读完全部的数据(缓冲区4096),但线程现在发送了很多的消息(每8个字节发送一个)。

chinatotti 2009-12-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zgl7903 的回复:]
if(!ReadFile(m_hCom, chBuf, nSize, &lLen, &m_ro))  //ReadFile 怎么老是返回TRUE
nSize是多少?
[/Quote]
readfile 是BOOL型的当然返回TRUE,或者是FALSE;nSize是你要读取的数,llen存储的是实际读的数!
chinatotti 2009-12-30
  • 打赏
  • 举报
回复
自己循环处理下啊!
zoulie 2009-12-28
  • 打赏
  • 举报
回复
MSDN的例子
The following sample code illustrates testing for end-of-file for an asynchronous read operation:

// set up overlapped structure fields
// to simplify this sample, we'll eschew an event handle
gOverLapped.Offset = 0;
gOverLapped.OffsetHigh = 0;
gOverLapped.hEvent = NULL;

// attempt an asynchronous read operation
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead,
&gOverlapped) ;

// if there was a problem, or the async. operation's still pending ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
// during the call to ReadFile

// code to handle that
}

case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress

// do something else for a while
GoDoSomethingElse() ;

// check on the results of the asynchronous read
bResult = GetOverlappedResult(hFile, &gOverlapped,
&nBytesRead, FALSE) ;

// if there was a problem ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
//during asynchronous operation
}

// deal with other error cases
}
}
} // end case

// deal with other error cases

} // end switch
} // end if

zoulie 2009-12-28
  • 打赏
  • 举报
回复
readfile返回true表示操作完成,看下lLen的值
wzyzb 2009-12-23
  • 打赏
  • 举报
回复
我看到很多2002年的帖子 也是这个问题都没提供解决方法 难道没人解决吗?
wzyzb 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tr0j4n 的回复:]
参考
[/Quote] 谢谢 还是没解决问题
MoXiaoRab 2009-12-23
  • 打赏
  • 举报
回复

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧