如何在readfile中设置重叠参数? [英] How do I set the parameter of overlapped in readfile?

查看:143
本文介绍了如何在readfile中设置重叠参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我想通过VC ++编码从其他服务器通过异步串口接收数据。



I使用ReadFile()函数,如下所示:



char buf [1024];

size = 1;



ReadFile(hComm,buf,size,dwRead,0);



这可以按顺序接收一个字节和一个字节的数据。



但是在线程更改或更快的波特率情况下,接收过程中的任何操作都可能会丢失来自上面端口的数据。



所以我会使用重叠结构变量将状态从信号状态更改为事件驱动。



作为一个新手,我很难找到样本ReadFile()使用重叠的最后一个参数。



为了安全接收,我被推荐在ReadFile()中使用重叠变量不会丢失任何字符串行通信 - 这是对的吗?



所以,请让我知道正确样品的方法。



谢谢。



我尝试过的方法:



这个问题浪费了1周。

Hi
I would like to receive data via async serial port from another server by coding VC++.

I use the ReadFile() function like as this:

char buf[1024];
size = 1;

ReadFile (hComm, buf, size, dwRead, 0);

This can receive data one byte and one byte sequentially.

But any action during receiving may lost the data from above port in the case of thread change or faster baudrate.

So I would change the state from signalled state to event driven using overlapped structure variable.

As a novice, It is very difficult for me to find sample of ReadFile() using last parameter overlapped.

To receive safely, I was recommened that "using overlapped variable" in the ReadFile() do not lost any character in the serial commnication - is this right?

So, please let me know the method with right sample of it.

Thank you.

What I have tried:

More 1 week wasted for this problem.

推荐答案

我不知道你浪费了一个星期的时间,但谷歌花了1秒才找到同步和重叠的输入和输出(Windows) [ ^ ]。
I don't know whay you wasted a week doing, but it took Google 1 second to find Synchronization and Overlapped Input and Output (Windows)[^].


可以在串口通讯 [ ^ ]。



使用重叠I / O时,必须将重叠结构传递给所有 ReadFile() WriteFile()来电。对于可能处于挂起状态的每个操作,您必须创建一个事件,将事件句柄放在相应的重叠结构中,调用 WaitForSingleObject() / WaitForMultipleObjects() GetOverlappedResult()事件发生时。



但是对于串行接收,通常无法预测可以接收多少字节。然后,您还可以使用串行事件在数据到达时收到通知。要执行此操作,请使用 EV_RXCHAR 调用 SetCommMask(),并可选择 EV_ERR ,并在接收循环中 WaitCommEvent()传递另一个重叠结构。我在此CP解决方案中提供了一次示例代码:串行通信问题 [ ^ ]



EV_RXCHAR EV_ERR 事件后调用 ClearCommError( )检查错误并获取可用字节数。将上面的可用字节数传递给 ReadFile()时,可以在那里简单地使用空重叠结构,因为调用将立即返回(无挂起)。当使用我上面的解决方案中的代码时,它应该如下所示:

A simplified example and descriptions can be found at Serial Communications[^].

When using overlapped I/O, you have to pass an overlapped structure to all ReadFile() and WriteFile() calls. For each operation that might be in a pending state you have to create an event, place the event handle in the corresponding overlapped structure, call WaitForSingleObject() / WaitForMultipleObjects() and GetOverlappedResult() when the event occured.

But with serial receiving it is often not predictable how many bytes may be received. Then you might also use serial events to be notified when data has been arrived. To do this call SetCommMask() with EV_RXCHAR and optionally EV_ERR, and in the receive loop WaitCommEvent() passing another overlapped structure. I have provided sample code once in this CP solution: Serial Communication issue[^]

Upon the EV_RXCHAR and EV_ERR events call then ClearCommError() to check for errors and get the number of available bytes. When passing the number of available bytes from the above to ReadFile(), you can simple use an empty overlapped structure there because the call will return immediately (no pending). When using the code from my above solution it should look like:
// At begin of receive thread function
OVERLAPPED ovRead = {0};

// Inside loop after waiting
if (nEvent & (EV_RXCHAR | EV_ERR))
{
    DWORD dwComErrors = 0;
    COMSTAT tComStat;
    ::ClearCommError(m_hCom, &dwComErrors, &tComStat);
    if (dwComErrors)
    {
        // handle error here
    }
    if (tComStat.cbInQue)
    {
        DWORD dwRead;
        // Ensure that pBuf is large enough.
        // If not, read and process data in chunks.
        // This call will succeed (no pending) because we know how many data
        //  are available.
        // So we can pass an empty overlapped structure here.
        ::ReadFile(m_hCom, pBuf, tComStat.cbInQue, &dwRead, &ovRead);
    }
}




Quote:

安全接收,我被推荐在ReadFile()中使用重叠变量不会丢失串行通信中的任何字符 - 这是对吗?

To receive safely, I was recommened that "using overlapped variable" in the ReadFile() do not lost any character in the serial commnication - is this right?

这是不正确的,因为当接收事件不是时,内部接收缓冲区仍可能溢出及时处理。

That is not right because the internal receive buffer may still overrun when receive events are not handled in time.


这篇关于如何在readfile中设置重叠参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆