1

我正在调用WriteFile将数据发送到调制解调器:

BOOL writeResult = WriteFile(m_hPort, p_message, length, &numOut, NULL);

在哪里:

  • m_hPort是有效的HANDLE
  • p_message 是一个unsigned char*包含ate0\r
  • 长度是int一个值为 5
  • numOutunsigned long初始化为 0

偶尔我会看到这个方法成功但是 numOut != length

WriteFile 如何在不发送任何数据的情况下返回成功?

编辑这就是我创建句柄的方式:

HANDLE hPort = CreateFileA("\\\\.\\COM5", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

我已经检查了退货,但这并不INVALID_HANDLE_VALUE表明它是有效的。

4

2 回答 2

1

该文档指出:

当写入缓冲区空间不足的非阻塞字节模式管道句柄时,WriteFile 返回 TRUE,并带有 *lpNumberOfBytesWritten < nNumberOfBytesToWrite

当您发送数据的速度快于传输速度时,调制解调器会不会也有类似的行为?

于 2012-02-24T10:16:55.950 回答
1

毕竟是硬件问题。

重新启动转换器将清除硬件缓冲区,导致我们怀疑这是该设备的驱动程序的问题。根据 Hans 的建议,我们放弃了超时,然后开始更详细地调查错误报告。

Reducing the timeout to something sensible meant we were able to identify that the buffers were filling up until they cannae' take nae' more! Which is why power cycling the converter fixed it (we tried re-routing power through engineering and reversing the polarity, but unusually, neither resolved the issue).

The root cause was some flaky hardware flow control causing the software to block indefinitely. Disabling the flow control solved the issue.

于 2012-03-12T18:03:16.643 回答