COM 端口上的 Win32 重叠读取文件返回 ERROR_OPERATION_ABORTED

发布于 2024-07-09 02:34:43 字数 1360 浏览 5 评论 0原文

好吧,这是一个针对 SO 蜂巢思维的代码……

直到今天,我的代码在许多系统上运行得很好,并且部署在许多站点上。 它涉及从串行端口读取和写入数据的线程。

尝试检查新设备时,我的代码在 ReadFile 之后调用 GetOverlappedResult 时遇到了 995 ERROR_OPERATION_ABORTED 错误。 有时读取会起作用,有时我会收到此错误。 令人惊讶的是,只需忽略错误并重试就可以在不丢失任何数据的情况下工作。 不需要 ClearCommError。

这是片段。

if (!ReadFile(handle,&c,1,&read, &olap))
    {
        if (GetLastError() != ERROR_IO_PENDING)
        {
            logger().log_api(LOG_ERROR,"ser_rx_char:ReadFile");
            throw Exception("ser_rx_char:ReadFile");
        }
    }

    WaitForSingleObjectEx(r_event, INFINITE, true);  // alertable, so, thread can be closed correctly.

    if (GetOverlappedResult(handle,&olap,&read, TRUE) != 0)
    {
        if (read != 1)
            throw Exception("ser_rx_char: no data");

        logger().log(LOG_VERBOSE,"read char %d ( read = %d) ",c, read);
    }
    else
    {
        DWORD err = GetLastError();
        if (err != 995)   //Filters our ERROR_OPERATION_ABORTED
        {
            logger().log_api(LOG_ERROR,"ser_rx_char: GetOverlappedResult");
            throw Exception("ser_rx_char:GetOverlappedResult");
        }
    }

我的第一个猜测是归咎于 COM 端口驱动程序,我以前没有使用过它(它是 Blackmagic Decklink 上的 RS422 端口,仅供参考),但这感觉像是一种逃避。

哦,还有 Vista SP1 Business 32 位,天哪。

在我将其归结为“其他人的问题”之前,有人知道可能导致此问题的原因吗?

Ok, one for the SO hive mind...

I have code which has - until today - run just fine on many systems and is deployed at many sites. It involves threads reading and writing data from a serial port.

Trying to check out a new device, my code was swamped with 995 ERROR_OPERATION_ABORTED errors calling GetOverlappedResult after the ReadFile. Sometimes the read would work, othertimes I'd get this error. Just ignoring the error and retrying would - amazingly - work without dropping any data. No ClearCommError required.

Here's the snippet.

if (!ReadFile(handle,&c,1,&read, &olap))
    {
        if (GetLastError() != ERROR_IO_PENDING)
        {
            logger().log_api(LOG_ERROR,"ser_rx_char:ReadFile");
            throw Exception("ser_rx_char:ReadFile");
        }
    }

    WaitForSingleObjectEx(r_event, INFINITE, true);  // alertable, so, thread can be closed correctly.

    if (GetOverlappedResult(handle,&olap,&read, TRUE) != 0)
    {
        if (read != 1)
            throw Exception("ser_rx_char: no data");

        logger().log(LOG_VERBOSE,"read char %d ( read = %d) ",c, read);
    }
    else
    {
        DWORD err = GetLastError();
        if (err != 995)   //Filters our ERROR_OPERATION_ABORTED
        {
            logger().log_api(LOG_ERROR,"ser_rx_char: GetOverlappedResult");
            throw Exception("ser_rx_char:GetOverlappedResult");
        }
    }

My first guess is to blame the COM port driver, which I havent' used before (it's a RS422 port on a Blackmagic Decklink, FYI), but that feels like a cop-out.

Oh, and Vista SP1 Business 32-bit, for my sins.

Before I just put this down to "Someone else's problem", does anyone have any ideas of what might cause this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2024-07-16 02:34:43

您如何在 ReadFile 之前设置 OVERLAPPED 结构? - 我总是将它们归零(显然,hEvent 除外),这可能部分是迷信,但我有一种感觉,这在过去给我带来了问题。

我担心责怪驱动程序(如果它是非 MS 并且不仅仅是参考文献中的微小调整)并不是完全不现实的。 编写 COM 驱动程序是一件极其复杂的事情,测试它的困难在于编写的每个应用程序对串行端口及其 IOCTL 的使用都略有不同。

另一个常见问题是未设置整个端口 - 例如未调用 SetCommTimeouts 或 SetupComm。 我不知道你是否犯了这种错误,但我遇到过一些人,他们说他们没有使用超时,但实际上他们的意思是他们没有调用 SetCommTimeouts,所以他们正在使用它们,但没有他们设置的概念...

对于第 3 方 COM 驱动程序来说,这种东西可能是谋杀,因为人们经常使用 MS 驱动程序摆脱任何旧的废话,并且它并不总是与另一个设备。

How are you setting over the OVERLAPPED structure before the ReadFile? - I always zero them (other than the hEvent, obviously), which is perhaps part superstition, but I have a feeling that it's caused me a problem in the past.

I'm afraid blaming the driver (if it's non-MS and not just a tiny tweak from the reference) is not completely unrealistic. To write a COM driver is an incredibly complex thing, and the difficulty with testing it is that every application ever written uses the serial ports and their IOCTLs slightly differently.

Another common problem is not to set the whole port up - for example not calling SetCommTimeouts or SetupComm. I've no idea if you're making this sort of mistake, but I have met people who say they're not using timeouts when they actually mean that they didn't call SetCommTimeouts so they're using them but don't have a notion what they're set to...

This kind of stuff can be murder for 3rd-party COM drivers, because people have often got away with any old crap with the MS driver, and it doesn't always work the same with another device.

枕花眠 2024-07-16 02:34:43

除了将 OVERLAPPED 归零之外,您还可以检查如何设置 olap.hEvent,即 CreateEvent 的参数是什么? 如果您正在创建一个预先发出信号的事件(即 CreateEvent 的第三个参数为 TRUE),我希望立即返回。 另外,不要忘记,如果您将manualReset(CreateEvent 的第二个参数)指定为FALSE,GetOverlappedResult() 将帮助您清除事件——这可能解释了为什么它第二次起作用。

无法从您的代码片段中真正判断出这些是否会影响您 - 希望这会有所帮助。

in addition to zeroing the OVERLAPPED, you might also check how you're setting olap.hEvent, that is, what are your arguments to CreateEvent? If you're creating an event that's pre-signalled (i.e. the third argument to CreateEvent is TRUE) I would expect an immediate return. Also, don't forget that if you specify manualReset (the second argument to CreateEvent) as FALSE, GetOverlappedResult() will helpfully clear the event for you - which might explain why it works the second time around.

Can't really tell from your snippet whether either of these affect you - hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文