Win32 API:ReadFile 未超时
我正在编写一些代码来与硬件交互。硬件通过 USB 连接到 PC,设备内部有 USB 转串口转换器(在 Windows 中显示为 COM 端口设备)。
我在使用 Win32 API ReadFile 系统调用时遇到问题。我似乎无法让它像广告中那样工作。我已经将 COMMTIMEOUTS 结构设置如下:
COMMTIMEOUTS ct;
ct.ReadIntervalTimeout = MAXDWORD;
ct.ReadTotalTimeoutconstant = 0;
ct.ReadTotalTimeoutMultiplier = 0;
ct.WriteTotalTimeoutConstant = 0;
ct.WriteTotalTimeoutMultiplier = 0;
if(SetCommTimeouts(device_id_, &ct) == 0)
{
return ERROR; // this is never hit.
}
根据 Win32 API 文档,它说:
读取间隔超时
最长时间 允许在到达之间经过 通信上的两个字节 行,以毫秒为单位。期间 ReadFile操作,时间段 当第一个字节是时开始 已收到。如果之间的间隔 任意两个字节的到达超过此值 金额,ReadFile操作是 已完成且所有缓冲数据均已完成 回来了。零值表示 不使用间隔超时。
MAXDWORD 值,与 两个值均为零 ReadTotalTimeoutConstant 和 ReadTotalTimeoutMultiplier 成员, 指定读操作是 立即返回字节 已经收到的,甚至 如果没有收到字节。
我发送的命令应该返回一个单字节整数。大多数时候,设备接收命令并返回适当的值。然而,有时,它似乎不会返回值,并且 ReadFile() 会阻塞,直到收到更多字节(例如,通过按下设备上的按钮)。一旦按下按钮,我期望的初始整数响应就会与按钮按下代码一起收到。虽然这不是我所期望的设备本身的行为,但根据 MSDN 文档,我更关心的是 ReadFile() 不应该阻塞的情况。有没有办法解决 ReadFile() 阻塞的问题?
I'm writing some code to interface with a piece of hardware. The hardware connects to the PC via a USB with a USB-to-Serial converter inside the device (it shows up as a COM port device in Windows).
I'm having issues with the Win32 API ReadFile system call. I can't seem to get it to work as advertised. I've setup the COMMTIMEOUTS structure as so:
COMMTIMEOUTS ct;
ct.ReadIntervalTimeout = MAXDWORD;
ct.ReadTotalTimeoutconstant = 0;
ct.ReadTotalTimeoutMultiplier = 0;
ct.WriteTotalTimeoutConstant = 0;
ct.WriteTotalTimeoutMultiplier = 0;
if(SetCommTimeouts(device_id_, &ct) == 0)
{
return ERROR; // this is never hit.
}
Which according to the Win32 API documentation, says:
ReadIntervalTimeout
The maximum time
allowed to elapse between the arrival
of two bytes on the communications
line, in milliseconds. During a
ReadFile operation, the time period
begins when the first byte is
received. If the interval between the
arrival of any two bytes exceeds this
amount, the ReadFile operation is
completed and any buffered data is
returned. A value of zero indicates
that interval time-outs are not used.A value of MAXDWORD, combined with
zero values for both the
ReadTotalTimeoutConstant and
ReadTotalTimeoutMultiplier members,
specifies that the read operation is
to return immediately with the bytes
that have already been received, even
if no bytes have been received.
The command I'm sending is supposed to return a single byte integer. Most of the time, the command is received by the device and it returns the appropriate value. Sometimes, however, it doesn't seem to return a value and ReadFile() blocks until more bytes are recieved (eg. by pressing buttons on the device). Once a button is hit, the initial integer response I was expecting is received along with the button press code. While this isn't the behavior I'm expecting from the device itself, I'm more concerned with ReadFile() blocking when it shouldn't be, according to the MSDN documentation. Is there a remedy for ReadFile() blocking here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
噢!事实证明,ReadFile 阻塞只是一个症状,而不是问题。相关硬件设备仅配备 4MHz 处理器。将写入设备的 3 个字符命令分开并单独发送,字符之间有 1 毫秒的暂停,可以解决此问题。
D'oh! Turns out ReadFile blocking was just a symptom, not the problem. The hardware device in question only has a 4MHz processor in it. Splitting up the 3 character command written to the device and sending them individually with a 1ms pause between characters fixes the issue.