问题描述
您好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.
推荐答案
// 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);
}
}
安全接收,我被推荐在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中设置重叠参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!